Logo ROOT  
Reference Guide
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
15A cache when writing files over the network
16
17A caching system to speed up network I/O, i.e. when there is
18no operating system caching support (like the buffer cache for
19local disk I/O). The cache makes sure that every I/O is done with
20a (large) fixed length buffer thereby avoiding many small I/O's.
21Currently the write cache system is used by the classes TNetFile,
22TXNetFile and TWebFile (via TFile::WriteBuffers()).
23
24The 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
33
34////////////////////////////////////////////////////////////////////////////////
35/// Default Constructor.
36
38{
39 fBufferSize = 0;
40 fNtot = 0;
41 fSeekStart = 0;
42 fFile = 0;
43 fBuffer = 0;
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
54 : TObject()
55{
56 if (buffersize < 10000) buffersize = 512000;
57 fBufferSize = buffersize;
58 fSeekStart = 0;
59 fNtot = 0;
60 fFile = file;
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
71{
72 delete [] fBuffer;
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Flush the current write buffer to the file.
77/// Returns kTRUE in case of error.
78
80{
81 if (!fNtot) return kFALSE;
83 //printf("Flushing buffer at fSeekStart=%lld, fNtot=%d\n",fSeekStart,fNtot);
87 fNtot = 0;
88 return status;
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// Print class internal structure.
93
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
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
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
136 fFile->Seek(pos); // Flush may have changed this
137 if (fFile->WriteBuffer(buf,len)) return -1; // failure
139 return 1;
140 }
141 }
142 if (!fNtot) fSeekStart = pos;
143 memcpy(fBuffer+fNtot,buf,len);
144 fNtot += len;
145
146 return 1;
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Set the file using this cache.
151/// Any write not yet flushed will be lost.
152
154{
155 fFile = file;
156}
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
long long Long64_t
Definition: RtypesCore.h:69
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
R__EXTERN Int_t gDebug
Definition: Rtypes.h:91
A cache when writing files over the network.
Int_t fBufferSize
Allocated size of fBuffer.
Int_t fNtot
Total size of cached blocks.
char * fBuffer
[fBufferSize] buffer of contiguous prefetched blocks
virtual Bool_t Flush()
Flush the current write buffer to the file.
TFile * fFile
Pointer to file.
virtual void Print(Option_t *option="") const
Print class internal structure.
TFileCacheWrite()
Default Constructor.
Bool_t fRecursive
flag to avoid recursive calls
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.
virtual void SetFile(TFile *file)
Set the file using this cache.
Long64_t fSeekStart
Seek value of first block in cache.
virtual Int_t WriteBuffer(const char *buf, Long64_t pos, Int_t len)
Write buffer at position pos in the write buffer.
virtual ~TFileCacheWrite()
Destructor.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
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:2115
virtual Bool_t WriteBuffer(const char *buf, Int_t len)
Write a buffer to the file.
Definition: TFile.cxx:2317
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
Basic string class.
Definition: TString.h:131
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
Definition: file.py:1