jpountz commented on code in PR #12841:
URL: https://github.com/apache/lucene/pull/12841#discussion_r1415213374


##########
lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java:
##########
@@ -212,6 +213,46 @@ public long readLong() throws IOException {
     }
   }
 
+  @Override
+  public void readGroupVInts(long[] dst, int limit) throws IOException {
+    int i;
+    for (i = 0; i <= limit - 4; i += 4) {
+      readGroupVInt(dst, i);
+    }
+    for (; i < limit; ++i) {
+      dst[i] = readVInt();
+    }
+  }
+
+  private void readGroupVInt(long[] dst, int offset) throws IOException {
+    ByteBuffer block = blocks[blockIndex(pos)];
+    int blockOffset = blockOffset(pos);
+    if (block.limit() - blockOffset < GroupVIntUtil.MAX_LENGTH_PER_GROUP) {
+      GroupVIntUtil.fallbackReadGroupVInt(this, dst, offset);
+      return;
+    }
+
+    final int flag = readByte() & 0xFF;
+
+    final int n1Minus1 = flag >> 6;
+    final int n2Minus1 = (flag >> 4) & 0x03;
+    final int n3Minus1 = (flag >> 2) & 0x03;
+    final int n4Minus1 = flag & 0x03;
+
+    blockOffset = blockOffset(pos);

Review Comment:
   We already computed `blockOffset()` above, can we avoid computing it twice, 
e.g. by replacing the `readByte()` call with `block.get(blockOffset++)`?



##########
lucene/core/src/java/org/apache/lucene/store/DataOutput.java:
##########
@@ -29,6 +30,7 @@
  * internal state like file position).
  */
 public abstract class DataOutput {
+  private BytesRefBuilder groupVIntBytes = new BytesRefBuilder();

Review Comment:
   Can it be made final?



##########
lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java:
##########
@@ -1438,4 +1440,68 @@ public void testListAllIsSorted() throws IOException {
       assertArrayEquals(expected, actual);
     }
   }
+
+  public void testDataTypes() throws IOException {
+    final long[] values = new long[] {43, 12345, 123456, 1234567890};
+    try (Directory dir = getDirectory(createTempDir("testDataTypes"))) {
+      IndexOutput out = dir.createOutput("test", IOContext.DEFAULT);
+      out.writeByte((byte) 43);
+      out.writeShort((short) 12345);
+      out.writeInt(1234567890);
+      out.writeGroupVInts(values, 4);
+      out.writeLong(1234567890123456789L);
+      out.close();
+
+      long[] restored = new long[4];
+      IndexInput in = dir.openInput("test", IOContext.DEFAULT);
+      assertEquals(43, in.readByte());
+      assertEquals(12345, in.readShort());
+      assertEquals(1234567890, in.readInt());
+      in.readGroupVInts(restored, 4);
+      assertArrayEquals(values, restored);
+      assertEquals(1234567890123456789L, in.readLong());
+      in.close();
+    }
+  }
+
+  public void testGroupVInt() throws IOException {
+    try (Directory dir = getDirectory(createTempDir("testGroupVInt"))) {
+      // test fallbackReadGroupVInt
+      doTestGroupVInt(dir, 5, 1, 6, 8);
+
+      // test large data to covers multiple blocks in ByteBuffersDataInput

Review Comment:
   adding it explicitly since this is probably the most important case to cover
   
   ```suggestion
         // test large data to covers multiple blocks in ByteBuffersDataInput 
and MMapDirectory via TestMultiMMap
   ```



##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java:
##########
@@ -303,6 +304,48 @@ public byte readByte(long pos) throws IOException {
     }
   }
 
+  @Override
+  public void readGroupVInts(long[] dst, int limit) throws IOException {
+    int i;
+    for (i = 0; i <= limit - 4; i += 4) {
+      readGroupVInt(dst, i);
+    }
+    for (; i < limit; ++i) {
+      dst[i] = readVInt();
+    }
+  }
+
+  private void readGroupVInt(long[] dst, int offset) throws IOException {
+    if (curSegment.byteSize() - curPosition < 
GroupVIntUtil.MAX_LENGTH_PER_GROUP) {
+      GroupVIntUtil.fallbackReadGroupVInt(this, dst, offset);
+      return;
+    }
+
+    try {
+      final int flag = readByte() & 0xFF;

Review Comment:
   Since we already did the job of checking that we have enough bytes left, we 
don't need `readByte()` to do it for us?
   
   ```suggestion
         final int flag = curSegment.get(LAYOUT_BYTE, curPosition++) & 0xFF;
   ```



##########
lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java:
##########
@@ -303,6 +304,48 @@ public byte readByte(long pos) throws IOException {
     }
   }
 
+  @Override
+  public void readGroupVInts(long[] dst, int limit) throws IOException {
+    int i;
+    for (i = 0; i <= limit - 4; i += 4) {
+      readGroupVInt(dst, i);
+    }
+    for (; i < limit; ++i) {
+      dst[i] = readVInt();
+    }
+  }
+
+  private void readGroupVInt(long[] dst, int offset) throws IOException {
+    if (curSegment.byteSize() - curPosition < 
GroupVIntUtil.MAX_LENGTH_PER_GROUP) {
+      GroupVIntUtil.fallbackReadGroupVInt(this, dst, offset);
+      return;
+    }
+

Review Comment:
   Maybe copy `curSegment` to a local variable here, e.g. `MemorySegment 
curSegment = this.curSegment` so that the JVM doesn't have to worry about 
`curSegment` getting updated?



##########
lucene/core/src/java/org/apache/lucene/util/GroupVIntUtil.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import org.apache.lucene.store.DataInput;
+
+/**
+ * This class contains utility methods and constants for group varint
+ *
+ * @lucene.internal
+ */
+public final class GroupVIntUtil {
+  // the maximum length of a single group-varint is 4 integers + 1 byte flag.
+  public static final int MAX_LENGTH_PER_GROUP = 17;
+  public static final int[] GROUP_VINT_MASKS = new int[] {0xFF, 0xFFFF, 
0xFFFFFF, 0xFFFFFFFF};
+
+  /**
+   * Read single group varint. we need a long[] because this is what postings 
are using.
+   *
+   * @param dst the array to read ints into.
+   * @param offset the offset in the array to start storing ints.
+   */
+  public static void fallbackReadGroupVInt(DataInput in, long[] dst, int 
offset)

Review Comment:
   It doesn't need `fallback` as a prefix anymore now that it's in its own 
class. Let's document that this is a default implementation and that it is 
recommended to use `DataInput#readGroupVInts` directly rather than this method?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to