Loading [MathJax]/extensions/tex2jax.js
ROOT
6.06/09
Reference Guide
ROOT Home Page
Main Page
Related Pages
User's Classes
Namespaces
All Classes
Files
Release Notes
File List
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
io
io
src
TFileCacheWrite.cxx
Go to the documentation of this file.
1
// @(#)root/io:$Id$
2
// Author: Rene Brun 18/05/2006
3
4
/*************************************************************************
5
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6
* All rights reserved. *
7
* *
8
* For the licensing terms see $ROOTSYS/LICENSE. *
9
* For the list of contributors see $ROOTSYS/README/CREDITS. *
10
*************************************************************************/
11
12
/**
13
\class TFileCacheWrite TFileCacheWrite.cxx
14
\ingroup IO
15
A cache when writing files over the network
16
17
A caching system to speed up network I/O, i.e. when there is
18
no operating system caching support (like the buffer cache for
19
local disk I/O). The cache makes sure that every I/O is done with
20
a (large) fixed length buffer thereby avoiding many small I/O's.
21
Currently the write cache system is used by the classes TNetFile,
22
TXNetFile and TWebFile (via TFile::WriteBuffers()).
23
24
The write cache is automatically created when writing a remote file
25
(created in TFile::Open()).
26
*/
27
28
29
#include "TFile.h"
30
#include "
TFileCacheWrite.h
"
31
32
ClassImp
(
TFileCacheWrite
)
33
34
////////////////////////////////////////////////////////////////////////////////
35
/// Default Constructor.
36
37
TFileCacheWrite
::
TFileCacheWrite
() :
TObject
()
38
{
39
fBufferSize = 0;
40
fNtot = 0;
41
fSeekStart = 0;
42
fFile = 0;
43
fBuffer = 0;
44
fRecursive =
kFALSE
;
45
}
46
47
////////////////////////////////////////////////////////////////////////////////
48
/// Creates a TFileCacheWrite data structure.
49
/// The write cache will be connected to file.
50
/// The size of the cache will be buffersize,
51
/// if buffersize < 10000 a default size of 512 Kbytes is used
52
53
TFileCacheWrite::TFileCacheWrite
(
TFile
*file,
Int_t
buffersize)
54
:
TObject
()
55
{
56
if
(buffersize < 10000) buffersize = 512000;
57
fBufferSize
= buffersize;
58
fSeekStart
= 0;
59
fNtot
= 0;
60
fFile
= file;
61
fRecursive
=
kFALSE
;
62
fBuffer
=
new
char
[
fBufferSize
];
63
if
(file) file->
SetCacheWrite
(
this
);
64
if
(
gDebug
> 0)
Info
(
"TFileCacheWrite"
,
"Creating a write cache with buffersize=%d bytes"
,buffersize);
65
}
66
67
////////////////////////////////////////////////////////////////////////////////
68
/// Destructor.
69
70
TFileCacheWrite::~TFileCacheWrite
()
71
{
72
delete
[]
fBuffer
;
73
}
74
75
////////////////////////////////////////////////////////////////////////////////
76
/// Flush the current write buffer to the file.
77
/// Returns kTRUE in case of error.
78
79
Bool_t
TFileCacheWrite::Flush
()
80
{
81
if
(!
fNtot
)
return
kFALSE
;
82
fFile
->
Seek
(
fSeekStart
);
83
//printf("Flushing buffer at fSeekStart=%lld, fNtot=%d\n",fSeekStart,fNtot);
84
fRecursive
=
kTRUE
;
85
Bool_t
status
=
fFile
->
WriteBuffer
(
fBuffer
,
fNtot
);
86
fRecursive
=
kFALSE
;
87
fNtot
= 0;
88
return
status
;
89
}
90
91
////////////////////////////////////////////////////////////////////////////////
92
/// Print class internal structure.
93
94
void
TFileCacheWrite::Print
(
Option_t
*option)
const
95
{
96
TString
opt = option;
97
printf
(
"Write cache for file %s\n"
,
fFile
->
GetName
());
98
printf
(
"Size of write cache: %d bytes to be written at %lld\n"
,
fNtot
,
fSeekStart
);
99
opt.
ToLower
();
100
}
101
102
////////////////////////////////////////////////////////////////////////////////
103
/// Called by the read cache to check if the requested data is not
104
/// in the write cache buffer.
105
/// Returns -1 if data not in write cache,
106
/// 0 otherwise.
107
108
Int_t
TFileCacheWrite::ReadBuffer
(
char
*buf,
Long64_t
pos,
Int_t
len)
109
{
110
if
(pos < fSeekStart || pos+len >
fSeekStart
+
fNtot
)
return
-1;
111
memcpy(buf,
fBuffer
+pos-
fSeekStart
,len);
112
return
0;
113
}
114
115
////////////////////////////////////////////////////////////////////////////////
116
/// Write buffer at position pos in the write buffer.
117
/// The function returns 1 if the buffer has been successfully entered into the write buffer.
118
/// The function returns 0 in case WriteBuffer() was recusively called via Flush().
119
/// The function returns -1 in case of error.
120
121
Int_t
TFileCacheWrite::WriteBuffer
(
const
char
*buf,
Long64_t
pos,
Int_t
len)
122
{
123
if
(
fRecursive
)
return
0;
124
125
//printf("TFileCacheWrite::WriteBuffer, pos=%lld, len=%d, fSeekStart=%lld, fNtot=%d\n",pos,len,fSeekStart,fNtot);
126
127
if
(
fSeekStart
+
fNtot
!= pos) {
128
//we must flush the current cache
129
if
(
Flush
())
return
-1;
//failure
130
}
131
if
(
fNtot
+ len >=
fBufferSize
) {
132
if
(
Flush
())
return
-1;
//failure
133
if
(len >=
fBufferSize
) {
134
//buffer larger than the cache itself: direct write to file
135
fRecursive
=
kTRUE
;
136
if
(
fFile
->
WriteBuffer
(buf,len))
return
-1;
// failure
137
fRecursive
=
kFALSE
;
138
return
1;
139
}
140
}
141
if
(!
fNtot
)
fSeekStart
= pos;
142
memcpy(
fBuffer
+
fNtot
,buf,len);
143
fNtot
+= len;
144
145
return
1;
146
}
147
148
////////////////////////////////////////////////////////////////////////////////
149
/// Set the file using this cache.
150
/// Any write not yet flushed will be lost.
151
152
void
TFileCacheWrite::SetFile
(
TFile
*file)
153
{
154
fFile
= file;
155
}
TFileCacheWrite::fNtot
Int_t fNtot
Total size of cached blocks.
Definition:
TFileCacheWrite.h:26
Long64_t
long long Long64_t
Definition:
RtypesCore.h:69
TFileCacheWrite::TFileCacheWrite
TFileCacheWrite()
Option_t
const char Option_t
Definition:
RtypesCore.h:62
TFile::Seek
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Seek to a specific position in the file. Pos it either kBeg, kCur or kEnd.
Definition:
TFile.cxx:2085
TObject::Info
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition:
TObject.cxx:892
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition:
TFile.h:45
TString
Basic string class.
Definition:
TString.h:137
TString::ToLower
void ToLower()
Change string to lower-case.
Definition:
TString.cxx:1088
status
TAlienJobStatus * status
Definition:
TAlienJob.cxx:51
Int_t
int Int_t
Definition:
RtypesCore.h:41
Bool_t
bool Bool_t
Definition:
RtypesCore.h:59
kFALSE
const Bool_t kFALSE
Definition:
Rtypes.h:92
TFileCacheWrite::fBuffer
char * fBuffer
[fBufferSize] buffer of contiguous prefetched blocks
Definition:
TFileCacheWrite.h:28
TFileCacheWrite.h
TFileCacheWrite::~TFileCacheWrite
virtual ~TFileCacheWrite()
Destructor.
Definition:
TFileCacheWrite.cxx:70
TFileCacheWrite::Flush
virtual Bool_t Flush()
Flush the current write buffer to the file.
Definition:
TFileCacheWrite.cxx:79
TFileCacheWrite::Print
virtual void Print(Option_t *option="") const
Print class internal structure.
Definition:
TFileCacheWrite.cxx:94
TFile::WriteBuffer
virtual Bool_t WriteBuffer(const char *buf, Int_t len)
Write a buffer to the file.
Definition:
TFile.cxx:2288
TFileCacheWrite::fFile
TFile * fFile
Pointer to file.
Definition:
TFileCacheWrite.h:27
TFileCacheWrite::ReadBuffer
virtual Int_t ReadBuffer(char *buf, Long64_t pos, Int_t len)
Called by the read cache to check if the requested data is not in the write cache buffer...
Definition:
TFileCacheWrite.cxx:108
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition:
TNamed.h:51
TFileCacheWrite::fRecursive
Bool_t fRecursive
flag to avoid recursive calls
Definition:
TFileCacheWrite.h:29
ClassImp
ClassImp(TFileCacheWrite) TFileCacheWrite
Default Constructor.
Definition:
TFileCacheWrite.cxx:32
printf
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
TObject
Mother of all ROOT objects.
Definition:
TObject.h:58
TFileCacheWrite::WriteBuffer
virtual Int_t WriteBuffer(const char *buf, Long64_t pos, Int_t len)
Write buffer at position pos in the write buffer.
Definition:
TFileCacheWrite.cxx:121
TFileCacheWrite::fBufferSize
Int_t fBufferSize
Allocated size of fBuffer.
Definition:
TFileCacheWrite.h:25
gDebug
R__EXTERN Int_t gDebug
Definition:
Rtypes.h:128
TFileCacheWrite::fSeekStart
Long64_t fSeekStart
Seek value of first block in cache.
Definition:
TFileCacheWrite.h:24
TFileCacheWrite::SetFile
virtual void SetFile(TFile *file)
Set the file using this cache.
Definition:
TFileCacheWrite.cxx:152
kTRUE
const Bool_t kTRUE
Definition:
Rtypes.h:91
TFile::SetCacheWrite
virtual void SetCacheWrite(TFileCacheWrite *cache)
Set a pointer to the write cache.
Definition:
TFile.cxx:2200
TFileCacheWrite
A cache when writing files over the network.
Definition:
TFileCacheWrite.h:21