maedhroz commented on code in PR #2409:
URL: https://github.com/apache/cassandra/pull/2409#discussion_r1227305235


##########
src/java/org/apache/cassandra/index/sai/disk/v1/kdtree/BKDTreeRamBuffer.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.cassandra.index.sai.disk.v1.kdtree;
+
+import java.io.IOException;
+
+import com.google.common.base.Preconditions;
+
+import org.apache.lucene.codecs.MutablePointValues;
+import org.apache.lucene.util.Accountable;
+import org.apache.lucene.util.ByteBlockPool;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.Counter;
+import org.apache.lucene.util.packed.PackedInts;
+import org.apache.lucene.util.packed.PackedLongValues;
+
+/**
+ * On-heap buffer for point values that provides a sortable view of itself as 
{@link MutablePointValues}.
+ */
+public class BKDTreeRamBuffer implements Accountable
+{
+    private final Counter bytesUsed;
+    private final ByteBlockPool bytes;
+    private final int pointNumBytes;
+    private final int packedBytesLength;
+    private final byte[] packedValue;
+    private final PackedLongValues.Builder docIDsBuilder;
+    private int numPoints;
+    private int numRows;
+    private int lastSegmentRowID = -1;
+    private boolean closed = false;
+
+    public BKDTreeRamBuffer(int pointNumBytes)
+    {
+        this.bytesUsed = Counter.newCounter();
+        this.pointNumBytes = pointNumBytes;
+
+        this.bytes = new ByteBlockPool(new 
ByteBlockPool.DirectTrackingAllocator(bytesUsed));
+
+        packedValue = new byte[pointNumBytes];
+        packedBytesLength = pointNumBytes;
+
+        docIDsBuilder = 
PackedLongValues.deltaPackedBuilder(PackedInts.COMPACT);
+        bytesUsed.addAndGet(docIDsBuilder.ramBytesUsed());
+    }
+
+    @Override
+    public long ramBytesUsed()
+    {
+        return bytesUsed.get();
+    }
+
+    public int numRows()
+    {
+        return numRows;
+    }
+
+    public long addPackedValue(int segmentRowId, BytesRef value)
+    {
+        ensureOpen();
+        
+        if (value.length != packedBytesLength)
+        {
+            throw new IllegalArgumentException("The value has length=" + 
value.length + " but should be " + pointNumBytes);
+        }
+
+        long startingBytesUsed = bytesUsed.get();
+        long startingDocIDsBytesUsed = docIDsBuilder.ramBytesUsed();
+
+        docIDsBuilder.add(segmentRowId);
+        bytes.append(value);
+
+        if (segmentRowId != lastSegmentRowID)
+        {
+            numRows++;
+            lastSegmentRowID = segmentRowId;
+        }
+
+        numPoints++;
+
+        long docIDsAllocatedBytes = docIDsBuilder.ramBytesUsed() - 
startingDocIDsBytesUsed;
+        long endingBytesAllocated = bytesUsed.addAndGet(docIDsAllocatedBytes);
+        
+        return endingBytesAllocated - startingBytesUsed;
+    }
+
+    public MutableOneDimPointValues asPointValues()
+    {
+        ensureOpen();
+        // building packed longs is destructive
+        closed = true;
+        final PackedLongValues docIDs = docIDsBuilder.build();
+        return new MutableOneDimPointValues()
+        {
+            final int[] ords = new int[numPoints];

Review Comment:
   nit: Not a hot path, so could try to on-liner this...
   
   ```
   final int[] ords = IntStream.range(0, numPoints).toArray();
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to