// @(#)root/io:$Id$
// Author: Rene Brun   19/05/2006

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TFileCacheRead
#define ROOT_TFileCacheRead


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFileCacheRead                                                       //
//                                                                      //
// TFile cache when reading                                             //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TObject
#include "TObject.h"
#endif

#ifndef ROOT_TFile
#include "TFile.h"
#endif

class TBranch;
class TFilePrefetch;

class TFileCacheRead : public TObject {

protected:
   TFilePrefetch *fPrefetch;         //! Object that does the asynchronous reading in another thread
   Int_t          fBufferSizeMin;    // Original size of fBuffer
   Int_t          fBufferSize;       // Allocated size of fBuffer (at a given time)
   Int_t          fBufferLen;        // Current buffer length (<= fBufferSize)

   Long64_t       fBytesRead;        // Number of bytes read for this cache
   Long64_t       fBytesReadExtra;   // Number of extra bytes (overhead) read by the readahead buffer
   Int_t          fReadCalls;        // Number of read calls for this cache
   Long64_t       fNoCacheBytesRead; // Number of bytes read by basket to fill cached tree
   Int_t          fNoCacheReadCalls; // Number of read calls by basket to fill cached tree

   Bool_t         fAsyncReading;
   Bool_t         fEnablePrefetching;// reading by prefetching asynchronously

   Int_t          fNseek;            // Number of blocks to be prefetched
   Int_t          fNtot;             // Total size of prefetched blocks
   Int_t          fNb;               // Number of long buffers
   Int_t          fSeekSize;         // Allocated size of fSeek
   Long64_t      *fSeek;             //[fNseek] Position on file of buffers to be prefetched
   Long64_t      *fSeekSort;         //[fNseek] Position on file of buffers to be prefetched (sorted)
   Int_t         *fSeekIndex;        //[fNseek] sorted index table of fSeek
   Long64_t      *fPos;              //[fNb] start of long buffers
   Int_t         *fSeekLen;          //[fNseek] Length of buffers to be prefetched
   Int_t         *fSeekSortLen;      //[fNseek] Length of buffers to be prefetched (sorted)
   Int_t         *fSeekPos;          //[fNseek] Position of sorted blocks in fBuffer
   Int_t         *fLen;              //[fNb] Length of long buffers
   TFile         *fFile;             // Pointer to file
   char          *fBuffer;           //[fBufferSize] buffer of contiguous prefetched blocks
   Bool_t         fIsSorted;         // True if fSeek array is sorted
   Bool_t         fIsTransferred;    // True when fBuffer contains something valid
   Long64_t       fPrefetchedBlocks; // Number of blocks prefetched.

   //variables for the second block prefetched with the same semantics as for the first one
   Int_t          fBNseek;
   Int_t          fBNtot;
   Int_t          fBNb;
   Int_t          fBSeekSize;
   Long64_t      *fBSeek;        //[fBNseek]
   Long64_t      *fBSeekSort;    //[fBNseek]
   Int_t         *fBSeekIndex;   //[fBNseek]
   Long64_t      *fBPos;         //[fBNb]
   Int_t         *fBSeekLen;     //[fBNseek]
   Int_t         *fBSeekSortLen; //[fBNseek]
   Int_t         *fBSeekPos;     //[fBNseek]
   Int_t         *fBLen;         //[fBNb]
   Bool_t         fBIsSorted;
   Bool_t         fBIsTransferred;

