You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2015/10/07 11:23:53 UTC

svn commit: r1707213 - in /lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd: BKDReader.java BKDUtil.java BKDWriter.java HeapPointWriter.java OfflinePointWriter.java

Author: mikemccand
Date: Wed Oct  7 09:23:53 2015
New Revision: 1707213

URL: http://svn.apache.org/viewvc?rev=1707213&view=rev
Log:
LUCENE-6825: fix some nocommits; remove some difficult try/finally logic

Modified:
    lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java
    lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDUtil.java
    lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
    lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java
    lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/OfflinePointWriter.java

Modified: lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java?rev=1707213&r1=1707212&r2=1707213&view=diff
==============================================================================
--- lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java (original)
+++ lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java Wed Oct  7 09:23:53 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.util.bkd;
 import java.io.IOException;
 import java.util.Arrays;
 
+import org.apache.lucene.codecs.CodecUtil;
 import org.apache.lucene.index.SortedNumericDocValues;
 import org.apache.lucene.search.DocIdSet;
 import org.apache.lucene.store.ByteArrayDataInput;
@@ -67,6 +68,7 @@ public final class BKDReader implements
   /** Caller must pre-seek the provided {@link IndexInput} to the index location that {@link BKDWriter#finish} returned */
   public BKDReader(IndexInput in, int maxDoc) throws IOException {
 
+    CodecUtil.checkHeader(in, BKDWriter.CODEC_NAME, BKDWriter.VERSION_START, BKDWriter.VERSION_START);
     numDims = in.readVInt();
     maxPointsInLeafNode = in.readVInt();
     bytesPerDim = in.readVInt();
@@ -161,7 +163,7 @@ public final class BKDReader implements
                         byte[] cellMinPacked, byte[] cellMaxPacked)
     throws IOException {
 
-    //System.out.println("\nR: intersect nodeID=" + nodeID + " cellMin=" + bytesToInt(cellMinPacked, 0) + " cellMax=" + bytesToInt(cellMaxPacked, 0));
+    //System.out.println("\nR: intersect nodeID=" + nodeID + " cellMin=" + BKDUtil.bytesToInt(cellMinPacked, 0) + " cellMax=" + BKDUtil.bytesToInt(cellMaxPacked, 0));
 
     // Optimization: only check the visitor when the current cell does not fully contain the bbox.  E.g. if the
     // query is a small area around London, UK, most of the high nodes in the BKD tree as we recurse will fully
@@ -246,16 +248,6 @@ public final class BKDReader implements
     }
   }
 
-  // nocommit removeme
-  static int bytesToInt(byte[] src, int index) {
-    int x = 0;
-    for(int i=0;i<4;i++) {
-      x |= (src[4*index+i] & 0xff) << (24-i*8);
-    }
-    // Re-flip the sign bit to restore the original value:
-    return x ^ 0x80000000;
-  }
-
   @Override
   public long ramBytesUsed() {
     return splitPackedValues.length +

Modified: lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDUtil.java?rev=1707213&r1=1707212&r2=1707213&view=diff
==============================================================================
--- lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDUtil.java (original)
+++ lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDUtil.java Wed Oct  7 09:23:53 2015
@@ -68,4 +68,14 @@ final class BKDUtil {
 
     return true;
   }
+
+  // nocommit only used temporarily for debugging test cases that encode ints:
+  static int bytesToInt(byte[] src, int index) {
+    int x = 0;
+    for(int i=0;i<4;i++) {
+      x |= (src[4*index+i] & 0xff) << (24-i*8);
+    }
+    // Re-flip the sign bit to restore the original value:
+    return x ^ 0x80000000;
+  }
 }

Modified: lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java?rev=1707213&r1=1707212&r2=1707213&view=diff
==============================================================================
--- lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java (original)
+++ lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java Wed Oct  7 09:23:53 2015
@@ -24,6 +24,7 @@ import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.Comparator;
 
