iverase commented on a change in pull request #7: URL: https://github.com/apache/lucene/pull/7#discussion_r614031179
########## File path: lucene/core/src/java/org/apache/lucene/util/bkd/BKDDefaultReader.java ########## @@ -0,0 +1,899 @@ +/* + * 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.io.UncheckedIOException; +import java.util.Arrays; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.PointValues; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.MathUtil; + +/** + * Handles reading a block KD-tree previously written with {@link BKDWriter}. + * + * @lucene.experimental + */ +public class BKDDefaultReader implements BKDReader { + + final BKDConfig config; + final int numLeaves; + // Packed array of byte[] holding all docs and values: + final IndexInput in; + final byte[] minPackedValue; + final byte[] maxPackedValue; + final long pointCount; + final int docCount; + final int version; + final long minLeafBlockFP; + // Packed array of byte[] holding all split values in the full binary tree: + private final IndexInput packedIndex; + + /** + * Caller must pre-seek the provided {@link IndexInput} to the index location that {@link + * BKDWriter#finish} returned. BKD tree is always stored off-heap. + */ + public BKDDefaultReader(IndexInput metaIn, IndexInput indexIn, IndexInput dataIn) + throws IOException { + version = + CodecUtil.checkHeader( + metaIn, BKDWriter.CODEC_NAME, BKDWriter.VERSION_START, BKDWriter.VERSION_CURRENT); + final int numDims = metaIn.readVInt(); + final int numIndexDims; + if (version >= BKDWriter.VERSION_SELECTIVE_INDEXING) { + numIndexDims = metaIn.readVInt(); + } else { + numIndexDims = numDims; + } + final int maxPointsInLeafNode = metaIn.readVInt(); + final int bytesPerDim = metaIn.readVInt(); + config = new BKDConfig(numDims, numIndexDims, bytesPerDim, maxPointsInLeafNode); + + // Read index: + numLeaves = metaIn.readVInt(); + assert numLeaves > 0; + + minPackedValue = new byte[config.packedIndexBytesLength]; + maxPackedValue = new byte[config.packedIndexBytesLength]; + + metaIn.readBytes(minPackedValue, 0, config.packedIndexBytesLength); + metaIn.readBytes(maxPackedValue, 0, config.packedIndexBytesLength); + + for (int dim = 0; dim < config.numIndexDims; dim++) { + if (Arrays.compareUnsigned( + minPackedValue, + dim * config.bytesPerDim, + dim * config.bytesPerDim + config.bytesPerDim, + maxPackedValue, + dim * config.bytesPerDim, + dim * config.bytesPerDim + config.bytesPerDim) + > 0) { + throw new CorruptIndexException( + "minPackedValue " + + new BytesRef(minPackedValue) + + " is > maxPackedValue " + + new BytesRef(maxPackedValue) + + " for dim=" + + dim, + metaIn); + } + } + + pointCount = metaIn.readVLong(); + docCount = metaIn.readVInt(); + + int numIndexBytes = metaIn.readVInt(); + long indexStartPointer; + if (version >= BKDWriter.VERSION_META_FILE) { + minLeafBlockFP = metaIn.readLong(); + indexStartPointer = metaIn.readLong(); + } else { + indexStartPointer = indexIn.getFilePointer(); + minLeafBlockFP = indexIn.readVLong(); + indexIn.seek(indexStartPointer); + } + this.packedIndex = indexIn.slice("packedIndex", indexStartPointer, numIndexBytes); + this.in = dataIn; + } + + @Override + public BKDConfig getConfig() { + return config; + } + + @Override + public byte[] getMinPackedValue() { + return minPackedValue; + } + + @Override + public byte[] getMaxPackedValue() { + return maxPackedValue; + } Review comment: yes, I am cloning them in BKDPointValues but that is probably wrong and should be clone at this level. -- 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. 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