SegmentFactory Class Reference

SegmentFactory is a factory class for creating instances of the Segment interface. More...

#include <SegmentFactory.h>

List of all members.

Public Member Functions

virtual ~SegmentFactory ()
ConfigMap const & getConfigMap () const
 
Returns:
ConfigMap parameterizing this factory

SharedSegment newLinearDeviceSegment (SharedCache cache, LinearDeviceSegmentParams const &params)
 Opens a LinearDeviceSegment.
SharedSegment newRandomAllocationSegment (SharedSegment delegateSegment, bool bFormat, bool deferInit=false)
 Opens a RandomAllocationSegment.
SharedSegment newVersionedRandomAllocationSegment (SharedSegment delegateSegment, SharedSegment pTempSegment, bool bFormat, bool deferInit=false)
 Opens a VersionedRandomAllocationSegment.
SharedSegment newSnapshotRandomAllocationSegment (SharedSegment delegateSegment, SharedSegment versionedSegment, TxnId snapshotCsn, bool readOnlyCommittedData=false)
 Opens a new SnapshotRandomAllocationSegment.
SharedSegment newDynamicDelegatingSegment (SharedSegment delegateSegment)
 Opens a new DynamicDelegatingSegment.
SharedSegment newWALSegment (SharedSegment logSegment)
 Opens a WALSegment.
SharedSegment newLinearViewSegment (SharedSegment delegateSegment, PageId firstPageId)
 Opens a LinearViewSegment.
SharedSegment newCircularSegment (SharedSegment delegateSegment, SharedCheckpointProvider pCheckpointProvider, PageId oldestPageId=NULL_PAGE_ID, PageId newestPageId=NULL_PAGE_ID)
 Opens a CircularSegment.
SharedSegment newVersionedSegment (SharedSegment dataSegment, SharedSegment logSegment, PseudoUuid const &onlineUuid, SegVersionNum versionNumber)
 Opens a VersionedSegment.
SegmentAccessor newScratchSegment (SharedCache pCache, uint nPagesMax=MAXU)
 Creates a ScratchSegment.
SharedSegment newTracingSegment (SharedSegment pSegment, std::string sourceName, bool qualifySourceName=true)
 If necessary, wraps a TracingSegment around another segment.
SharedSegment newTempDeviceSegment (SharedCache pCache, DeviceMode deviceMode, std::string deviceFileName)
 Creates a new temporary device paired with a LinearDeviceSegment.

Static Public Member Functions

static SharedSegmentFactory newSegmentFactory (ConfigMap const &configMap, SharedTraceTarget pTraceTarget)
 Constructs a new SegmentFactory.
template<class PDerivedSegment>
static PDerivedSegment dynamicCast (SharedSegment pSegment)
 Some implementations of the Segment interface extend the interface with implementation-specific features.
static SnapshotRandomAllocationSegmentgetSnapshotSegment (SharedSegment pSegment)
 Casts a shared segment to a SnapshotRandomAllocationSegment, if the segment is in fact a SnapshotRandomAllocationSegment.

Private Member Functions

DeviceId allocateTempDeviceId ()
void deallocateTempDeviceId (DeviceId)
 SegmentFactory (ConfigMap const &configMap, SharedTraceTarget pTraceTarget)

Private Attributes

SharedTraceTarget pTraceTarget
 Trace target if tracing enable.
boost::dynamic_bitset tempDeviceIdBitset
DeviceId firstTempDeviceId
StrictMutex mutex
ConfigMap configMap

Static Private Attributes

static ParamName paramTraceSegments

Friends

class TempSegDestructor


Detailed Description

SegmentFactory is a factory class for creating instances of the Segment interface.

See the design docs for more detail.

Definition at line 49 of file SegmentFactory.h.


Constructor & Destructor Documentation

SegmentFactory::SegmentFactory ( ConfigMap const &  configMap,
SharedTraceTarget  pTraceTarget 
) [explicit, private]

Definition at line 53 of file SegmentFactory.cpp.

References firstTempDeviceId, pTraceTarget, and tempDeviceIdBitset.

Referenced by newSegmentFactory().

