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_files
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 like
22TNetXNGFile (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;
56 fBufferSize = bufsize;
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;
81 fFile->Seek(fSeekStart);
82 //printf("Flushing buffer at fSeekStart=%lld, fNtot=%d\n",fSeekStart,fNtot);
84 Bool_t status = fFile->WriteBuffer(fBuffer, 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{
109 if (pos < fSeekStart || pos+len > fSeekStart+fNtot) return -1;
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}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
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
if(name) objname
Int_t gDebug
Definition TROOT.cxx:777
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 file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
virtual void SetCacheWrite(TFileCacheWrite *cache)
Set a pointer to the write cache.
Definition TFile.cxx:2440
TObject()
TObject constructor.
Definition TObject.h:259
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1072
Basic string class.
Definition TString.h:138
void ToLower()
Change string to lower-case.
Definition TString.cxx:1189