00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Fennel_SegPageBackupRestoreDevice_Included
00025 #define Fennel_SegPageBackupRestoreDevice_Included
00026
00027 #include "fennel/common/ClosableObject.h"
00028 #include "fennel/common/FennelExcn.h"
00029 #include "fennel/synch/SynchObj.h"
00030 #include "fennel/device/RandomAccessRequest.h"
00031 #include "fennel/segment/SegPageLock.h"
00032
00033 #include <vector>
00034 #include <boost/enable_shared_from_this.hpp>
00035 #include <boost/scoped_array.hpp>
00036 #include <boost/scoped_ptr.hpp>
00037 #include <boost/utility.hpp>
00038
00039 FENNEL_BEGIN_NAMESPACE
00040
00044 class FENNEL_SEGMENT_EXPORT BackupRestorePage
00045 : public RandomAccessRequestBinding
00046 {
00050 PBuffer pBuffer;
00051
00056 BlockNum pageCounter;
00057
00061 uint bufferSize;
00062
00066 bool isRead;
00067
00072 WeakSegPageBackupRestoreDevice pParent;
00073
00074 public:
00075
00076 virtual PBuffer getBuffer() const;
00077 virtual uint getBufferSize() const;
00078 virtual void notifyTransferCompletion(bool bSuccess);
00079
00085 void setParent(WeakSegPageBackupRestoreDevice pParentInit);
00086
00092 void setBuffer(PBuffer pBuffer);
00093
00099 void setBufferSize(uint bufferSize);
00100
00105 BlockNum getPageCounter();
00106
00113 void setPageCounter(BlockNum counter);
00114
00120 void setReadRequest(bool isRead);
00121 };
00122
00126 class FENNEL_SEGMENT_EXPORT SegPageBackupRestoreDevice
00127 : public boost::noncopyable,
00128 public ClosableObject,
00129 public boost::enable_shared_from_this<SegPageBackupRestoreDevice>
00130 {
00134 std::string backupFilePathname;
00135
00139 uint nReservedPages;
00140
00144 SegmentAccessor scratchAccessor;
00145
00149 DeviceAccessScheduler &scheduler;
00150
00154 SharedRandomAccessDevice pDataDevice;
00155
00159 char *mode;
00160
00161
00162
00163
00164
00165 std::string compressionProgram;
00166
00167
00168
00169
00170
00171 uint nScratchPages;
00172
00176 bool isCompressed;
00177
00181 SegPageLock scratchLock;
00182
00186 FILE *backupFile;
00187
00191 uint pageSize;
00192
00196 boost::scoped_array<PBuffer> reservedPages;
00197
00202 std::vector<BackupRestorePage *> freeScratchPageQueue;
00203
00208 LocalCondition freeScratchPageCondition;
00209
00213 StrictMutex mutex;
00214
00215 typedef std::hash_map<BlockNum, BackupRestorePage *> PendingWriteMap;
00216
00217 typedef PendingWriteMap::iterator PendingWriteMapIter;
00218
00223 PendingWriteMap pendingWriteMap;
00224
00231 BlockNum currPageReadCount;
00232
00239 BlockNum currPageWriteCount;
00240
00244 boost::scoped_array<BackupRestorePage> backupRestorePages;
00245
00249 boost::scoped_ptr<FennelExcn> pPendingExcn;
00250
00254 void init();
00255
00265 void setCompressionProgramPathname(const std::string &programName);
00266
00275 void initScratchPages(
00276 SegPageLock &scratchLock,
00277 uint nScratchPages,
00278 uint nReservedPages,
00279 uint bufferSize);
00280
00287 BackupRestorePage *getFreeScratchPage();
00288
00297 void freeScratchPage(BackupRestorePage &scratchPage);
00298
00306 void writeBackupPage(PConstBuffer pageBuffer, bool scheduledWrite);
00307
00312 void checkPendingException();
00313
00314
00315 virtual void closeImpl();
00316
00317 public:
00336 SegPageBackupRestoreDevice(
00337 const std::string &backupFilePathname,
00338 const char *mode,
00339 const std::string &compressionProgram,
00340 uint nScratchPages,
00341 uint nReservedPages,
00342 SegmentAccessor &scratchAccessor,
00343 DeviceAccessScheduler &scheduler,
00344 SharedRandomAccessDevice pDataDevice);
00345
00366 static SharedSegPageBackupRestoreDevice newSegPageBackupRestoreDevice(
00367 const std::string &backupFilePathname,
00368 const char *mode,
00369 const std::string &compressionProgram,
00370 uint nScratchPages,
00371 uint nReservedPages,
00372 SegmentAccessor &scratchAccessor,
00373 DeviceAccessScheduler &scheduler,
00374 SharedRandomAccessDevice pDataDevice);
00375
00384 PBuffer getReservedBufferPage();
00385
00391 void writeBackupPage(PConstBuffer pageBuffer);
00392
00399 void backupPage(BlockId blockId);
00400
00408 void notifyReadTransferCompletion(
00409 BackupRestorePage &scratchPage,
00410 bool bSuccess);
00411
00418 void restorePage(BlockId blockId);
00419
00427 void notifyWriteTransferCompletion(
00428 BackupRestorePage &scratchPage,
00429 bool bSuccess);
00430
00434 void waitForPendingWrites();
00435
00440 StrictMutex &getMutex();
00441 };
00442
00443 FENNEL_END_NAMESPACE
00444
00445 #endif
00446
00447