00056     : configMap(configMapInit)
00057 {
00058     pTraceTarget = pTraceTargetInit;
00059 
00060     // TODO:  parameterize
00061     firstTempDeviceId = DeviceId(512);
00062     tempDeviceIdBitset.resize(512);
00063 }

SegmentFactory::~SegmentFactory (  )  [virtual]

Definition at line 65 of file SegmentFactory.cpp.

00066 {
00067 }


Member Function Documentation

DeviceId SegmentFactory::allocateTempDeviceId (  )  [private]

Definition at line 291 of file SegmentFactory.cpp.

References firstTempDeviceId, mutex, and tempDeviceIdBitset.

Referenced by newTempDeviceSegment().

00292 {
00293     StrictMutexGuard mutexGuard(mutex);
00294     // TODO:  submit fast find-clear-bit to boost
00295     for (uint i = 0; i < tempDeviceIdBitset.size(); ++i) {
00296         if (!tempDeviceIdBitset[i]) {
00297             tempDeviceIdBitset[i] = true;
00298             return firstTempDeviceId + i;
00299         }
00300     }
00301     permAssert(false);
00302 }

void SegmentFactory::deallocateTempDeviceId ( DeviceId   )  [private]

Definition at line 304 of file SegmentFactory.cpp.

References firstTempDeviceId, mutex, opaqueToInt(), and tempDeviceIdBitset.

00305 {
00306     StrictMutexGuard mutexGuard(mutex);
00307     uint i = opaqueToInt(deviceId - firstTempDeviceId);
00308     assert(tempDeviceIdBitset[i]);
00309     tempDeviceIdBitset[i] = false;
00310 }

SharedSegmentFactory SegmentFactory::newSegmentFactory ( ConfigMap const &  configMap,
SharedTraceTarget  pTraceTarget 
) [static]

Constructs a new SegmentFactory.

Parameters:
configMap ConfigMap defining properties to use when instantiating segments
pTraceTarget target for trace messages; if NULL, tracing is never performed (otherwise it depends on configMap)
Returns:
new factory

Definition at line 45 of file SegmentFactory.cpp.

References configMap, and SegmentFactory().

Referenced by Database::init(), SparseBitmapTest::openStorage(), and SegStorageTestBase::SegStorageTestBase().

00048 {
00049     return SharedSegmentFactory(
00050         new SegmentFactory(configMap,pTraceTarget));
00051 }

ConfigMap const & SegmentFactory::getConfigMap (  )  const

Returns:
ConfigMap parameterizing this factory

Definition at line 69 of file SegmentFactory.cpp.

References configMap.

00070 {
00071     return configMap;
00072 }

SharedSegment SegmentFactory::newLinearDeviceSegment ( SharedCache  cache,
LinearDeviceSegmentParams const &  params 
)

Opens a LinearDeviceSegment.

Parameters:
cache the cache to use for this segment
params initialization parameters
Returns:
new segment

Definition at line 74 of file SegmentFactory.cpp.

References newTracingSegment().

00077 {
00078     SharedSegment pSegment(
00079         new LinearDeviceSegment(cache,params),
00080         ClosableObjectDestructor());
00081     SharedSegment tracingSegment =
00082         newTracingSegment(pSegment,"LinearDeviceSegment");
00083     tracingSegment->initForUse();
00084     return tracingSegment;
00085 }

SharedSegment SegmentFactory::newRandomAllocationSegment ( SharedSegment  delegateSegment,
bool  bFormat,
bool  deferInit = false 
)

Opens a RandomAllocationSegment.

Parameters:
delegateSegment the underlying segment providing storage; this segment must return LINEAR_ALLOCATION from getAllocationOrder(), and should already be allocated to the desired size
bFormat if true, the RandomAllocationSegment is formatted as empty; otherwise, the existing formatting is read
deferInit if true, defer initialization on the segment; defaults to false
Returns:
new segment

Definition at line 87 of file SegmentFactory.cpp.

References newTracingSegment(), and NULL_PAGE_ID.