+import org.apache.lucene.codecs.CodecUtil;
 import org.apache.lucene.store.ByteArrayDataInput;
 import org.apache.lucene.store.ByteArrayDataOutput;
 import org.apache.lucene.store.IndexOutput;
@@ -71,6 +72,10 @@ import org.apache.lucene.util.RamUsageEs
 
 public final class BKDWriter {
 
+  static final String CODEC_NAME = "BKD";
+  static final int VERSION_START = 0;
+  static final int VERSION_CURRENT = VERSION_START;
+
   /** How many bytes each docs takes in the fixed-width offline format */
   private final int bytesPerDoc;
 
@@ -100,7 +105,7 @@ public final class BKDWriter {
 
   // nocommit grep for lat / lon and fix!
 
-  private OfflineSorter.ByteSequencesWriter writer;
+  private OfflineSorter.ByteSequencesWriter offlinePointWriter;
   private HeapPointWriter heapPointWriter;
 
   private Path tempInput;
@@ -137,6 +142,8 @@ public final class BKDWriter {
 
     // We write first maxPointsSortInHeap in heap, then cutover to offline for additional points:
     heapPointWriter = new HeapPointWriter(16, maxPointsSortInHeap, packedBytesLength);
+
+    // nocommit use TrackingDirWrapper here to do file deletion cleanup in one place...
   }
 
   public static void verifyParams(int numDims, int maxPointsInLeafNode, float maxMBSortInHeap) {
@@ -162,7 +169,7 @@ public final class BKDWriter {
 
     // For each .add we just append to this input file, then in .finish we sort this input and resursively build the tree:
     tempInput = Files.createTempFile(OfflineSorter.getDefaultTempDir(), "in", "");
-    writer = new OfflineSorter.ByteSequencesWriter(tempInput);
+    offlinePointWriter = new OfflineSorter.ByteSequencesWriter(tempInput);
     PointReader reader = heapPointWriter.getReader(0);
     for(int i=0;i<pointCount;i++) {
       boolean hasNext = reader.next();
@@ -175,7 +182,7 @@ public final class BKDWriter {
       scratchBytesOutput.writeVInt(heapPointWriter.docIDs[i]);
       scratchBytesOutput.writeVLong(i);
       // TODO: can/should OfflineSorter optimize the fixed-width case?
-      writer.write(scratchBytes, 0, scratchBytes.length);
+      offlinePointWriter.write(scratchBytes, 0, scratchBytes.length);
     }
 
     heapPointWriter = null;
@@ -187,14 +194,14 @@ public final class BKDWriter {
     }
 
     if (pointCount >= maxPointsSortInHeap) {
-      if (writer == null) {
+      if (offlinePointWriter == null) {
         switchToOffline();
       }
       scratchBytesOutput.reset(scratchBytes);
       scratchBytesOutput.writeBytes(packedValue, 0, packedValue.length);
       scratchBytesOutput.writeVInt(docID);
       scratchBytesOutput.writeVLong(pointCount);
-      writer.write(scratchBytes, 0, scratchBytes.length);
+      offlinePointWriter.write(scratchBytes, 0, scratchBytes.length);
     } else {
       // Not too many points added yet, continue using heap:
       heapPointWriter.append(packedValue, pointCount, docID);
@@ -379,8 +386,8 @@ public final class BKDWriter {
   public long finish(IndexOutput out) throws IOException {
     //System.out.println("\nBKDTreeWriter.finish pointCount=" + pointCount + " out=" + out + " heapWriter=" + heapWriter);
 
-    if (writer != null) {
-      writer.close();
+    if (offlinePointWriter != null) {
+      offlinePointWriter.close();
     }
 
     LongBitSet ordBitSet = new LongBitSet(pointCount);
@@ -418,47 +425,34 @@ public final class BKDWriter {
     // Sort all docs once by each dimension:
     PathSlice[] sortedPointWriters = new PathSlice[numDims];
 
-    boolean success = false;
-    try {
-      for(int dim=0;dim<numDims;dim++) {
-        sortedPointWriters[dim] = new PathSlice(sort(dim), 0, pointCount);
-      }
-      heapPointWriter = null;
+    for(int dim=0;dim<numDims;dim++) {
+      sortedPointWriters[dim] = new PathSlice(sort(dim), 0, pointCount);
+    }
 
-      byte[] minPacked = new byte[packedBytesLength];
-      byte[] maxPacked = new byte[packedBytesLength];
-      Arrays.fill(maxPacked, (byte) 0xff);
+    // nocommit make sure this happens on exc:
+    IOUtils.rm(tempInput);
+    heapPointWriter = null;
 
-      build(1, numLeaves, sortedPointWriters,
-            ordBitSet, out,
-            minPacked, maxPacked,
-            splitPackedValues,
-            leafBlockFPs);
-      success = true;
-    } finally {
-      if (success) {
-        for(int dim=0;dim<numDims;dim++) {
-          sortedPointWriters[dim].writer.destroy();
-        }
-        IOUtils.rm(tempInput);
-      } else {
-        for(int dim=0;dim<numDims;dim++) {
-          try {
-            sortedPointWriters[dim].writer.destroy();
-          } catch (Throwable t) {
-            // Suppress to keep throwing original exc
-          }
-        }
-        IOUtils.deleteFilesIgnoringExceptions(tempInput);
-      }
+    byte[] minPacked = new byte[packedBytesLength];
+    byte[] maxPacked = new byte[packedBytesLength];
+    Arrays.fill(maxPacked, (byte) 0xff);
+
+    build(1, numLeaves, sortedPointWriters,
+          ordBitSet, out,
+          minPacked, maxPacked,
+          splitPackedValues,
+          leafBlockFPs);
+
+    // nocommit make sure this happens on exc:
+    for(PathSlice slice : sortedPointWriters) {
+      slice.writer.destroy();
     }
 
     //System.out.println("Total nodes: " + innerNodeCount);
 
-    // nocommit write header + version here & read/confirm in BKDReader
-
     // Write index:
     long indexFP = out.getFilePointer();
+    CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);
     out.writeVInt(numDims);
     out.writeVInt(maxPointsInLeafNode);
     out.writeVInt(bytesPerDim);
@@ -581,7 +575,7 @@ public final class BKDWriter {
     PathSlice source = slices[splitDim];
 
     if (DEBUG) System.out.println("\nBUILD: nodeID=" + nodeID + " leafNodeOffset=" + leafNodeOffset + " splitDim=" + splitDim + "\n  count=" + source.count + "\n  source=" + source +
-                                  "\n  min=" + bytesToInt(minPackedValue, 0) + " max=" + bytesToInt(maxPackedValue, 0));
+                                  "\n  min=" + BKDUtil.bytesToInt(minPackedValue, 0) + " max=" + BKDUtil.bytesToInt(maxPackedValue, 0));
 
     if (nodeID >= leafNodeOffset) {
       // Leaf node: write block
@@ -706,50 +700,31 @@ public final class BKDWriter {
       System.out.println("pointCount=" + pointCount);
       ordBitSet.clear(0, pointCount);
 
-      boolean success = false;
-      try {
-        // Recurse on left tree:
-        build(2*nodeID, leafNodeOffset, leftSlices,
-              ordBitSet, out,
-              minPackedValue, maxSplitPackedValue,
-              splitPackedValues, leafBlockFPs);
-        for(int dim=0;dim<numDims;dim++) {
-          if (dim != splitDim) {
-            // nocommit need try/finally monster around this?
-            leftSlices[dim].writer.destroy();
-          }
+      // Recurse on left tree:
+      build(2*nodeID, leafNodeOffset, leftSlices,
+            ordBitSet, out,
+            minPackedValue, maxSplitPackedValue,
+            splitPackedValues, leafBlockFPs);
+      for(int dim=0;dim<numDims;dim++) {
+        if (dim != splitDim) {
+          // nocommit need try/finally monster around this?
+          leftSlices[dim].writer.destroy();
         }
+      }
 
-        // Recurse on right tree:
-        build(2*nodeID+1, leafNodeOffset, rightSlices,
-              ordBitSet, out,
-              minSplitPackedValue, maxPackedValue,
-              splitPackedValues, leafBlockFPs);
-        for(int dim=0;dim<numDims;dim++) {
-          if (dim != splitDim) {
-            // nocommit need try/finally monster around this?
-            rightSlices[dim].writer.destroy();
-          }
-        }
-        success = true;
-      } finally {
-        if (success == false) {
-          for(int dim=0;dim<numDims;dim++) {
-            if (dim != splitDim) {
-              try {
-                leftSlices[dim].writer.destroy();
-              } catch (Throwable t) {
-                // Suppress to keep throwing original exc
-              }
-              try {
-                rightSlices[dim].writer.destroy();
-              } catch (Throwable t) {
-                // Suppress to keep throwing original exc
-              }
-            }
-          }
+      // Recurse on right tree:
+      build(2*nodeID+1, leafNodeOffset, rightSlices,
+            ordBitSet, out,
+            minSplitPackedValue, maxPackedValue,
+            splitPackedValues, leafBlockFPs);
+      for(int dim=0;dim<numDims;dim++) {
+        if (dim != splitDim) {
+          // nocommit need try/finally monster around this?
+          rightSlices[dim].writer.destroy();
         }
       }
+      
+      // nocommit make sure we destroy on exception
     }
   }
 
@@ -764,14 +739,4 @@ public final class BKDWriter {
       return new OfflinePointWriter(count, packedBytesLength);
     }
   }
-
-  // nocommit removeme
-  static int bytesToInt(byte[] src, int index) {
-    int x = 0;
-    for(int i=0;i<4;i++) {
-      x |= (src[4*index+i] & 0xff) << (24-i*8);
-    }
-    // Re-flip the sign bit to restore the original value:
-    return x ^ 0x80000000;
-  }
 }

Modified: lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java?rev=1707213&r1=1707212&r2=1707213&view=diff
==============================================================================
--- lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java (original)
+++ lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java Wed Oct  7 09:23:53 2015
@@ -34,7 +34,7 @@ final class HeapPointWriter implements P
   final List<byte[]> blocks = new ArrayList<>();
 
   public HeapPointWriter(int initSize, int maxSize, int packedBytesLength) {
-    System.out.println("HeapWriter.init initSize=" + initSize + " maxSize=" + maxSize + " packedBytesLength=" + packedBytesLength);
+    System.out.println("HeapPointWriter.init initSize=" + initSize + " maxSize=" + maxSize + " packedBytesLength=" + packedBytesLength);
     docIDs = new int[initSize];
     ords = new long[initSize];
     this.maxSize = maxSize;
@@ -109,6 +109,6 @@ final class HeapPointWriter implements P
 
   @Override
   public String toString() {
-    return "HeapWriter(count=" + nextWrite + " alloc=" + ords.length + ")";
+    return "HeapPointWriter(count=" + nextWrite + " alloc=" + ords.length + ")";
   }
 }

Modified: lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/OfflinePointWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/OfflinePointWriter.java?rev=1707213&r1=1707212&r2=1707213&view=diff
==============================================================================
--- lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/OfflinePointWriter.java (original)
+++ lucene/dev/branches/lucene6825/lucene/core/src/java/org/apache/lucene/util/bkd/OfflinePointWriter.java Wed Oct  7 09:23:53 2015
@@ -78,7 +78,7 @@ final class OfflinePointWriter implement
 
   @Override
   public String toString() {
-    return "OfflineWriter(count=" + count + " tempFile=" + tempFile + ")";
+    return "OfflinePointWriter(count=" + count + " tempFile=" + tempFile + ")";
   }
 }