// @(#)root/io:$Id$
// Author: Elvin Sindrilaru   19/05/2011

/*************************************************************************
 * Copyright (C) 1995-2011, 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_TFilePrefetch
#define ROOT_TFilePrefetch

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFilePrefetch                                                        //
//                                                                      //
// The prefetching mechanism uses two classes (TFilePrefetch and        //
// TFPBlock) to prefetch in advance a block of tree entries. There is   //
// a thread which takes care of actually transferring the blocks and    //
// making them available to the main requesting thread. Therefore,      //
// the time spent by the main thread waiting for the data before        //
// processing considerably decreases. Besides the prefetching           //
// mechanisms there is also a local caching option which can be         //
// enabled by the user. Both capabilities are disabled by default       //
// and must be explicitly enabled by the user.                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TFile
#include "TFile.h"
#endif
#ifndef ROOT_TThread
#include "TThread.h"
#endif
#ifndef ROOT_TFPBlock
#include "TFPBlock.h"
#endif
#ifndef ROOT_TCondition
#include "TCondition.h"
#endif
#ifndef ROOT_TSemaphore
#include "TSemaphore.h"
#endif
#ifndef ROOT_TMD5
#include "TMD5.h"
#endif
#ifndef ROOT_TObject
#include "TObject.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef ROOT_TObjString
#include "TObjString.h"
#endif
#ifndef ROOT_TMutex
#include "TMutex.h"
#endif
#ifndef ROOT_TObjArray
#include "TObjArray.h"
#endif
#ifndef ROOT_TStopwatch
#include "TStopwatch.h"
#endif


class TFilePrefetch : public TObject {

private:
   TFile      *fFile;              // reference to the file
   TList      *fPendingBlocks;     // list of pending blocks to be read
   TList      *fReadBlocks;        // list of blocks read
   TThread    *fConsumer;          // consumer thread
   TMutex     *fMutexPendingList;  // mutex for the pending list
   TMutex     *fMutexReadList;     // mutex for the list of read blocks
   TCondition *fNewBlockAdded;     // signal the addition of a new pending block
   TCondition *fReadBlockAdded;    // signal the addition of a new red block
   TSemaphore *fSemMasterWorker;   // semaphore used to kill the consumer thread
   TSemaphore *fSemWorkerMaster;   // semaphore used to notify the master that worker is killed
   TSemaphore *fSemChangeFile;     // semaphore used when changin a file in TChain
   TString     fPathCache;         // path to the cache directory
   TStopwatch  fWaitTime;          // time wating to prefetch a buffer (in usec)
   Bool_t      fThreadJoined;      // mark if async thread was joined

   static TThread::VoidRtnFunc_t ThreadProc(void*);  //create a joinable worker thread

public:
   TFilePrefetch(TFile*);
   virtual ~TFilePrefetch();

   void      ReadAsync(TFPBlock*, Bool_t&);
   void      ReadListOfBlocks();

   void      AddPendingBlock(TFPBlock*);
   TFPBlock *GetPendingBlock();

   void      AddReadBlock(TFPBlock*);
   Bool_t    ReadBuffer(char*, Long64_t, Int_t);
   void      ReadBlock(Long64_t*, Int_t*, Int_t);
   TFPBlock *CreateBlockObj(Long64_t*, Int_t*, Int_t);

   TThread  *GetThread() const;
   Int_t     ThreadStart();

   Bool_t    SetCache(const char*);
   Bool_t    CheckBlockInCache(char*&, TFPBlock*);
   char     *GetBlockFromCache(const char*, Int_t);
   void      SaveBlockInCache(TFPBlock*);

   Int_t     SumHex(const char*);
   Bool_t    BinarySearchReadList(TFPBlock*, Long64_t, Int_t, Int_t*);
   Long64_t  GetWaitTime();

   void      SetFile(TFile*);
   TCondition* GetCondNewBlock() const { return fNewBlockAdded; };
   void      WaitFinishPrefetch();

   ClassDef(TFilePrefetch, 0);  // File block prefetcher
};

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