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:
Adrien Grand 2014-04-11 14:56:29 +00:00
parent d598f8b599
commit b2f347c92d
9 changed files with 44 additions and 26 deletions

View File

@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing;
* limitations under the License.
*/
import static org.apache.lucene.util.BitUtil.zigZagDecode;
import java.io.IOException;
import java.util.Arrays;
@ -33,10 +35,6 @@ import org.apache.lucene.util.packed.PackedInts;
*/
public final class CompressingStoredFieldsIndexReader implements Cloneable {
static long moveLowOrderBitToSign(long n) {
return ((n >>> 1) ^ -(n & 1));
}
final int maxDoc;
final int[] docBases;
final long[] startPointers;
@ -122,13 +120,13 @@ public final class CompressingStoredFieldsIndexReader implements Cloneable {
private int relativeDocBase(int block, int 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;
}
private long relativeStartPointer(int block, int 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;
}

View File

@ -17,6 +17,8 @@ package org.apache.lucene.codecs.compressing;
* limitations under the License.
*/
import static org.apache.lucene.util.BitUtil.zigZagEncode;
import java.io.Closeable;
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 long moveSignToLowOrderBit(long n) {
return (n >> 63) ^ (n << 1);
}
final IndexOutput fieldsIndexOut;
int totalDocs;
int blockDocs;
@ -124,7 +122,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
long maxDelta = 0;
for (int i = 0; i < blockChunks; ++i) {
final int delta = docBase - avgChunkDocs * i;
maxDelta |= moveSignToLowOrderBit(delta);
maxDelta |= zigZagEncode(delta);
docBase += docBaseDeltas[i];
}
@ -135,8 +133,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
docBase = 0;
for (int i = 0; i < blockChunks; ++i) {
final long delta = docBase - avgChunkDocs * i;
assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue();
writer.add(moveSignToLowOrderBit(delta));
assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue();
writer.add(zigZagEncode(delta));
docBase += docBaseDeltas[i];
}
writer.finish();
@ -155,7 +153,7 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
for (int i = 0; i < blockChunks; ++i) {
startPointer += startPointerDeltas[i];
final long delta = startPointer - avgChunkSize * i;
maxDelta |= moveSignToLowOrderBit(delta);
maxDelta |= zigZagEncode(delta);
}
final int bitsPerStartPointer = PackedInts.bitsRequired(maxDelta);
@ -166,8 +164,8 @@ public final class CompressingStoredFieldsIndexWriter implements Closeable {
for (int i = 0; i < blockChunks; ++i) {
startPointer += startPointerDeltas[i];
final long delta = startPointer - avgChunkSize * i;
assert PackedInts.bitsRequired(moveSignToLowOrderBit(delta)) <= writer.bitsPerValue();
writer.add(moveSignToLowOrderBit(delta));
assert PackedInts.bitsRequired(zigZagEncode(delta)) <= writer.bitsPerValue();
writer.add(zigZagEncode(delta));
}
writer.finish();
}

View File

@ -187,4 +187,29 @@ public final class BitUtil {
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));
}
}

View File

@ -31,10 +31,6 @@ abstract class AbstractBlockPackedWriter {
static final int MIN_VALUE_EQUALS_0 = 1 << 0;
static final int BPV_SHIFT = 1;
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;

View File

@ -17,12 +17,12 @@ package org.apache.lucene.util.packed;
* 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.MAX_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.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.numBlocks;

View File

@ -17,6 +17,7 @@ package org.apache.lucene.util.packed;
* 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.MAX_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 {
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();

View File

@ -17,6 +17,8 @@ package org.apache.lucene.util.packed;
* limitations under the License.
*/
import static org.apache.lucene.util.BitUtil.zigZagEncode;
import java.io.IOException;
import org.apache.lucene.store.DataOutput;

View File

@ -17,9 +17,9 @@ package org.apache.lucene.util.packed;
* 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.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.numBlocks;

View File

@ -17,6 +17,8 @@ package org.apache.lucene.util.packed;
* limitations under the License.
*/
import static org.apache.lucene.util.BitUtil.zigZagEncode;
import java.io.IOException;
import org.apache.lucene.store.DataOutput;