Skip to content

Commit

Permalink
Encode deltas instead of ids
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Jan 16, 2025
1 parent cef5800 commit cb4ae0c
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ private int serializeDocIdSet(DocIdSet set, BytesStreamOutput os) {
}
os.writeByte(classByte);
int numDocs = 0;
int prevDoc = 0;

try {
DocIdSetIterator iterator = set.iterator();
Expand All @@ -919,7 +920,9 @@ private int serializeDocIdSet(DocIdSet set, BytesStreamOutput os) {
byte[] nextBlock = new byte[BLOCK_SIZE];
while (nextDoc != NO_MORE_DOCS) {
nextDoc = iterator.nextDoc();
blockPos = writeVInt(nextDoc, currentBlock, nextBlock, blockPos);
int delta = nextDoc - prevDoc;
prevDoc = nextDoc;
blockPos = writeVInt(delta, currentBlock, nextBlock, blockPos);
numDocs++;
if (blockPos >= BLOCK_SIZE) {
os.writeBytes(currentBlock);
Expand Down Expand Up @@ -1025,6 +1028,7 @@ private long readVInt(byte[] currentBlock, byte[] nextBlock, int pos) throws IOE
private int addDocs(BytesStreamInput is, Consumer<Integer> docAdder) {
// Returns bitSetLength which may or may not be needed.
int bitSetLength = 0;
int prevDoc = 0;
try {
byte[] currentBlock = new byte[BLOCK_SIZE];
byte[] nextBlock = new byte[BLOCK_SIZE];
Expand All @@ -1034,7 +1038,8 @@ private int addDocs(BytesStreamInput is, Consumer<Integer> docAdder) {
is.readBytes(nextBlock, 0, BLOCK_SIZE);

long packedVInt = readVInt(currentBlock, nextBlock, blockPos);
int nextDoc = (int) packedVInt; // Apparently equivalent to packedVInt & 0xffffffff
int delta = (int) packedVInt; // Apparently equivalent to packedVInt & 0xffffffff
int nextDoc = prevDoc + delta;
blockPos = (int) (packedVInt >> 32);
// If nextPos is into nextBlock, we need to fetch another block
if (blockPos >= BLOCK_SIZE) {
Expand All @@ -1046,10 +1051,12 @@ private int addDocs(BytesStreamInput is, Consumer<Integer> docAdder) {

while (nextDoc != NO_MORE_DOCS) {
docAdder.accept(nextDoc);
prevDoc = nextDoc;
bitSetLength++;

packedVInt = readVInt(currentBlock, nextBlock, blockPos);
nextDoc = (int) packedVInt; // Equivalent to packedVInt & 0xffffffff
delta = (int) packedVInt; // Equivalent to packedVInt & 0xffffffff
nextDoc = prevDoc + delta;
blockPos = (int) (packedVInt >> 32);
// If nextPos is into nextBlock, we need to fetch another block
if (blockPos >= BLOCK_SIZE) {
Expand Down

0 comments on commit cb4ae0c

Please sign in to comment.