mirror of https://github.com/apache/lucene.git
Extract the zig-zag encoding/decoding routines we have to utility methods.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1586669 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d598f8b599
commit
b2f347c92d
lucene/core/src/java/org/apache/lucene
|
@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagDecode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@ -33,10 +35,6 @@ import org.apache.lucene.util.packed.PackedInts;
|
||||||
*/
|
*/
|
||||||
public final class CompressingStoredFieldsIndexReader implements Cloneable {
|
public final class CompressingStoredFieldsIndexReader implements Cloneable {
|
||||||
|
|
||||||
static long moveLowOrderBitToSign(long n) {
|
|
||||||
return ((n >>> 1) ^ -(n & 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
final int maxDoc;
|
final int maxDoc;
|
||||||
final int[] docBases;
|
final int[] docBases;
|
||||||
final long[] startPointers;
|
final long[] startPointers;
|
||||||
|
@ -122,13 +120,13 @@ public final class CompressingStoredFieldsIndexReader implements Cloneable {
|
||||||
|
|
||||||
private int relativeDocBase(int block, int relativeChunk) {
|
private int relativeDocBase(int block, int relativeChunk) {
|
||||||
final int expected = avgChunkDocs[block] * relativeChunk;
|
final int expected = avgChunkDocs[block] * relativeChunk;
|
||||||
final long delta = moveLowOrderBitToSign(docBasesDeltas[block].get(relativeChunk));
|
final long delta = zigZagDecode(docBasesDeltas[block].get(relativeChunk));
|
||||||
return expected + (int) delta;
|
return expected + (int) delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long relativeStartPointer(int block, int relativeChunk) {
|
private long relativeStartPointer(int block, int relativeChunk) {
|
||||||
final long expected = avgChunkSizes[block] * relativeChunk;
|
final long expected = avgChunkSizes[block] * relativeChunk;
|
||||||
final long delta = moveLowOrderBitToSign(startPointersDeltas[block].get(relativeChunk));
|
final long delta = zigZagDecode(startPointersDeltas[block].get(relativeChunk));
|
||||||
return expected + delta;
|
return expected + delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagEncode;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -72,10 +74,6 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
|
||||||
|
|
||||||
static final int BLOCK_SIZE = 1024; // number of chunks to serialize at once
|
static final int BLOCK_SIZE = 1024; // number of chunks to serialize at once
|
||||||
|
|
||||||
static long moveSignToLowOrderBit(long n) {
|
|
||||||
return (n >> 63) ^ (n << 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
final IndexOutput fieldsIndexOut;
|
final IndexOutput fieldsIndexOut;
|
||||||
int totalDocs;
|
int totalDocs;
|
||||||
int blockDocs;
|
int blockDocs;
|
||||||
|
@ -124,7 +122,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
|
||||||
long maxDelta = 0;
|
long maxDelta = 0;
|
||||||
for (int i = 0; i < blockChunks; ++i) {
|
for (int i = 0; i < blockChunks; ++i) {
|
||||||
final int delta = docBase - avgChunkDocs * i;
|
final int delta = docBase - avgChunkDocs * i;
|
||||||
maxDelta |= moveSignToLowOrderBit(delta);
|
maxDelta |= zigZagEncode(delta);
|
||||||
docBase += docBaseDeltas[i];
|
docBase += docBaseDeltas[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,8 +133,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
|
||||||
docBase = 0;
|
docBase = 0;
|
||||||
for (int i = 0; i < blockChunks; ++i) {
|
for (int i = 0; i < blockChunks; ++i) {
|
||||||
final long delta = docBase - avgChunkDocs * i;
|
final long delta = docBase - avgChunkDocs * i;
|
||||||
assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue();
|
assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue();
|
||||||
writer.add(moveSignToLowOrderBit(delta));
|
writer.add(zigZagEncode(delta));
|
||||||
docBase += docBaseDeltas[i];
|
docBase += docBaseDeltas[i];
|
||||||
}
|
}
|
||||||
writer.finish();
|
writer.finish();
|
||||||
|
@ -155,7 +153,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
|
||||||
for (int i = 0; i < blockChunks; ++i) {
|
for (int i = 0; i < blockChunks; ++i) {
|
||||||
startPointer += startPointerDeltas[i];
|
startPointer += startPointerDeltas[i];
|
||||||
final long delta = startPointer - avgChunkSize * i;
|
final long delta = startPointer - avgChunkSize * i;
|
||||||
maxDelta |= moveSignToLowOrderBit(delta);
|
maxDelta |= zigZagEncode(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
final int bitsPerStartPointer = PackedInts.bitsRequired(maxDelta);
|
final int bitsPerStartPointer = PackedInts.bitsRequired(maxDelta);
|
||||||
|
@ -166,8 +164,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
|
||||||
for (int i = 0; i < blockChunks; ++i) {
|
for (int i = 0; i < blockChunks; ++i) {
|
||||||
startPointer += startPointerDeltas[i];
|
startPointer += startPointerDeltas[i];
|
||||||
final long delta = startPointer - avgChunkSize * i;
|
final long delta = startPointer - avgChunkSize * i;
|
||||||
assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue();
|
assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue();
|
||||||
writer.add(moveSignToLowOrderBit(delta));
|
writer.add(zigZagEncode(delta));
|
||||||
}
|
}
|
||||||
writer.finish();
|
writer.finish();
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,4 +187,29 @@ public final class BitUtil {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Same as {@link #zigZagEncode(long)} but on integers. */
|
||||||
|
public static int zigZagEncode(int i) {
|
||||||
|
return (i >> 31) ^ (i << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <a href="https://developers.google.com/protocol-buffers/docs/encoding#types">Zig-zag</a>
|
||||||
|
* encode the provided long. Assuming the input is a signed long whose
|
||||||
|
* absolute value can be stored on <tt>n</tt> bits, the returned value will
|
||||||
|
* be an unsigned long that can be stored on <tt>n+1</tt> bits.
|
||||||
|
*/
|
||||||
|
public static long zigZagEncode(long l) {
|
||||||
|
return (l >> 63) ^ (l << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decode an int previously encoded with {@link #zigZagEncode(int)}. */
|
||||||
|
public static int zigZagDecode(int i) {
|
||||||
|
return ((i >>> 1) ^ -(i & 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decode a long previously encoded with {@link #zigZagEncode(long)}. */
|
||||||
|
public static long zigZagDecode(long l) {
|
||||||
|
return ((l >>> 1) ^ -(l & 1));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,6 @@ abstract class AbstractBlockPackedWriter {
|
||||||
static final int MIN_VALUE_EQUALS_0 = 1 << 0;
|
static final int MIN_VALUE_EQUALS_0 = 1 << 0;
|
||||||
static final int BPV_SHIFT = 1;
|
static final int BPV_SHIFT = 1;
|
||||||
|
|
||||||
static long zigZagEncode(long n) {
|
|
||||||
return (n >> 63) ^ (n << 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// same as DataOutput.writeVLong but accepts negative values
|
// same as DataOutput.writeVLong but accepts negative values
|
||||||
static void writeVLong(DataOutput out, long i) throws IOException {
|
static void writeVLong(DataOutput out, long i) throws IOException {
|
||||||
int k = 0;
|
int k = 0;
|
||||||
|
|
|
@ -17,12 +17,12 @@ package org.apache.lucene.util.packed;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagDecode;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_VALUE_EQUALS_0;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_VALUE_EQUALS_0;
|
||||||
import static org.apache.lucene.util.packed.BlockPackedReaderIterator.readVLong;
|
import static org.apache.lucene.util.packed.BlockPackedReaderIterator.readVLong;
|
||||||
import static org.apache.lucene.util.packed.BlockPackedReaderIterator.zigZagDecode;
|
|
||||||
import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;
|
import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;
|
||||||
import static org.apache.lucene.util.packed.PackedInts.numBlocks;
|
import static org.apache.lucene.util.packed.PackedInts.numBlocks;
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.lucene.util.packed;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagDecode;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.BPV_SHIFT;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
||||||
|
@ -38,10 +39,6 @@ import org.apache.lucene.util.LongsRef;
|
||||||
*/
|
*/
|
||||||
public final class BlockPackedReaderIterator {
|
public final class BlockPackedReaderIterator {
|
||||||
|
|
||||||
static long zigZagDecode(long n) {
|
|
||||||
return ((n >>> 1) ^ -(n & 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
// same as DataInput.readVLong but supports negative values
|
// same as DataInput.readVLong but supports negative values
|
||||||
static long readVLong(DataInput in) throws IOException {
|
static long readVLong(DataInput in) throws IOException {
|
||||||
byte b = in.readByte();
|
byte b = in.readByte();
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.lucene.util.packed;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagEncode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.lucene.store.DataOutput;
|
import org.apache.lucene.store.DataOutput;
|
||||||
|
|
|
@ -17,9 +17,9 @@ package org.apache.lucene.util.packed;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagDecode;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MAX_BLOCK_SIZE;
|
||||||
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
import static org.apache.lucene.util.packed.AbstractBlockPackedWriter.MIN_BLOCK_SIZE;
|
||||||
import static org.apache.lucene.util.packed.BlockPackedReaderIterator.zigZagDecode;
|
|
||||||
import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;
|
import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;
|
||||||
import static org.apache.lucene.util.packed.PackedInts.numBlocks;
|
import static org.apache.lucene.util.packed.PackedInts.numBlocks;
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.lucene.util.packed;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import static org.apache.lucene.util.BitUtil.zigZagEncode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.lucene.store.DataOutput;
|
import org.apache.lucene.store.DataOutput;
|
||||||
|
|
Loading…
Reference in New Issue