00091 {
00092     RandomAllocationSegment *pRandomSegment =
00093         new RandomAllocationSegment(delegateSegment);
00094     SharedSegment pSegment(pRandomSegment,ClosableObjectDestructor());
00095     SharedSegment tracingSegment =
00096         newTracingSegment(pSegment,"RandomAllocationSegment");
00097     // Format the segment through the tracing segment so the operation
00098     // is traced
00099     if (bFormat) {
00100         tracingSegment->deallocatePageRange(NULL_PAGE_ID,NULL_PAGE_ID);
00101     }
00102     if (!deferInit) {
00103         tracingSegment->initForUse();
00104     }
00105     return tracingSegment;
00106 }

SharedSegment SegmentFactory::newVersionedRandomAllocationSegment ( SharedSegment  delegateSegment,
SharedSegment  pTempSegment,
bool  bFormat,
bool  deferInit = false 
)

Opens a VersionedRandomAllocationSegment.

Parameters:
delegateSegment the underlying segment providing storage; this segment must return LINEAR_ALLOCATION from getAllocationOrder(), and should already be allocated to the desired size
pTempSegment the temporary segment used to store modifications to allocation node pages
bFormat if true, the VersionedRandomAllocationSegment is formatted as empty; otherwise, the existing formatting is read
deferInit if true, defer initialization on the segment; defaults to false
Returns:
new segment

Definition at line 108 of file SegmentFactory.cpp.

References newTracingSegment(), and NULL_PAGE_ID.

00113 {
00114     VersionedRandomAllocationSegment *pVersionedRandomSegment =
00115         new VersionedRandomAllocationSegment(delegateSegment, pTempSegment);
00116     SharedSegment pSegment(pVersionedRandomSegment, ClosableObjectDestructor());
00117     SharedSegment tracingSegment =
00118         newTracingSegment(pSegment, "VersionedRandomAllocationSegment");
00119     // Format the segment through the tracing segment so the operation
00120     // is traced
00121     if (bFormat) {
00122         tracingSegment->deallocatePageRange(NULL_PAGE_ID, NULL_PAGE_ID);
00123     }
00124     if (!deferInit) {
00125         tracingSegment->initForUse();
00126     }
00127     return tracingSegment;
00128 }

SharedSegment SegmentFactory::newSnapshotRandomAllocationSegment ( SharedSegment  delegateSegment,
SharedSegment  versionedSegment,
TxnId  snapshotCsn,
bool  readOnlyCommittedData = false 
)

Opens a new SnapshotRandomAllocationSegment.

Parameters:
delegateSegment the underlying segment providing storage; most likely, this is the same as the versionedSegment
versionedSegment the underlying segment that provides versioning of pages
snapshotCsn the commit sequence number associated with the segment that determines which pages to read
readOnlyCommittedData if true, read only committed data, even ignoring uncommitted data created by the current transaction; defaults to false
Returns:
new segment

Definition at line 130 of file SegmentFactory.cpp.

References newTracingSegment().

00135 {
00136     SnapshotRandomAllocationSegment *pSnapshotSegment =
00137         new SnapshotRandomAllocationSegment(
00138             delegateSegment,
00139             versionedSegment,
00140             snapshotCsn,
00141             readOnlyCommittedData);
00142     SharedSegment pSegment(pSnapshotSegment, ClosableObjectDestructor());
00143     SharedSegment tracingSegment =
00144         newTracingSegment(pSegment, "SnapshotRandomAllocationSegment");
00145     tracingSegment->initForUse();
00146     return tracingSegment;
00147 }

SharedSegment SegmentFactory::newDynamicDelegatingSegment ( SharedSegment  delegateSegment  ) 

Opens a new DynamicDelegatingSegment.

Parameters:
delegateSegment initial underlying delegating segment
Returns:
new segment

Definition at line 149 of file SegmentFactory.cpp.

References newTracingSegment().

00151 {
00152     DynamicDelegatingSegment *pDelegatingSegment =
00153         new DynamicDelegatingSegment(WeakSegment(delegateSegment));
00154     SharedSegment pSegment(pDelegatingSegment, ClosableObjectDestructor());
00155     SharedSegment tracingSegment =
00156         newTracingSegment(pSegment, "DynamicDelegatingSegment");
00157     tracingSegment->initForUse();
00158     return tracingSegment;
00159 }

