lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] iverase commented on a change in pull request #556: LUCENE-8673: Use radix sorting when merging dimensional points
Date Thu, 31 Jan 2019 14:23:16 GMT
iverase commented on a change in pull request #556: LUCENE-8673: Use radix sorting when merging
dimensional points
URL: https://github.com/apache/lucene-solr/pull/556#discussion_r252685840
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/util/bkd/BKDRadixSelector.java
 ##########
 @@ -0,0 +1,350 @@
+/*
+ * 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.bkd;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.FutureArrays;
+import org.apache.lucene.util.IntroSelector;
+
+/**
+ *
+ * Offline Radix selector for BKD tree.
+ *
+ *  @lucene.internal
+ * */
+public final class BKDRadixSelector {
+  //size of the histogram
+  private static final int HISTOGRAM_SIZE = 256;
+  // we store one histogram per recursion level
+  private final int[][] histogram;
+  //bytes we are sorting
+  private final int bytesPerDim;
+  //data size
+  private int packedByteLength;
+  //flag to when we are moving to sort on heap
+  private final int maxPointsSortedOffHeap;
+  //holder for partition points
+  private int[] partitionBucket;
+  //holder for partition bytes
+  private byte[] partitionBytes;
+  //re-usable on-heap selector
+  private HeapRadixSelector heapRadixSelector;
+  // scratch object to move bytes around
+  BytesRef bytesRef = new BytesRef();
+
+  /**
+   * Sole constructor.
+   */
+  public BKDRadixSelector(int numDim, int bytesPerDim, int maxPointsSortedOffHeap) {
+    this.bytesPerDim = bytesPerDim;
+    this.packedByteLength = numDim * bytesPerDim;
+    this.maxPointsSortedOffHeap = maxPointsSortedOffHeap;
+    this.partitionBucket = new int[bytesPerDim];
+    this.partitionBytes =  new byte[bytesPerDim];
+    this.histogram = new int[bytesPerDim][HISTOGRAM_SIZE];
+    this.bytesRef.length = numDim * bytesPerDim;
+    this.heapRadixSelector = new HeapRadixSelector(numDim, bytesPerDim);
+  }
+
+  /**
+   * Method to partition the input data. It returns the value of the dimension where
+   * the split happens.
+   */
+  public byte[] select(PointWriter data, PointWriter left, PointWriter right, int from, int
to, int middle, int dim) throws IOException {
+    checkArgs(from, to, middle);
+
+    //If we are on heap then we just sort on heap
+    if (data instanceof HeapPointWriter) {
+      return heapSort((HeapPointWriter) data, left, right, dim, middle, 0, 0);
+    }
+
+    //reset histogram
+    for (int i = 0; i < bytesPerDim; i++) {
+      Arrays.fill(histogram[i], 0);
+    }
+    //find common prefix, it does already set histogram values if needed
+    int commonPrefix = findCommonPrefix(data, from, to, dim);
+
+    //if all equals we just partition the data
+    if (commonPrefix == bytesPerDim) {
+      return partition(data, left, right, from, to, middle, dim, null, commonPrefix - 1,
middle);
+    }
+    //let's rock'n'roll
+    return buildHistogramAndPartition(data, left, right, from, to, middle, 0, commonPrefix,
dim,0, 0);
+  }
+
+  void checkArgs(int from, int to, int middle) {
+    if (middle < from) {
+      throw new IllegalArgumentException("middle must be >= from");
+    }
+    if (middle >= to) {
+      throw new IllegalArgumentException("middle must be < to");
+    }
+  }
+
+  private int findCommonPrefix(PointWriter data, int from, int to, int dim) throws IOException{
+    //find common prefix
+    byte[] commonPrefix = new byte[bytesPerDim];
+    int commonPrefixPosition = bytesPerDim;
+    try (PointReader reader = data.getReader(from, to)) {
+      reader.next();
+      byte[] packedValue = reader.packedValue();
+      System.arraycopy(packedValue, dim * bytesPerDim, commonPrefix, 0, bytesPerDim);
+      for (int i =from + 1; i< to; i++) {
+        reader.next();
+        packedValue = reader.packedValue();
+        int j = FutureArrays.mismatch(commonPrefix, 0, commonPrefixPosition, packedValue,
dim * bytesPerDim, dim * bytesPerDim + commonPrefixPosition);
+        if (j == 0) {
+          return 0;
+        } else if (j != -1) {
+          commonPrefixPosition =j;
+        }
+      }
+    }
+    //build histogram up to the common prefix
+    for (int i=0; i < commonPrefixPosition; i++) {
+      partitionBucket[i] = commonPrefix[i] & 0xff;
+      partitionBytes[i] = commonPrefix[i];
+      histogram[i][partitionBucket[i]] = to - from;
+    }
+    return commonPrefixPosition;
+  }
+
+  private byte[] buildHistogramAndPartition(PointWriter data, PointWriter left, PointWriter
right, int from, int to, int middle,
+                                            int iteration,  int commonPrefix, int dim, int
leftCount, int rightCount) throws IOException {
+    //build histogram at the commonPrefix byte
+    try (PointReader reader = data.getReader(from, to)) {
+      if (iteration == 0) {
+        // we specialise this case
+        reader.buildHistogram(dim * bytesPerDim + commonPrefix, histogram[commonPrefix]);
+      } else {
+        while (reader.next()) {
+          byte[] packedValue = reader.packedValue();
+          if (hasCommonPrefix(packedValue, dim, commonPrefix)) {
+            int bucket = packedValue[dim * bytesPerDim + commonPrefix] & 0xff;
+            histogram[commonPrefix][bucket]++;
+          }
+        }
+      }
+    }
+    //Count left points and record the partition point
+    for(int i = 0; i < HISTOGRAM_SIZE; i++) {
+      int size = histogram[commonPrefix][i];
+      if (leftCount + size > middle) {
+        partitionBucket[commonPrefix] = i;
+        partitionBytes[commonPrefix] = (byte) i;
+        break;
+      }
+      leftCount += size;
+    }
+    //Count right points
+    for(int i = partitionBucket[commonPrefix] + 1; i < HISTOGRAM_SIZE; i++) {
+      rightCount += histogram[commonPrefix][i];;
+    }
+
+    assert leftCount + rightCount + histogram[commonPrefix][partitionBucket[commonPrefix]]
== to - from;
+
+    if (commonPrefix == bytesPerDim - 1) {
+      // we are done, lets break data around. No need to sort on heap, maybe we need to sort
by docID?
+      return partition(data, left, right, from, to, middle, dim, null, commonPrefix, middle
- leftCount);
 
 Review comment:
   just want to point out here, we are not tie-breaking by docID

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Mime
View raw message