Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Int_t gDebug
Definition TROOT.cxx:597
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.
TFileCacheWrite()
Default Constructor.
~TFileCacheWrite() override
Destructor.
Bool_t fRecursive
flag to avoid recursive calls
void Print(Option_t *option="") const override
Print class internal structure.
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.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:2274
virtual void SetCacheWrite(TFileCacheWrite *cache)
Set a pointer to the write cache.
Definition TFile.cxx:2388
virtual Bool_t WriteBuffer(const char *buf, Int_t len)
Write a buffer to the file.
Definition TFile.cxx:2476
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:961
Basic string class.
Definition TString.h:139
void ToLower()
Change string to lower-case.
Definition TString.cxx:1182