SharedSegment SegmentFactory::newWALSegment ( SharedSegment  logSegment  ) 

Opens a WALSegment.

Parameters:
logSegment the Segment in which log pages are stored; this segment must guarantee at least ASCENDING_ALLOCATION (TBD: plus some other restrictions on the way in which page allocation is implemented)
Returns:
new segment

Definition at line 161 of file SegmentFactory.cpp.

References newTracingSegment().

00163 {
00164     SharedSegment pSegment(
00165         new WALSegment(logSegment),
00166         ClosableObjectDestructor());
00167     SharedSegment tracingSegment =
00168         newTracingSegment(pSegment,"WALSegment");
00169     tracingSegment->initForUse();
00170     return tracingSegment;
00171 }

SharedSegment SegmentFactory::newLinearViewSegment ( SharedSegment  delegateSegment,
PageId  firstPageId 
)

Opens a LinearViewSegment.

Parameters:
delegateSegment the underlying segment
firstPageId PageId of starting page in underlying segment, or NULL_PAGE_ID to create a new LinearViewSegment
Returns:
new segment

Definition at line 173 of file SegmentFactory.cpp.

References newTracingSegment().

00176 {
00177     SharedSegment pSegment(
00178         new LinearViewSegment(delegateSegment,firstPageId),
00179         ClosableObjectDestructor());
00180     SharedSegment tracingSegment =
00181         newTracingSegment(pSegment,"LinearViewSegment");
00182     tracingSegment->initForUse();
00183     return tracingSegment;
00184 }

SharedSegment SegmentFactory::newCircularSegment ( SharedSegment  delegateSegment,
SharedCheckpointProvider  pCheckpointProvider,
PageId  oldestPageId = NULL_PAGE_ID,
PageId  newestPageId = NULL_PAGE_ID 
)

Opens a CircularSegment.

Parameters:
delegateSegment the underlying segment
pCheckpointProvider the CheckpointProvider to call when segment space is getting low; if this is singular, the caller must take care of checkpointing to prevent space from running out
oldestPageId restored oldest PageId for recovery, or NULL_PAGE_ID for empty segment
newestPageId restored newest PageId for recovery, or NULL_PAGE_ID for empty segment or unknown
Returns:
new segment

Definition at line 215 of file SegmentFactory.cpp.

References newTracingSegment().

00220 {
00221     SharedSegment pSegment(
00222         new CircularSegment(
00223             delegateSegment,
00224             pCheckpointProvider,
00225             oldestPageId,newestPageId),
00226         ClosableObjectDestructor());
00227     SharedSegment tracingSegment =
00228         newTracingSegment(pSegment,"CircularSegment");
00229     tracingSegment->initForUse();
00230     return tracingSegment;
00231 }

SharedSegment SegmentFactory::newVersionedSegment ( SharedSegment  dataSegment,
SharedSegment  logSegment,
PseudoUuid const &  onlineUuid,
SegVersionNum  versionNumber 
)

Opens a VersionedSegment.

Parameters:
dataSegment the segment storing the latest page versions
logSegment the log segment used for storing old page versions; an assertion violation will result if this is not an instance of WALSegment
versionNumber TODO: doc
onlineUuid TODO: doc
Returns:
new segment

Definition at line 186 of file SegmentFactory.cpp.

References newTracingSegment().

00191 {
00192     SharedSegment pSegment(
00193         new VersionedSegment(dataSegment,logSegment,onlineUuid,versionNumber),
00194         ClosableObjectDestructor());
00195     SharedSegment tracingSegment =
00196         newTracingSegment(pSegment,"VersionedSegment");
00197     tracingSegment->initForUse();
00198     return tracingSegment;
00199 }

SegmentAccessor SegmentFactory::newScratchSegment ( SharedCache  pCache,
uint  nPagesMax = MAXU 
)

Creates a ScratchSegment.

Parameters:
pCache cache from which to allocate scratch pages
nPagesMax maximum number of scratch pages to allocate from cache, or MAXU for unlimited
Returns:
SegmentAccessor for returned segment; all access must be through this SegmentAccessor's pCacheAccessor

