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,
22TNetXNGFile 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
32
33////////////////////////////////////////////////////////////////////////////////
34/// Default Constructor.
35
37{
38 fBufferSize = 0;
39 fNtot = 0;
40 fSeekStart = 0;
41 fFile = 0;
42 fBuffer = 0;
44}
45
46////////////////////////////////////////////////////////////////////////////////
47/// Creates a TFileCacheWrite data structure.
48/// The write cache will be connected to file.
49/// The size of the cache will be bufsize,
50/// if bufsize < 10000 a default size of 512 Kbytes is used
51
53 : TObject()
54{
55 if (bufsize < 10000) bufsize = 512000;
57 fSeekStart = 0;
58 fNtot = 0;
59 fFile = file;
61 fBuffer = new char[fBufferSize];
62 if (file) file->SetCacheWrite(this);
63 if (gDebug > 0) Info("TFileCacheWrite","Creating a write cache with buffersize=%d bytes",bufsize);
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Destructor.
68
73
74////////////////////////////////////////////////////////////////////////////////
75/// Flush the current write buffer to the file.
76/// Returns kTRUE in case of error.
77
79{
80 if (!fNtot) return kFALSE;
82 //printf("Flushing buffer at fSeekStart=%lld, fNtot=%d\n",fSeekStart,fNtot);
86 fNtot = 0;
87 return status;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Print class internal structure.
92
94{
95 TString opt = option;
96 printf("Write cache for file %s\n",fFile->GetName());
97 printf("Size of write cache: %d bytes to be written at %lld\n",fNtot,fSeekStart);
98 opt.ToLower();
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Called by the read cache to check if the requested data is not
103/// in the write cache buffer.
104/// Returns -1 if data not in write cache,
105/// 0 otherwise.
106
108{
110 memcpy(buf,fBuffer+pos-fSeekStart,len);
111 return 0;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Write buffer at position pos in the write buffer.
116/// The function returns 1 if the buffer has been successfully entered into the write buffer.
117/// The function returns 0 in case WriteBuffer() was recusively called via Flush().
118/// The function returns -1 in case of error.
119
121{
122 if (fRecursive) return 0;
123
124 //printf("TFileCacheWrite::WriteBuffer, pos=%lld, len=%d, fSeekStart=%lld, fNtot=%d\n",pos,len,fSeekStart,fNtot);
125
126 if (fSeekStart + fNtot != pos) {
127 //we must flush the current cache
128 if (Flush()) return -1; //failure
129 }
130 if (fNtot + len >= fBufferSize) {
131 if (Flush()) return -1; //failure
132 if (len >= fBufferSize) {
133 //buffer larger than the cache itself: direct write to file
135 fFile->Seek(pos); // Flush may have changed this
136 if (fFile->WriteBuffer(buf,len)) return -1; // failure
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
153{
154 fFile = file;
155}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:627
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:131
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:2304
virtual void SetCacheWrite(TFileCacheWrite *cache)
Set a pointer to the write cache.
Definition TFile.cxx:2418
virtual Bool_t WriteBuffer(const char *buf, Int_t len)
Write a buffer to the file.
Definition TFile.cxx:2506
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
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:1045
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189