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_SegmentFactory_Included
00025 #define Fennel_SegmentFactory_Included
00026 
00027 #include "fennel/common/CompoundId.h"
00028 #include "fennel/common/ClosableObject.h"
00029 #include "fennel/common/ConfigMap.h"
00030 #include "fennel/synch/SynchObj.h"
00031 #include "fennel/segment/TracingSegment.h"
00032 #include "fennel/segment/SnapshotRandomAllocationSegment.h"
00033 #include "fennel/device/DeviceMode.h"
00034 
00035 #include <boost/dynamic_bitset.hpp>
00036 #include <boost/utility.hpp>
00037 #include <boost/enable_shared_from_this.hpp>
00038 
00039 FENNEL_BEGIN_NAMESPACE
00040 
00041 class LinearDeviceSegmentParams;
00042 class PseudoUuid;
00043 
00049 class FENNEL_SEGMENT_EXPORT SegmentFactory
00050     : public boost::noncopyable,
00051         public boost::enable_shared_from_this<SegmentFactory>
00052 {
00053     friend class TempSegDestructor;
00054 
00055     static ParamName paramTraceSegments;
00056 
00060     SharedTraceTarget pTraceTarget;
00061 
00062     boost::dynamic_bitset<> tempDeviceIdBitset;
00063 
00064     DeviceId firstTempDeviceId;
00065 
00066     StrictMutex mutex;
00067 
00068     ConfigMap configMap;
00069 
00070     DeviceId allocateTempDeviceId();
00071 
00072     void deallocateTempDeviceId(DeviceId);
00073 
00074     explicit SegmentFactory(
00075         ConfigMap const &configMap,
00076         SharedTraceTarget pTraceTarget);
00077 public:
00089     static SharedSegmentFactory newSegmentFactory(
00090         ConfigMap const &configMap,
00091         SharedTraceTarget pTraceTarget);
00092 
00093     virtual ~SegmentFactory();
00094 
00098     ConfigMap const &getConfigMap() const;
00099 
00109     SharedSegment newLinearDeviceSegment(
00110         SharedCache cache,
00111         LinearDeviceSegmentParams const ¶ms);
00112 
00128     SharedSegment newRandomAllocationSegment(
00129         SharedSegment delegateSegment,
00130         bool bFormat,
00131         bool deferInit = false);
00132 
00151     SharedSegment newVersionedRandomAllocationSegment(
00152         SharedSegment delegateSegment,
00153         SharedSegment pTempSegment,
00154         bool bFormat,
00155         bool deferInit = false);
00156 
00175     SharedSegment newSnapshotRandomAllocationSegment(
00176         SharedSegment delegateSegment,
00177         SharedSegment versionedSegment,
00178         TxnId snapshotCsn,
00179         bool readOnlyCommittedData = false);
00180 
00188     SharedSegment newDynamicDelegatingSegment(SharedSegment delegateSegment);
00189 
00199     SharedSegment newWALSegment(
00200         SharedSegment logSegment);
00201 
00212     SharedSegment newLinearViewSegment(
00213         SharedSegment delegateSegment,
00214         PageId firstPageId);
00215 
00234     SharedSegment newCircularSegment(
00235         SharedSegment delegateSegment,
00236         SharedCheckpointProvider pCheckpointProvider,
00237         PageId oldestPageId = NULL_PAGE_ID,
00238         PageId newestPageId = NULL_PAGE_ID);
00239 
00255     SharedSegment newVersionedSegment(
00256         SharedSegment dataSegment,
00257         SharedSegment logSegment,
00258         PseudoUuid const &onlineUuid,
00259         SegVersionNum versionNumber);
00260 
00272     SegmentAccessor newScratchSegment(
00273         SharedCache pCache,
00274         uint nPagesMax = MAXU);
00275 
00288     SharedSegment newTracingSegment(
00289         SharedSegment pSegment,
00290         std::string sourceName,
00291         bool qualifySourceName = true);
00292 
00306     SharedSegment newTempDeviceSegment(
00307         SharedCache pCache,
00308         DeviceMode deviceMode,
00309         std::string deviceFileName);
00310 
00335     template <class PDerivedSegment>
00336     static PDerivedSegment dynamicCast(SharedSegment pSegment)
00337     {
00338         PDerivedSegment pDerived = dynamic_cast<PDerivedSegment>(
00339             pSegment.get());
00340         if (pDerived) {
00341             return pDerived;
00342         }
00343         TracingSegment *pTracing = dynamic_cast<TracingSegment *>(
00344             pSegment.get());
00345         if (pTracing) {
00346             pDerived = dynamic_cast<PDerivedSegment>(
00347                 pTracing->getDelegateSegment().get());
00348         }
00349         return pDerived;
00350     }
00351 
00361     static SnapshotRandomAllocationSegment *getSnapshotSegment(
00362         SharedSegment pSegment);
00363 };
00364 
00365 class FENNEL_SEGMENT_EXPORT TempSegDestructor
00366     : public ClosableObjectDestructor
00367 {
00368     SharedSegmentFactory pSegmentFactory;
00369 
00370 public:
00371     explicit TempSegDestructor(SharedSegmentFactory);
00372     void operator()(Segment *pSegment);
00373 };
00374 
00375 FENNEL_END_NAMESPACE
00376 
00377 #endif
00378 
00379