Definition at line 201 of file SegmentFactory.cpp.

References newTracingSegment(), SegmentAccessor::pCacheAccessor, and SegmentAccessor::pSegment.

00204 {
00205     boost::shared_ptr<ScratchSegment> pSegment(
00206         new ScratchSegment(pCache,nPagesMax),
00207         ClosableObjectDestructor());
00208     SegmentAccessor segmentAccessor;
00209     segmentAccessor.pSegment = newTracingSegment(pSegment,"ScratchSegment");
00210     segmentAccessor.pSegment->initForUse();
00211     segmentAccessor.pCacheAccessor = pSegment;
00212     return segmentAccessor;
00213 }

SharedSegment SegmentFactory::newTracingSegment ( SharedSegment  pSegment,
std::string  sourceName,
bool  qualifySourceName = true 
)

If necessary, wraps a TracingSegment around another segment.

Parameters:
pSegment the underlying segment
sourceName the trace source name for this segment
qualifySourceName if true, pSegment's pointer address is appended to sourceName to make it unique
Returns:
the wrapped segment

Definition at line 233 of file SegmentFactory.cpp.

References pTraceTarget, and TRACE_FINE.

Referenced by newCircularSegment(), newDynamicDelegatingSegment(), newLinearDeviceSegment(), newLinearViewSegment(), newRandomAllocationSegment(), newScratchSegment(), newSnapshotRandomAllocationSegment(), newTempDeviceSegment(), newVersionedRandomAllocationSegment(), newVersionedSegment(), and newWALSegment().

00237 {
00238     if (!pTraceTarget.get()) {
00239         return pSegment;
00240     }
00241     if (qualifySourceName) {
00242         std::ostringstream oss;
00243         oss << "segment." << sourceName << "." << pSegment.get();
00244         sourceName = oss.str();
00245     }
00246     if (pTraceTarget->getSourceTraceLevel(sourceName) > TRACE_FINE) {
00247         // all segment tracing is TRACE_FINE or lower, so don't bother
00248         return pSegment;
00249     }
00250     SharedSegment pTracingSegment(
00251         new TracingSegment(pSegment,pTraceTarget,sourceName),
00252         ClosableObjectDestructor());
00253 
00254     pSegment->setTracingSegment(WeakSegment(pTracingSegment));
00255 
00256     return pTracingSegment;
00257 }

SharedSegment SegmentFactory::newTempDeviceSegment ( SharedCache  pCache,
DeviceMode  deviceMode,
std::string  deviceFileName 
)

Creates a new temporary device paired with a LinearDeviceSegment.

The device will be automatically deleted when the segment is destroyed.

Parameters:
pCache the cache to use for this segment
deviceMode mode in which to open the device
deviceFileName filename to use for device; if relative, location is dependent on SegmentFactory configuration parameters
Returns:
new segment

Definition at line 260 of file SegmentFactory.cpp.

References allocateTempDeviceId(), DeviceMode::create, LinearDeviceSegmentParams::firstBlockId, MAXU, newTracingSegment(), LinearDeviceSegmentParams::nPagesAllocated, FileSystem::remove(), CompoundId::setBlockNum(), CompoundId::setDeviceId(), and TempSegDestructor.

00264 {
00265     // TODO:  guard to automatically deallocateTempDeviceId on failure?
00266     DeviceId deviceId = allocateTempDeviceId();
00267     if (deviceMode.create) {
00268         FileSystem::remove(deviceFileName.c_str());
00269     }
00270     // TODO: depending on config params?
00271     // deviceMode.temporary = true;
00272     SharedRandomAccessDevice pDevice(
00273         new RandomAccessFileDevice(deviceFileName,deviceMode));
00274     pCache->registerDevice(deviceId,pDevice);
00275     LinearDeviceSegmentParams deviceParams;
00276     CompoundId::setDeviceId(deviceParams.firstBlockId,deviceId);
00277     CompoundId::setBlockNum(deviceParams.firstBlockId,0);
00278     deviceParams.nPagesAllocated = 0;
00279     if (!deviceMode.create) {
00280         deviceParams.nPagesAllocated = MAXU;
00281     }
00282     SharedSegment pSegment(
00283         new LinearDeviceSegment(pCache,deviceParams),
00284         TempSegDestructor(shared_from_this()));
00285     SharedSegment tracingSegment =
00286         newTracingSegment(pSegment,"TempLinearDeviceSegment");
00287     tracingSegment->initForUse();
00288     return tracingSegment;
00289 }

