public interface OffsetIndex extends AutoCloseable
The offset index handles indexing of entries in a given Segment. Given the offset and position of an entry
in a segment, the index will write the position to an underlying Buffer. With this information, the index provides
useful metadata about the log such as the number of physical entries in the log and the first and last offsets.
Each entry in the index is stored in 8 bytes, a 1 byte status flag, a 24-bit unsigned offset, and a 32-bit unsigned
position. This places a limitation on the maximum indexed offset at 2^31 - 1 and maximum indexed position at
2^32 - 1.
When the index is first created, the Buffer provided to the constructor will be scanned for existing entries.
The index assumes that entries will always be indexed in increasing order. However, this index also allows arbitrary
entries to be missing from the log due to log compaction. Because of the potential for missing entries, binary search
is used to locate positions rather than absolute positions. For efficiency, a MappedBuffer
can be used to improve the performance of the binary search algorithm for persistent indexes.
In order to prevent searching the index for missing entries, all offsets are added to a memory efficient BitArray
as they're written to the index. The bit array is sized according to the underlying index buffer. Prior to searching
for an offset in the index, the BitArray is checked for existence of the offset in the index. Only if the offset
exists in the index is a binary search required.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the index.
|
boolean |
contains(long offset)
Returns a boolean value indicating whether the index contains the given offset.
|
void |
delete()
Deletes the index.
|
long |
find(long offset)
Finds the real offset for the given relative offset.
|
void |
flush()
Flushes the index to the underlying storage.
|
boolean |
index(long offset,
long position)
Indexes the given offset with the given position.
|
boolean |
isEmpty()
Returns a boolean value indicating whether the index is empty.
|
long |
lastOffset()
Returns the last offset in the index.
|
long |
position(long offset)
Finds the starting position of the given offset.
|
int |
size()
Returns the number of entries active in the index.
|
static long |
size(int maxEntries)
Returns the count of the index for the given number of entries.
|
long |
truncate(long offset)
Truncates the index up to the given offset.
|
static long size(int maxEntries)
long lastOffset()
boolean index(long offset,
long position)
offset - The offset to index.position - The position of the offset to index.IllegalArgumentException - if the offset is less than or equal to the last offset in the index,
or position is greater than MAX_POSITIONboolean isEmpty()
int size()
boolean contains(long offset)
offset - The offset to check.long position(long offset)
offset - The offset to look up.long find(long offset)
long truncate(long offset)
This method assumes that the given offset is contained within the index. If the offset is not indexed then the index will not be truncated.
offset - The offset after which to truncate the index.void flush()
void close()
close in interface AutoCloseablevoid delete()
Copyright © 2013–2016. All rights reserved.