-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use group-varint encoding for the tail of postings (#12782)
Co-authored-by: Adrien Grand <jpountz@gmail.com>
- Loading branch information
Showing
6 changed files
with
202 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
lucene/core/src/java/org/apache/lucene/codecs/lucene99/GroupVIntReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.lucene99; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.store.DataInput; | ||
|
||
/** Decode integers using group-varint. */ | ||
public class GroupVIntReader { | ||
|
||
public static void readValues(DataInput in, long[] docs, int limit) throws IOException { | ||
int i; | ||
for (i = 0; i <= limit - 4; i += 4) { | ||
final int flag = in.readByte() & 0xFF; | ||
|
||
final int n1Minus1 = flag >> 6; | ||
final int n2Minus1 = (flag >> 4) & 0x03; | ||
final int n3Minus1 = (flag >> 2) & 0x03; | ||
final int n4Minus1 = flag & 0x03; | ||
|
||
docs[i] = readLong(in, n1Minus1); | ||
docs[i + 1] = readLong(in, n2Minus1); | ||
docs[i + 2] = readLong(in, n3Minus1); | ||
docs[i + 3] = readLong(in, n4Minus1); | ||
} | ||
for (; i < limit; ++i) { | ||
docs[i] = in.readVInt(); | ||
} | ||
} | ||
|
||
private static long readLong(DataInput in, int numBytesMinus1) throws IOException { | ||
switch (numBytesMinus1) { | ||
case 0: | ||
return in.readByte() & 0xFFL; | ||
case 1: | ||
return in.readShort() & 0xFFFFL; | ||
case 2: | ||
return (in.readShort() & 0xFFFFL) | ((in.readByte() & 0xFFL) << 16); | ||
default: | ||
return in.readInt() & 0xFFFFFFFFL; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
lucene/core/src/java/org/apache/lucene/codecs/lucene99/GroupVIntWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.lucene99; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.store.DataOutput; | ||
|
||
/** | ||
* Encode integers using group-varint. It uses VInt to encode tail values that are not enough for a | ||
* group | ||
*/ | ||
public class GroupVIntWriter { | ||
|
||
// the maximum size of one group is 4 integers + 1 byte flag. | ||
private byte[] bytes = new byte[17]; | ||
private int byteOffset = 0; | ||
|
||
public GroupVIntWriter() {} | ||
|
||
private int encodeValue(int v) { | ||
int lastOff = byteOffset; | ||
do { | ||
bytes[byteOffset++] = (byte) (v & 0xFF); | ||
v >>>= 8; | ||
} while (v != 0); | ||
return byteOffset - lastOff; | ||
} | ||
|
||
public void writeValues(DataOutput out, long[] values, int limit) throws IOException { | ||
int off = 0; | ||
|
||
// encode each group | ||
while ((limit - off) >= 4) { | ||
byte flag = 0; | ||
byteOffset = 1; | ||
flag |= (encodeValue((int) values[off++]) - 1) << 6; | ||
flag |= (encodeValue((int) values[off++]) - 1) << 4; | ||
flag |= (encodeValue((int) values[off++]) - 1) << 2; | ||
flag |= (encodeValue((int) values[off++]) - 1); | ||
bytes[0] = flag; | ||
out.writeBytes(bytes, byteOffset); | ||
} | ||
|
||
// tail vints | ||
for (; off < limit; off++) { | ||
out.writeVInt((int) values[off]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestGroupVInt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.lucene99; | ||
|
||
import com.carrotsearch.randomizedtesting.generators.RandomNumbers; | ||
import java.io.IOException; | ||
import org.apache.lucene.store.ByteArrayDataInput; | ||
import org.apache.lucene.store.ByteArrayDataOutput; | ||
import org.apache.lucene.tests.util.LuceneTestCase; | ||
import org.apache.lucene.tests.util.TestUtil; | ||
import org.apache.lucene.util.ArrayUtil; | ||
import org.apache.lucene.util.packed.PackedInts; | ||
|
||
public class TestGroupVInt extends LuceneTestCase { | ||
|
||
public void testEncodeDecode() throws IOException { | ||
long[] values = new long[ForUtil.BLOCK_SIZE]; | ||
long[] restored = new long[ForUtil.BLOCK_SIZE]; | ||
final int iterations = atLeast(100); | ||
|
||
final GroupVIntWriter w = new GroupVIntWriter(); | ||
byte[] encoded = new byte[(int) (Integer.BYTES * ForUtil.BLOCK_SIZE * 1.25)]; | ||
|
||
for (int i = 0; i < iterations; i++) { | ||
final int bpv = TestUtil.nextInt(random(), 1, 31); | ||
final int numValues = TestUtil.nextInt(random(), 1, ForUtil.BLOCK_SIZE); | ||
|
||
// encode | ||
for (int j = 0; j < numValues; j++) { | ||
values[j] = RandomNumbers.randomIntBetween(random(), 0, (int) PackedInts.maxValue(bpv)); | ||
} | ||
w.writeValues(new ByteArrayDataOutput(encoded), values, numValues); | ||
|
||
// decode | ||
GroupVIntReader.readValues(new ByteArrayDataInput(encoded), restored, numValues); | ||
assertArrayEquals( | ||
ArrayUtil.copyOfSubArray(values, 0, numValues), | ||
ArrayUtil.copyOfSubArray(restored, 0, numValues)); | ||
} | ||
} | ||
} |