com.faunos.skwish.sys.mgr
Class UnitDir.Seg

java.lang.Object
  extended by com.faunos.skwish.Segment
      extended by com.faunos.skwish.sys.BaseSegment
          extended by com.faunos.skwish.sys.mgr.UnitDir.Seg
All Implemented Interfaces:
Range
Enclosing class:
UnitDir

public class UnitDir.Seg
extends BaseSegment

A BaseSegment backed with files in the enclosing instance's directory.

Author:
Babak Farhang

Field Summary
 
Fields inherited from class com.faunos.skwish.sys.BaseSegment
entryFile, helper, index
 
Fields inherited from class com.faunos.skwish.Segment
validator
 
Constructor Summary
protected UnitDir.Seg(FileChannel offsetFile, FileChannel entryFile)
          Access protected constructor.
 
Method Summary
 void append(BaseSegment segment, int maxIdGap)
          Appends the given segment to this segment.
 void appendWithLock(UnitDir.Seg seg)
          Appends the given source segment while holding locks on both this, the destination, and source segments.
 boolean covers(UnitDir.Seg segment)
          Determines whether this instance covers the given segment.
 void delete(long id, int count, boolean purge)
          Checks that appendSourceLock, is not owned by another thread, and also that the instance is not purgeable, and then proceeds with the base delete implementation.
 Lock getAppendLock()
          Returns the lock held when this instance is either the source or destination of an append.
 Lock getAppendSourceLock()
          Returns the lock held when this instance is the source of an append.
 void getEntry(long id, ByteBuffer out, ByteBuffer workBuffer)
          Returns the contents of the entry with the specified id.
 long getEntrySize(long id, ByteBuffer workBuffer)
          Returns the size of the entry with the specified id.
 String getName()
           
 UnitDir getUnitDir()
          Returns the enclosing UnitDir instance that created this segment.
 boolean isPurgeable()
          Determines whether this instance is purgeable.
protected  SubFileChannel newSubFileChannelImpl(FileChannel entryFile, long start, long end, boolean update)
          Returns a SubFileChannel that updates the usage count.
 String toString()
          Returns a concatenation of the simple class name together with the base ID and entry count.
 
Methods inherited from class com.faunos.skwish.sys.BaseSegment
allocateWorkBuffer, append, close, commit, flush, generateNoise, getBaseId, getContentsSize, getEntryChannel, getEntryCount, getEntryInsertionChannel, getEntryPart, getEntryUpdateChannel, getNextId, getTotalSize, insertEntry, insertEntry, isAlive, isAutoCommit, isReadOnly, killNext, setAutoCommit, writeNewSegment
 
Methods inherited from class com.faunos.skwish.Segment
contains, delete, delete, getEntry, getEntrySize, getEntryStream, hi, isDeleted, isDeleted, killNext, lo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UnitDir.Seg

protected UnitDir.Seg(FileChannel offsetFile,
                      FileChannel entryFile)
               throws IOException
Access protected constructor.

Throws:
IOException
Method Detail

appendWithLock

public void appendWithLock(UnitDir.Seg seg)
                    throws IOException
Appends the given source segment while holding locks on both this, the destination, and source segments. This method assumes that the necessary locks are not already owned by another; if not, a SegmentStoreException is thrown. Therefore, the caller will typically already own the locks to be held before invoking this method. On return, the locks are released, and if successful (no exception raised), the argument seg instance's purgeable property is set to true.

This append implementation is designed to prevent a segment from being appended (as either source or destination) while it is concurrently involved in another append operation, and from concurrent deletes in the source of an append.

Locks held

Locks are acquired in the following order. First, the src instance's append lock is acquired, then this (destination) instance's append lock, and finally the src instance's append-source lock.

The locks held are categorized below (not in the order they are acquired).

Throws:
IOException
See Also:
isPurgeable(), getAppendSourceLock(), delete(long, int, boolean)

isPurgeable

public boolean isPurgeable()
Determines whether this instance is purgeable. Once a segment has been appended to another segment, it is considered purgeable.

