|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.faunos.skwish.Segment
com.faunos.skwish.sys.BaseSegment
public class BaseSegment
The base implementation that is backed by an offset (index) and a contents file.
| Field Summary | |
|---|---|
protected FileChannel |
entryFile
|
protected static ChannelUtil<SegmentException> |
helper
|
protected Index |
index
|
| Fields inherited from class com.faunos.skwish.Segment |
|---|
validator |
| Constructor Summary | |
|---|---|
BaseSegment(FileChannel offsetFile,
FileChannel entryFile)
Reads and returns a new instance using the given offset (index) file and associated entry [contents] file. |
|
BaseSegment(Index index,
FileChannel entryFile)
Creates a new instance. |
|
| Method Summary | |
|---|---|
protected ByteBuffer |
allocateWorkBuffer()
Returns a work buffer for the current thread of execution. |
void |
append(BaseSegment segment)
Appends the given segment to this segment. |
void |
append(BaseSegment segment,
int maxIdGap)
Appends the given segment to this segment. |
void |
close()
Closes this segment and its underlying streams. |
void |
commit()
Commits the entry count. |
void |
delete(long id,
int count,
boolean purge)
Deletes count entries starting at the entry with the specified id. |
void |
flush()
Flushes out the backing storage streams. |
protected long |
generateNoise()
Returns a numeric value used to overwrite entries that are to be purged. |
long |
getBaseId()
Returns the instance's base ID. |
long |
getContentsSize()
Returns an estimate of the byte size of the total entry contents contained in this segment. |
void |
getEntry(long id,
ByteBuffer out,
ByteBuffer workBuffer)
Returns the contents of the entry with the specified id. |
FileChannel |
getEntryChannel(long id)
Returns the contents of the entry with the given id
as a FileChannel, or null, if deleted. |
long |
getEntryCount()
Returns the number of entries in this instance. |
FileChannel |
getEntryInsertionChannel()
Return an output stream (FileChannel) for inserting the next entry. |
int |
getEntryPart(long id,
long position,
ByteBuffer out,
ByteBuffer workBuffer)
Returns the specified part of the entry contents with the specified id. |
long |
getEntrySize(long id,
ByteBuffer workBuffer)
Returns the size of the entry with the specified id. |
FileChannel |
getEntryUpdateChannel(long id)
|
long |
getNextId()
Returns the ID the entry will get on the next insertion. |
long |
getTotalSize()
Returns an estimate of the total byte size of this segment, accounting for the index overhead. |
long |
insertEntry(ByteBuffer entry)
Inserts the given entry and returns the entry's ID. |
long |
insertEntry(ReadableByteChannel entry)
Inserts the entry represented by the given channel. |
boolean |
isAlive()
Determines whether the instance is still alive. |
boolean |
isAutoCommit()
Determines whether the instance is in auto-commit mode. |
boolean |
isReadOnly()
Determines whether this instance is read-only (unmodifiable). |
long |
killNext(int count)
Kills the next count entry IDs. |
protected SubFileChannel |
newSubFileChannelImpl(FileChannel entryFile,
long start,
long end,
boolean update)
Returns the implementation-specific SubFileChannel. |
void |
setAutoCommit(boolean autoCommit)
Sets the auto-commit property. |
static BaseSegment |
writeNewSegment(FileChannel offsetFile,
FileChannel entryFile,
long baseId,
Word type)
Writes and returns a new segment to the specified offset (index) and entry [contents] files. |
| Methods inherited from class com.faunos.skwish.Segment |
|---|
contains, delete, delete, getEntry, getEntrySize, getEntryStream, hi, isDeleted, isDeleted, killNext, lo, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected final Index index
protected final FileChannel entryFile
protected static final ChannelUtil<SegmentException> helper
| Constructor Detail |
|---|
public BaseSegment(FileChannel offsetFile,
FileChannel entryFile)
throws IOException
offsetFile - the offset (index) file. Must be positioned
at the start of the index (which need not be
at the beginning of the file)entryFile - the associated file containing the entry contents.
The file position doesn't matter.
IOException
public BaseSegment(Index index,
FileChannel entryFile)
throws IOException
IOExceptionpseudo-constructor| Method Detail |
|---|
public static BaseSegment writeNewSegment(FileChannel offsetFile,
FileChannel entryFile,
long baseId,
Word type)
throws IOException
IOExceptionpublic boolean isReadOnly()
Segment
isReadOnly in class Segmentfalsepublic long getBaseId()
Segment
getBaseId in class SegmentSegment.getEntryCount()public long getEntryCount()
Segment
getEntryCount in class Segmentpublic long getNextId()
Segment
getNextId in class Segment
public long killNext(int count)
throws IOException
Segmentcount entry IDs. A killed entry is an
entry that begins (and ends) life in the deleted state.
killNext in class Segmentcount - the number of entries to kill (must be greater than 1)
IOExceptionpublic long getContentsSize()
getTotalSize()public long getTotalSize()
public long insertEntry(ByteBuffer entry)
throws IOException
Segment
insertEntry in class Segmententry - they contents of the entry
IOException
public long insertEntry(ReadableByteChannel entry)
throws IOException
Segmententry represented by the given channel.
The contents are of the entry is understood to be the entire
remaining contents in the specified channel.
This is the most efficient method for inserting a large-size entry
(even one that doesn't fit in memory).
insertEntry in class SegmentIOException
public FileChannel getEntryInsertionChannel()
throws IOException
IOException
public void delete(long id,
int count,
boolean purge)
throws IOException
Segment
delete in class Segmentid - 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.
IOExceptionprotected long generateNoise()
public long getEntrySize(long id,
ByteBuffer workBuffer)
throws IOException
Segment
getEntrySize in class SegmentIOException
public int getEntryPart(long id,
long position,
ByteBuffer out,
ByteBuffer workBuffer)
throws IOException
Segmentout.remaining() bytes of the contents of the entry
are copied into the given out buffer.
getEntryPart in class Segmentposition - the starting position into the entry from where data will be
copied into the out bufferout - the out buffer into which data is writtenworkBuffer - a work buffer with a minimum size of 16
out has
no remaining bytes), or -1, if the position is greater than the
size of the entry, or if the entry is deleted.
IOException
public void getEntry(long id,
ByteBuffer out,
ByteBuffer workBuffer)
throws IOException
Segmentout
buffer.
getEntry in class Segmentout - 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
IOException
public FileChannel getEntryChannel(long id)
throws IOException
Segmentid
as a FileChannel, or null, if deleted.
The returned channel is positioned at zero and is read-only.
getEntryChannel in class SegmentIOExceptionnewSubFileChannelImpl(FileChannel, long, long, boolean)
public FileChannel getEntryUpdateChannel(long id)
throws IOException
IOException
protected SubFileChannel newSubFileChannelImpl(FileChannel entryFile,
long start,
long end,
boolean update)
throws IOException
SubFileChannel.
This method provides a hook for subclasses.
IOExceptiongetEntryChannel(long)
public void close()
throws IOException
IOExceptionpublic boolean isAlive()
public void flush()
throws IOException
IOExceptionpublic boolean isAutoCommit()
commit() is called; deletions are committed right away.
By default, BaseSegments are created in auto-commit mode.
commit(),
setAutoCommit(boolean)public void setAutoCommit(boolean autoCommit)
public void commit()
throws IOException
IOExceptionisAutoCommit(),
setAutoCommit(boolean)
public void append(BaseSegment segment)
throws IOException
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. Specifically, the segment's base ID may be no smaller than
this segment's next ID, and no larger than next ID + 1024.
This method simply invokes append(segment, 1024).
IOExceptionappend(BaseSegment, int)
public void append(BaseSegment segment,
int maxIdGap)
throws IOException
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.
IOExceptionappend(BaseSegment)protected ByteBuffer allocateWorkBuffer()
append
method. The base class returns a newly allocated 1kb direct buffer.
A better implementation may involve perhaps the use of larger
thread-local buffer. Such refinements are deferred to subclasses.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||