   void SetEnablePrefetchingImpl(Bool_t setPrefetching = kFALSE); // Can not be virtual as it is called from the constructor.

private:
   TFileCacheRead(const TFileCacheRead &);            //cannot be copied
   TFileCacheRead& operator=(const TFileCacheRead &);

public:
   TFileCacheRead();
   TFileCacheRead(TFile *file, Int_t buffersize, TObject *tree = 0);
   virtual ~TFileCacheRead();
   virtual Int_t       AddBranch(TBranch * /*b*/, Bool_t /*subbranches*/ = kFALSE) { return 0; }
   virtual Int_t       AddBranch(const char * /*branch*/, Bool_t /*subbranches*/ = kFALSE) { return 0; }
   virtual void        AddNoCacheBytesRead(Long64_t len) { fNoCacheBytesRead += len; }
   virtual void        AddNoCacheReadCalls(Int_t reads) { fNoCacheReadCalls += reads; }
   virtual void        Close(Option_t *option="");
   virtual Int_t       GetBufferSize() const { return fBufferSize; };
   virtual Long64_t    GetBytesRead() const { return fBytesRead; }
   virtual Long64_t    GetNoCacheBytesRead() const { return fNoCacheBytesRead; }
   virtual Long64_t    GetBytesReadExtra() const { return fBytesReadExtra; }
           TFile      *GetFile() const { return fFile; }   // Return the TFile being cached.
           Int_t       GetNseek() const { return fNseek; } // Return the number of blocks in the current cache.
           Int_t       GetNtot() const { return fNtot; }   // Return the total size of the prefetched blocks.
   virtual Int_t       GetReadCalls() const { return fReadCalls; }
   virtual Int_t       GetNoCacheReadCalls() const { return fNoCacheReadCalls; }
   virtual Int_t       GetUnzipBuffer(char ** /*buf*/, Long64_t /*pos*/, Int_t /*len*/, Bool_t * /*free*/) { return -1; }
           Long64_t    GetPrefetchedBlocks() const { return fPrefetchedBlocks; }
   virtual Bool_t      IsAsyncReading() const { return fAsyncReading; };
   virtual void        SetEnablePrefetching(Bool_t setPrefetching = kFALSE);
   virtual Bool_t      IsEnablePrefetching() const { return fEnablePrefetching; };
   virtual Bool_t      IsLearning() const {return kFALSE;}
   virtual void        Prefetch(Long64_t pos, Int_t len);
   virtual void        Print(Option_t *option="") const;
   virtual Int_t       ReadBufferExt(char *buf, Long64_t pos, Int_t len, Int_t &loc);
   virtual Int_t       ReadBufferExtNormal(char *buf, Long64_t pos, Int_t len, Int_t &loc);
   virtual Int_t       ReadBufferExtPrefetch(char *buf, Long64_t pos, Int_t len, Int_t &loc);
   virtual Int_t       ReadBuffer(char *buf, Long64_t pos, Int_t len);
   virtual Int_t       SetBufferSize(Int_t buffersize);
   virtual void        SetFile(TFile *file, TFile::ECacheAction action = TFile::kDisconnect);
   virtual void        SetSkipZip(Bool_t /*skip*/ = kTRUE) {} // This function is only used by TTreeCacheUnzip (ignore it)
   virtual void        Sort();
   virtual void        SecondSort();                          //Method used to sort and merge the chunks in the second block
   virtual void        SecondPrefetch(Long64_t, Int_t);       //Used to add chunks to the second block
   virtual TFilePrefetch* GetPrefetchObj();
   virtual void        WaitFinishPrefetch();                  //Gracefully join the prefetching thread

   ClassDef(TFileCacheRead,2)  //TFile cache when reading
};