Returns:
true, if and only if this instance has been appended to another UnitDir.Seg instance.

getAppendSourceLock

public Lock getAppendSourceLock()
Returns the lock held when this instance is the source of an append.

We acquire this lock when deleting entries from the segment: we don't want to be deleting from this segment while it is being appended to another segment.

See Also:
appendWithLock(com.faunos.skwish.sys.mgr.UnitDir.Seg), isPurgeable()

getAppendLock

public Lock getAppendLock()
Returns the lock held when this instance is either the source or destination of an append.

See Also:
appendWithLock(com.faunos.skwish.sys.mgr.UnitDir.Seg)

delete

public void delete(long id,
                   int count,
                   boolean purge)
            throws IOException
Checks that appendSourceLock, is not owned by another thread, and also that the instance is not purgeable, and then proceeds with the base delete implementation.

Overrides:
delete in class BaseSegment
Parameters:
id - the ID of the first entry to be deleted. Must be a valid ID in this segment [baseId, baseId + entryCount)
count - the number of entries to be deleted. Specifies a range of IDs to be deleted: [id, id + count). The range of IDs must be valid entries within this segment; o.w. an exception is raised.
purge - if true, the contents of the deleted entries are signaled to be purged (overwritten) immediately; o.w. the contents will be deleted on the next time the segment is compacted.
Throws:
IOException
See Also:
getAppendSourceLock()

append

public void append(BaseSegment segment,
                   int maxIdGap)
            throws IOException
Description copied from class: BaseSegment
Appends the given segment to this segment. The contents of entries marked "deleted" are not appended to this segment. The segment's base ID is expected to be close to, but ≥ the next ID of this segment. The maxIdGap parameter specifies minimum requirements for the proximity of IDs in the segment to be appended.

The append method is designed to support merging segments.

Implementation note:

This method changes the state of the underlying entry file. Specifically, on return from this method, the entry stream is positioned at the end of the file; the state of the source segment, together with that of its underlying streams, however, remains unchanged.

Overrides:
append in class BaseSegment
Throws:
IOException
See Also:
BaseSegment.append(BaseSegment)

getEntry

public void getEntry(long id,
                     ByteBuffer out,
                     ByteBuffer workBuffer)
              throws IOException
Description copied from class: Segment
Returns the contents of the entry with the specified id. If the entry is deleted, the method returns without modifying the out buffer.

Overrides:
getEntry in class BaseSegment
out - the buffer into which the contents are written. Its remaining bytes must be large enough; o.w., an exception is raised.
workBuffer - a work buffer used internally to read offsets. The minimum capacity of this work buffer must be 16
Throws:
IOException
See Also:
UnitDir.getUsageCount()

getEntrySize

public long getEntrySize(long id,
                         ByteBuffer workBuffer)
                  throws IOException
Description copied from class: Segment
Returns the size of the entry with the specified id. A work buffer with a minimum size of 16 must be passed in.

Overrides:
getEntrySize in class BaseSegment
Returns:
the size of the entry if present; -1, if deleted.
Throws:
IOException
See Also:
UnitDir.getUsageCount()

newSubFileChannelImpl

protected SubFileChannel newSubFileChannelImpl(FileChannel entryFile,
                                               long start,
                                               long end,
                                               boolean update)
                                        throws IOException
Returns a SubFileChannel that updates the usage count.

Overrides:
newSubFileChannelImpl in class BaseSegment
Throws:
IOException
See Also:
UnitDir.getUsageCount()

getUnitDir

public UnitDir getUnitDir()
Returns the enclosing UnitDir instance that created this segment.


covers

public boolean covers(UnitDir.Seg segment)
Determines whether this instance covers the given segment. This instance covers another if the range of IDs in the other segment is also contained in this segment, and if this segment's number of entries is greater than that of the other.

A better way to say this: a segment covers another segment if its set of entry IDs is a proper superset of that of the other.


getName

public String getName()

toString

public String toString()
Description copied from class: Segment
Returns a concatenation of the simple class name together with the base ID and entry count.

Overrides:
toString in class Segment


SourceForge.net Logo