SegPageBackupRestoreDevice.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/segment/SegPageBackupRestoreDevice.h#6 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     // implement RandomAccessRequestBinding
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      * If non-empty string, the full path of the program to use to compress or
00163      * decompress the backup file
00164      */
00165     std::string compressionProgram;
00166 
00167     /*
00168      * The number of scratch pages to allocate for backing up and restoring
00169      * data pages
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     // Implement ClosableObject
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 // End SegPageBackupRestoreDevice.h

Generated on Mon Jun 22 04:00:20 2009 for Fennel by  doxygen 1.5.1