Apache Lucene open-source search software
Merge trunk.
Adrien Grand577364e414fc
diff --cc lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.javaindex f1751986862,00000000000..6b16c86bacdmode 100644,000000..100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/AbstractBlockPackedWriter.java@@@ -1,146 -1,0 +1,146 @@@+package org.apache.lucene.util.packed;++/*+ * 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.+ */++import java.io.IOException;+import java.util.Arrays;++import org.apache.lucene.store.DataOutput;++abstract class AbstractBlockPackedWriter {++ static final int MAX_BLOCK_SIZE = 1 << (30 - 3);+ static final int MIN_VALUE_EQUALS_0 = 1 << 0;+ static final int BPV_SHIFT = 1;++ static void checkBlockSize(int blockSize) {+ if (blockSize <= 0 || blockSize > MAX_BLOCK_SIZE) {+ throw new IllegalArgumentException("blockSize must be > 0 and < " + MAX_BLOCK_SIZE + ", got " + blockSize);+ }+ if (blockSize < 64) {+ throw new IllegalArgumentException("blockSize must be >= 64, got " + blockSize);+ }+ if ((blockSize & (blockSize - 1)) != 0) {+ throw new IllegalArgumentException("blockSize must be a power of two, got " + blockSize);+ }+ }++ static long zigZagEncode(long n) {+ return (n >> 63) ^ (n << 1);+ }++ // same as DataOutput.writeVLong but accepts negative values+ static void writeVLong(DataOutput out, long i) throws IOException {+ int k = 0;+ while ((i & ~0x7FL) != 0L && k++ < 8) {+ out.writeByte((byte)((i & 0x7FL) | 0x80L));+ i >>>= 7;+ }+ out.writeByte((byte) i);+ }++ protected DataOutput out;+ protected final long[] values;+ protected byte[] blocks;+ protected int off;+ protected long ord;+ protected boolean finished;++ /**+ * Sole constructor.+ * @param blockSize the number of values of a single block, must be a multiple of <tt>64</tt>+ */+ public AbstractBlockPackedWriter(DataOutput out, int blockSize) {+ checkBlockSize(blockSize);+ reset(out);+ values = new long[blockSize];+ }++ /** Reset this writer to wrap <code>out</code>. The block size remains unchanged. */+ public void reset(DataOutput out) {+ assert out != null;+ this.out = out;+ off = 0;+ ord = 0L;+ finished = false;+ }++ private void checkNotFinished() {+ if (finished) {+ throw new IllegalStateException("Already finished");+ }+ }++ /** Append a new long. */+ public void add(long l) throws IOException {+ checkNotFinished();+ if (off == values.length) {+ flush();+ }+ values[off++] = l;+ ++ord;+ }++ // For testing only+ void addBlockOfZeros() throws IOException {+ checkNotFinished();+ if (off != 0 && off != values.length) {+ throw new IllegalStateException("" + off);+ }+ if (off == values.length) {+ flush();+ }+ Arrays.fill(values, 0);+ off = values.length;+ ord += values.length;+ }++ /** Flush all buffered data to disk. This instance is not usable anymore+ * after this method has been called until {@link #reset(DataOutput)} has+ * been called. */+ public void finish() throws IOException {+ checkNotFinished();+ if (off > 0) {+ flush();+ }+ finished = true;+ }++ /** Return the number of values which have been added. */+ public long ord() {+ return ord;+ }++ protected abstract void flush() throws IOException;++ protected final void writeValues(int bitsRequired) throws IOException {+ final PackedInts.Encoder encoder = PackedInts.getEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, bitsRequired);- final int iterations = values.length / encoder.valueCount();- final int blockSize = encoder.blockCount() * 8 * iterations;++ final int iterations = values.length / encoder.byteValueCount();++ final int blockSize = encoder.byteBlockCount() * iterations;+ if (blocks == null || blocks.length < blockSize) {+ blocks = new byte[blockSize];+ }+ if (off < values.length) {+ Arrays.fill(values, off, values.length, 0L);+ }+ encoder.encode(values, 0, blocks, 0, iterations);+ final int blockCount = (int) PackedInts.Format.PACKED.byteCount(PackedInts.VERSION_CURRENT, off, bitsRequired);+ out.writeBytes(blocks, blockCount);+ }++}diff --cc lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.javaindex 0b59f32c4d2,00000000000..288518d13b2mode 100644,000000..100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/BlockPackedReaderIterator.java@@@ -1,241 -1,0 +1,241 @@@+package org.apache.lucene.util.packed;++/*+ * 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.+ */++import static org.apache.lucene.util.packed.BlockPackedWriter.BPV_SHIFT;+import static org.apache.lucene.util.packed.BlockPackedWriter.MIN_VALUE_EQUALS_0;+import static org.apache.lucene.util.packed.BlockPackedWriter.checkBlockSize;++import java.io.EOFException;+import java.io.IOException;+import java.util.Arrays;++import org.apache.lucene.store.DataInput;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.util.LongsRef;++/**+ * Reader for sequences of longs written with {@link BlockPackedWriter}.+ * @see BlockPackedWriter+ * @lucene.internal+ */+public final class BlockPackedReaderIterator {++ static long zigZagDecode(long n) {+ return ((n >>> 1) ^ -(n & 1));+ }++ // same as DataInput.readVLong but supports negative values+ static long readVLong(DataInput in) throws IOException {+ byte b = in.readByte();+ if (b >= 0) return b;+ long i = b & 0x7FL;+ b = in.readByte();+ i |= (b & 0x7FL) << 7;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 14;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 21;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 28;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 35;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 42;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0x7FL) << 49;+ if (b >= 0) return i;+ b = in.readByte();+ i |= (b & 0xFFL) << 56;+ return i;+ }++ DataInput in;+ final int packedIntsVersion;+ long valueCount;+ final int blockSize;+ final long[] values;+ final LongsRef valuesRef;+ byte[] blocks;+ int off;+ long ord;++ /** Sole constructor.+ * @param blockSize the number of values of a block, must be equal to the+ * block size of the {@link BlockPackedWriter} which has+ * been used to write the stream+ */+ public BlockPackedReaderIterator(DataInput in, int packedIntsVersion, int blockSize, long valueCount) {+ checkBlockSize(blockSize);+ this.packedIntsVersion = packedIntsVersion;+ this.blockSize = blockSize;+ this.values = new long[blockSize];+ this.valuesRef = new LongsRef(this.values, 0, 0);+ reset(in, valueCount);+ }++ /** Reset the current reader to wrap a stream of <code>valueCount</code>+ * values contained in <code>in</code>. The block size remains unchanged. */+ public void reset(DataInput in, long valueCount) {+ this.in = in;+ assert valueCount >= 0;+ this.valueCount = valueCount;+ off = blockSize;+ ord = 0;+ }++ /** Skip exactly <code>count</code> values. */+ public void skip(long count) throws IOException {+ assert count >= 0;+ if (ord + count > valueCount || ord + count < 0) {+ throw new EOFException();+ }++ // 1. skip buffered values+ final int skipBuffer = (int) Math.min(count, blockSize - off);+ off += skipBuffer;+ ord += skipBuffer;+ count -= skipBuffer;+ if (count == 0L) {+ return;+ }++ // 2. skip as many blocks as necessary+ assert off == blockSize;+ while (count >= blockSize) {+ final int token = in.readByte() & 0xFF;+ final int bitsPerValue = token >>> BPV_SHIFT;+ if (bitsPerValue > 64) {+ throw new IOException("Corrupted");+ }+ if ((token & MIN_VALUE_EQUALS_0) == 0) {+ readVLong(in);+ }+ final long blockBytes = PackedInts.Format.PACKED.byteCount(packedIntsVersion, blockSize, bitsPerValue);+ skipBytes(blockBytes);+ ord += blockSize;+ count -= blockSize;+ }+ if (count == 0L) {+ return;+ }++ // 3. skip last values+ assert count < blockSize;+ refill();+ ord += count;+ off += count;+ }++ private void skipBytes(long count) throws IOException {+ if (in instanceof IndexInput) {+ final IndexInput iin = (IndexInput) in;+ iin.seek(iin.getFilePointer() + count);+ } else {+ if (blocks == null) {+ blocks = new byte[blockSize];+ }+ long skipped = 0;+ while (skipped < count) {+ final int toSkip = (int) Math.min(blocks.length, count - skipped);+ in.readBytes(blocks, 0, toSkip);+ skipped += toSkip;+ }+ }+ }++ /** Read the next value. */+ public long next() throws IOException {+ if (ord == valueCount) {+ throw new EOFException();+ }+ if (off == blockSize) {+ refill();+ }+ final long value = values[off++];+ ++ord;+ return value;+ }++ /** Read between <tt>1</tt> and <code>count</code> values. */+ public LongsRef next(int count) throws IOException {+ assert count > 0;+ if (ord == valueCount) {+ throw new EOFException();+ }+ if (off == blockSize) {+ refill();+ }++ count = Math.min(count, blockSize - off);+ count = (int) Math.min(count, valueCount - ord);++ valuesRef.offset = off;+ valuesRef.length = count;+ off += count;+ ord += count;+ return valuesRef;+ }++ private void refill() throws IOException {+ final int token = in.readByte() & 0xFF;+ final boolean minEquals0 = (token & MIN_VALUE_EQUALS_0) != 0;+ final int bitsPerValue = token >>> BPV_SHIFT;+ if (bitsPerValue > 64) {+ throw new IOException("Corrupted");+ }+ final long minValue = minEquals0 ? 0L : zigZagDecode(1L + readVLong(in));+ assert minEquals0 || minValue != 0;++ if (bitsPerValue == 0) {+ Arrays.fill(values, minValue);+ } else {+ final PackedInts.Decoder decoder = PackedInts.getDecoder(PackedInts.Format.PACKED, packedIntsVersion, bitsPerValue);- final int iterations = blockSize / decoder.valueCount();- final int blocksSize = iterations * 8 * decoder.blockCount();++ final int iterations = blockSize / decoder.byteValueCount();++ final int blocksSize = iterations * decoder.byteBlockCount();+ if (blocks == null || blocks.length < blocksSize) {+ blocks = new byte[blocksSize];+ }++ final int valueCount = (int) Math.min(this.valueCount - ord, blockSize);+ final int blocksCount = (int) PackedInts.Format.PACKED.byteCount(packedIntsVersion, valueCount, bitsPerValue);+ in.readBytes(blocks, 0, blocksCount);++ decoder.decode(blocks, 0, values, 0, iterations);++ if (minValue != 0) {+ for (int i = 0; i < valueCount; ++i) {+ values[i] += minValue;+ }+ }+ }+ off = 0;+ }++ /** Return the offset of the next value to read. */+ public long ord() {+ return ord;+ }++}