Class PrefixIndex
The sorted identifiers are sliced into buckets of a target size K; each bucket records
its first entry index and its first identifier as a separator. A lookup first finds the
candidate bucket with a floor search over the small, cache-resident separator array, then binary
searches only that bucket's entry range. All comparisons are unsigned byte-wise
(UnsignedBytes), matching the order the dataset is sorted in.
The index is built in the exact shape it is serialized: a startIndex table and a
separatorOffset table of bucketCount + 1 entries each, plus one contiguous
separatorData block. An empty identifier list yields a single bucket with an empty
separator, so build, serialization, and search stay uniform — the zero-identifier short-circuit
is then only an optimization, not a correctness requirement.
Callers must pass identifiers already sorted by unsigned bytes and deduplicated; this class does not verify either.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe generator's launch default for the bucket target sizeK. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of buckets, always at least 1.intbucketEnd(int bucket) Returns the entry index just past the last entry of the given bucket.intbucketStart(int bucket) Returns the first entry index of the given bucket.static PrefixIndexBuilds an index oversortedIdentifiersusingDEFAULT_BUCKET_SIZE.static PrefixIndexBuilds an index oversortedIdentifierswith the given bucket target size.intReturns the number of identifiers indexed.intselectBucket(byte[] query) Selects the candidate bucket forquery: the greatest bucket index whose separator is<= queryby unsigned byte order, or 0 whenqueryprecedes every separator.byte[]Returns a copy of the contiguousseparatorDatablock: every bucket's separator bytes stored back to back in bucket order.int[]Returns a copy of theseparatorOffsettable:bucketCount + 1byte offsets intoseparatorData(), one per separator plus a trailing total length.int[]Returns a copy of thestartIndextable:bucketCount + 1entries, the last equal toidentifierCount().
-
Field Details
-
DEFAULT_BUCKET_SIZE
public static final int DEFAULT_BUCKET_SIZEThe generator's launch default for the bucket target sizeK. The runtime reads the actualKof a dataset from its file header, never from this constant.- See Also:
-
-
Method Details
-
build
Builds an index oversortedIdentifiersusingDEFAULT_BUCKET_SIZE.- Parameters:
sortedIdentifiers- the identifiers, already sorted by unsigned bytes and deduplicated- Returns:
- the index
-
build
Builds an index oversortedIdentifierswith the given bucket target size.- Parameters:
sortedIdentifiers- the identifiers, already sorted by unsigned bytes and deduplicatedbucketSize- the target number of entries per bucket (K); must be at least 1- Returns:
- the index
- Throws:
IllegalArgumentException- ifbucketSizeis less than 1
-
identifierCount
public int identifierCount()Returns the number of identifiers indexed.- Returns:
- the identifier count
-
bucketCount
public int bucketCount()Returns the number of buckets, always at least 1.- Returns:
- the bucket count
-
selectBucket
public int selectBucket(byte[] query) Selects the candidate bucket forquery: the greatest bucket index whose separator is<= queryby unsigned byte order, or 0 whenqueryprecedes every separator. This is a floor search, not an exact match.- Parameters:
query- the lookup key, UTF-8 encoded- Returns:
- a bucket index in
[0, bucketCount)
-
bucketStart
public int bucketStart(int bucket) Returns the first entry index of the given bucket.- Parameters:
bucket- a bucket index in[0, bucketCount)- Returns:
- the entry index where
bucketbegins
-
bucketEnd
public int bucketEnd(int bucket) Returns the entry index just past the last entry of the given bucket.- Parameters:
bucket- a bucket index in[0, bucketCount)- Returns:
- the entry index where
bucketends, exclusive
-
startIndex
public int[] startIndex()Returns a copy of thestartIndextable:bucketCount + 1entries, the last equal toidentifierCount().- Returns:
- the start-index table
-
separatorOffset
public int[] separatorOffset()Returns a copy of theseparatorOffsettable:bucketCount + 1byte offsets intoseparatorData(), one per separator plus a trailing total length.- Returns:
- the separator-offset table
-
separatorData
public byte[] separatorData()Returns a copy of the contiguousseparatorDatablock: every bucket's separator bytes stored back to back in bucket order.- Returns:
- the separator-data block
-