template<class PDerivedSegment>
static PDerivedSegment SegmentFactory::dynamicCast ( SharedSegment  pSegment  )  [inline, static]

Some implementations of the Segment interface extend the interface with implementation-specific features.

dynamicCast provides access to a derived interface from the generic SharedSegment. It is the caller's responsibility to ensure that the returned (non-shared) pointer remains protected by the original shared_ptr. All generic access to the segment should still go through the original SharedSegment for proper tracing.

Example usage given a SharedSegment pSegment:

VersionedSegment *pVersionedSegment = SegmentFactory::dynamicCast<VersionedSegment *>(pSegment);

Parameters:
pSegment the SharedSegment to downcast
Returns:
result of the downcast as a reference, or NULL if pSegment was singular or of a different type

Definition at line 336 of file SegmentFactory.h.

References DelegatingSegment::getDelegateSegment().

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     }

SnapshotRandomAllocationSegment * SegmentFactory::getSnapshotSegment ( SharedSegment  pSegment  )  [static]

Casts a shared segment to a SnapshotRandomAllocationSegment, if the segment is in fact a SnapshotRandomAllocationSegment.

It may be necessary to extract the SnapshotRandomAllocationSegment from a DynamicDelegatingSegment.

Returns:
the underlying SnapshotRandomAllocationSegment or NULL if the segment is not a SnapshotRandomAllocationSegment

Definition at line 330 of file SegmentFactory.cpp.

References DynamicDelegatingSegment::getDelegateSegment().

Referenced by LcsClusterReplaceExecStream::open(), and LbmSplicerExecStream::open().

00332 {
00333     SnapshotRandomAllocationSegment *pSnapshotSegment =
00334         SegmentFactory::dynamicCast<SnapshotRandomAllocationSegment *>(
00335             pSegment);
00336     if (pSnapshotSegment == NULL) {
00337         DynamicDelegatingSegment *pDynamicSegment =
00338             SegmentFactory::dynamicCast<DynamicDelegatingSegment *>(
00339                 pSegment);
00340         if (pDynamicSegment != NULL) {
00341             pSnapshotSegment =
00342                 SegmentFactory::dynamicCast<SnapshotRandomAllocationSegment *>(
00343                     pDynamicSegment->getDelegateSegment());
00344         }
00345     }
00346     return pSnapshotSegment;
00347 }


Friends And Related Function Documentation

friend class TempSegDestructor [friend]

Definition at line 53 of file SegmentFactory.h.

Referenced by newTempDeviceSegment().


Member Data Documentation

ParamName SegmentFactory::paramTraceSegments [static, private]

Definition at line 55 of file SegmentFactory.h.

SharedTraceTarget SegmentFactory::pTraceTarget [private]

Trace target if tracing enable.

Definition at line 60 of file SegmentFactory.h.

Referenced by newTracingSegment(), and SegmentFactory().

boost::dynamic_bitset SegmentFactory::tempDeviceIdBitset [private]

Definition at line 62 of file SegmentFactory.h.

Referenced by allocateTempDeviceId(), deallocateTempDeviceId(), and SegmentFactory().

DeviceId SegmentFactory::firstTempDeviceId [private]

Definition at line 64 of file SegmentFactory.h.

Referenced by allocateTempDeviceId(), deallocateTempDeviceId(), and SegmentFactory().

StrictMutex SegmentFactory::mutex [private]

Definition at line 66 of file SegmentFactory.h.

Referenced by allocateTempDeviceId(), and deallocateTempDeviceId().

ConfigMap SegmentFactory::configMap [private]

Definition at line 68 of file SegmentFactory.h.

Referenced by getConfigMap(), and newSegmentFactory().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:45 2009 for Fennel by  doxygen 1.5.1