#endif
 TFileCacheRead.h:1
 TFileCacheRead.h:2
 TFileCacheRead.h:3
 TFileCacheRead.h:4
 TFileCacheRead.h:5
 TFileCacheRead.h:6
 TFileCacheRead.h:7
 TFileCacheRead.h:8
 TFileCacheRead.h:9
 TFileCacheRead.h:10
 TFileCacheRead.h:11
 TFileCacheRead.h:12
 TFileCacheRead.h:13
 TFileCacheRead.h:14
 TFileCacheRead.h:15
 TFileCacheRead.h:16
 TFileCacheRead.h:17
 TFileCacheRead.h:18
 TFileCacheRead.h:19
 TFileCacheRead.h:20
 TFileCacheRead.h:21
 TFileCacheRead.h:22
 TFileCacheRead.h:23
 TFileCacheRead.h:24
 TFileCacheRead.h:25
 TFileCacheRead.h:26
 TFileCacheRead.h:27
 TFileCacheRead.h:28
 TFileCacheRead.h:29
 TFileCacheRead.h:30
 TFileCacheRead.h:31
 TFileCacheRead.h:32
 TFileCacheRead.h:33
 TFileCacheRead.h:34
 TFileCacheRead.h:35
 TFileCacheRead.h:36
 TFileCacheRead.h:37
 TFileCacheRead.h:38
 TFileCacheRead.h:39
 TFileCacheRead.h:40
 TFileCacheRead.h:41
 TFileCacheRead.h:42
 TFileCacheRead.h:43
 TFileCacheRead.h:44
 TFileCacheRead.h:45
 TFileCacheRead.h:46
 TFileCacheRead.h:47
 TFileCacheRead.h:48
 TFileCacheRead.h:49
 TFileCacheRead.h:50
 TFileCacheRead.h:51
 TFileCacheRead.h:52
 TFileCacheRead.h:53
 TFileCacheRead.h:54
 TFileCacheRead.h:55
 TFileCacheRead.h:56
 TFileCacheRead.h:57
 TFileCacheRead.h:58
 TFileCacheRead.h:59
 TFileCacheRead.h:60
 TFileCacheRead.h:61
 TFileCacheRead.h:62
 TFileCacheRead.h:63
 TFileCacheRead.h:64
 TFileCacheRead.h:65
 TFileCacheRead.h:66
 TFileCacheRead.h:67
 TFileCacheRead.h:68
 TFileCacheRead.h:69
 TFileCacheRead.h:70
 TFileCacheRead.h:71
 TFileCacheRead.h:72
 TFileCacheRead.h:73
 TFileCacheRead.h:74
 TFileCacheRead.h:75
 TFileCacheRead.h:76
 TFileCacheRead.h:77
 TFileCacheRead.h:78
 TFileCacheRead.h:79
 TFileCacheRead.h:80
 TFileCacheRead.h:81
 TFileCacheRead.h:82
 TFileCacheRead.h:83
 TFileCacheRead.h:84
 TFileCacheRead.h:85
 TFileCacheRead.h:86
 TFileCacheRead.h:87
 TFileCacheRead.h:88
 TFileCacheRead.h:89
 TFileCacheRead.h:90
 TFileCacheRead.h:91
 TFileCacheRead.h:92
 TFileCacheRead.h:93
 TFileCacheRead.h:94
 TFileCacheRead.h:95
 TFileCacheRead.h:96
 TFileCacheRead.h:97
 TFileCacheRead.h:98
 TFileCacheRead.h:99
 TFileCacheRead.h:100
 TFileCacheRead.h:101
 TFileCacheRead.h:102
 TFileCacheRead.h:103
 TFileCacheRead.h:104
 TFileCacheRead.h:105
 TFileCacheRead.h:106
 TFileCacheRead.h:107
 TFileCacheRead.h:108
 TFileCacheRead.h:109
 TFileCacheRead.h:110
 TFileCacheRead.h:111
 TFileCacheRead.h:112
 TFileCacheRead.h:113
 TFileCacheRead.h:114
 TFileCacheRead.h:115
 TFileCacheRead.h:116
 TFileCacheRead.h:117
 TFileCacheRead.h:118
 TFileCacheRead.h:119
 TFileCacheRead.h:120
 TFileCacheRead.h:121
 TFileCacheRead.h:122
 TFileCacheRead.h:123
 TFileCacheRead.h:124
 TFileCacheRead.h:125
 TFileCacheRead.h:126
 TFileCacheRead.h:127
 TFileCacheRead.h:128
 TFileCacheRead.h:129
 TFileCacheRead.h:130
 TFileCacheRead.h:131
 TFileCacheRead.h:132
 TFileCacheRead.h:133
 TFileCacheRead.h:134