-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Feature/move long array group varint to backward codecs 15113 #15186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SAKSHIC
wants to merge
8
commits into
apache:main
Choose a base branch
from
SAKSHIC:feature/move-long-array-group-varint-to-backward-codecs-15113
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2e36e9
Move long[] group varint methods to backward-codecs (#15113)
sakshichourasia4 8ccea04
Add CHANGES.txt entry for #15113
sakshichourasia4 1dfefc4
Fix compilation issues and complete GroupVInt migration
sakshichourasia4 8b40649
Update CHANGES.txt
sakshichourasia4 ce65a9a
Remove duplicate TestGroupVIntUtil.java from incorrect location
sakshichourasia4 bf4a709
Fix Google Java Format violations
sakshichourasia4 2dfaefc
Add missing export for org.apache.lucene.backward_codecs.util package
sakshichourasia4 9c91faf
Add missing Javadoc for backward-codecs util package
sakshichourasia4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/DataOutputUtil.java
This file contains hidden or 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,45 @@ | ||
/* | ||
* 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.backward_codecs.store; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.backward_codecs.util.GroupVIntUtil; | ||
import org.apache.lucene.store.DataOutput; | ||
|
||
/** | ||
* Utility methods for DataOutput operations that are only used by backward codecs. | ||
* | ||
* @lucene.internal | ||
*/ | ||
public final class DataOutputUtil { | ||
|
||
private DataOutputUtil() {} // no instance | ||
|
||
/** | ||
* Encode integers using group-varint. It uses {@link DataOutput#writeVInt VInt} to encode tail | ||
* values that are not enough for a group. we need a long[] because this is what postings are | ||
* using, all longs are actually required to be integers. | ||
* | ||
* @param values the values to write | ||
* @param limit the number of values to write. | ||
* @lucene.experimental | ||
*/ | ||
public static void writeGroupVInts(DataOutput out, long[] values, int limit) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move that to |
||
byte[] groupVIntBytes = new byte[GroupVIntUtil.MAX_LENGTH_PER_GROUP]; | ||
GroupVIntUtil.writeGroupVInts(out, groupVIntBytes, values, limit); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/util/GroupVIntUtil.java
This file contains hidden or 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,135 @@ | ||
/* | ||
* 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.backward_codecs.util; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe move this class next to store package (to not open another package). |
||
|
||
import java.io.IOException; | ||
import org.apache.lucene.store.DataInput; | ||
import org.apache.lucene.store.DataOutput; | ||
import org.apache.lucene.util.BitUtil; | ||
|
||
/** | ||
* This class contains utility methods for group varint encoding/decoding of long[] arrays. These | ||
* methods are only used by backward codecs and have been moved here from the main GroupVIntUtil | ||
* class. | ||
* | ||
* @lucene.internal | ||
*/ | ||
public final class GroupVIntUtil { | ||
/** The maximum length of a single group-varint is 1 byte flag and 4 integers. */ | ||
public static final int MAX_LENGTH_PER_GROUP = Byte.BYTES + 4 * Integer.BYTES; | ||
|
||
private GroupVIntUtil() {} // no instance | ||
|
||
/** | ||
* Read all the group varints, including the tail vints to a long[]. | ||
* | ||
* @param dst the array to read ints into. | ||
* @param limit the number of int values to read. | ||
* @lucene.experimental | ||
*/ | ||
public static void readGroupVInts(DataInput in, long[] dst, int limit) throws IOException { | ||
int i; | ||
for (i = 0; i <= limit - 4; i += 4) { | ||
readGroupVInt(in, dst, i); | ||
} | ||
for (; i < limit; ++i) { | ||
dst[i] = in.readVInt() & 0xFFFFFFFFL; | ||
} | ||
} | ||
|
||
/** | ||
* Default implementation of read single group, for optimal performance, you should use {@link | ||
* GroupVIntUtil#readGroupVInts(DataInput, long[], int)} instead. | ||
* | ||
* @param in the input to use to read data. | ||
* @param dst the array to read ints into. | ||
* @param offset the offset in the array to start storing ints. | ||
*/ | ||
public static void readGroupVInt(DataInput in, long[] dst, int offset) throws IOException { | ||
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; | ||
|
||
dst[offset] = readIntInGroup(in, n1Minus1) & 0xFFFFFFFFL; | ||
dst[offset + 1] = readIntInGroup(in, n2Minus1) & 0xFFFFFFFFL; | ||
dst[offset + 2] = readIntInGroup(in, n3Minus1) & 0xFFFFFFFFL; | ||
dst[offset + 3] = readIntInGroup(in, n4Minus1) & 0xFFFFFFFFL; | ||
} | ||
|
||
private static int readIntInGroup(DataInput in, int numBytesMinus1) throws IOException { | ||
switch (numBytesMinus1) { | ||
case 0: | ||
return in.readByte() & 0xFF; | ||
case 1: | ||
return in.readShort() & 0xFFFF; | ||
case 2: | ||
return (in.readShort() & 0xFFFF) | ((in.readByte() & 0xFF) << 16); | ||
default: | ||
return in.readInt(); | ||
} | ||
} | ||
|
||
private static int numBytes(int v) { | ||
// | 1 to return 1 when v = 0 | ||
return Integer.BYTES - (Integer.numberOfLeadingZeros(v | 1) >> 3); | ||
} | ||
|
||
private static int toInt(long value) { | ||
if ((Long.compareUnsigned(value, 0xFFFFFFFFL) > 0)) { | ||
throw new ArithmeticException("integer overflow"); | ||
} | ||
return (int) value; | ||
} | ||
|
||
/** | ||
* The implementation for group-varint encoding, It uses a maximum of {@link | ||
* #MAX_LENGTH_PER_GROUP} bytes scratch buffer. | ||
*/ | ||
public static void writeGroupVInts(DataOutput out, byte[] scratch, long[] values, int limit) | ||
throws IOException { | ||
int readPos = 0; | ||
|
||
// encode each group | ||
while ((limit - readPos) >= 4) { | ||
int writePos = 0; | ||
final int n1Minus1 = numBytes(toInt(values[readPos])) - 1; | ||
final int n2Minus1 = numBytes(toInt(values[readPos + 1])) - 1; | ||
final int n3Minus1 = numBytes(toInt(values[readPos + 2])) - 1; | ||
final int n4Minus1 = numBytes(toInt(values[readPos + 3])) - 1; | ||
int flag = (n1Minus1 << 6) | (n2Minus1 << 4) | (n3Minus1 << 2) | (n4Minus1); | ||
scratch[writePos++] = (byte) flag; | ||
BitUtil.VH_LE_INT.set(scratch, writePos, (int) (values[readPos++])); | ||
writePos += n1Minus1 + 1; | ||
BitUtil.VH_LE_INT.set(scratch, writePos, (int) (values[readPos++])); | ||
writePos += n2Minus1 + 1; | ||
BitUtil.VH_LE_INT.set(scratch, writePos, (int) (values[readPos++])); | ||
writePos += n3Minus1 + 1; | ||
BitUtil.VH_LE_INT.set(scratch, writePos, (int) (values[readPos++])); | ||
writePos += n4Minus1 + 1; | ||
|
||
out.writeBytes(scratch, writePos); | ||
} | ||
|
||
// tail vints | ||
for (; readPos < limit; readPos++) { | ||
out.writeVInt(toInt(values[readPos])); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/util/package-info.java
This file contains hidden or 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,19 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** Utility classes for backward compatibility codecs. */ | ||
package org.apache.lucene.backward_codecs.util; |
55 changes: 55 additions & 0 deletions
55
...ckward-codecs/src/test/java/org/apache/lucene/backward_codecs/util/TestGroupVIntUtil.java
This file contains hidden or 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.backward_codecs.util; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.backward_codecs.store.DataOutputUtil; | ||
import org.apache.lucene.store.ByteBuffersDataInput; | ||
import org.apache.lucene.store.ByteBuffersDataOutput; | ||
import org.apache.lucene.tests.util.LuceneTestCase; | ||
|
||
public class TestGroupVIntUtil extends LuceneTestCase { | ||
|
||
public void testLongArrayRoundTrip() throws IOException { | ||
long[] original = {1L, 127L, 128L, 16383L, 16384L, 2097151L, 2097152L, 268435455L}; | ||
|
||
// Write using the backward-codecs utility | ||
ByteBuffersDataOutput out = new ByteBuffersDataOutput(); | ||
DataOutputUtil.writeGroupVInts(out, original, original.length); | ||
|
||
// Read back using the backward-codecs utility | ||
ByteBuffersDataInput in = out.toDataInput(); | ||
long[] result = new long[original.length]; | ||
GroupVIntUtil.readGroupVInts(in, result, original.length); | ||
|
||
assertArrayEquals(original, result); | ||
} | ||
|
||
public void testSingleGroupVInt() throws IOException { | ||
long[] original = {1L, 2L, 3L, 4L}; | ||
|
||
ByteBuffersDataOutput out = new ByteBuffersDataOutput(); | ||
byte[] scratch = new byte[GroupVIntUtil.MAX_LENGTH_PER_GROUP]; | ||
GroupVIntUtil.writeGroupVInts(out, scratch, original, original.length); | ||
|
||
ByteBuffersDataInput in = out.toDataInput(); | ||
long[] result = new long[original.length]; | ||
GroupVIntUtil.readGroupVInt(in, result, 0); | ||
|
||
assertArrayEquals(original, result); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see below: move the classes to another package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in addition, we do not need to open the package as it shozuld not be used from outside backwards_codecs!