From d1562ef6837e49bb055ebba27329516002de7e31 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 25 Apr 2012 19:36:41 +0000 Subject: [PATCH] LUCENE-2946: put vint table from fileformats in writeVInt git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1330511 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/store/DataInput.java | 10 +- .../org/apache/lucene/store/DataOutput.java | 103 ++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/lucene/core/src/java/org/apache/lucene/store/DataInput.java b/lucene/core/src/java/org/apache/lucene/store/DataInput.java index 6d2e621efb3..c711e6afba0 100644 --- a/lucene/core/src/java/org/apache/lucene/store/DataInput.java +++ b/lucene/core/src/java/org/apache/lucene/store/DataInput.java @@ -79,6 +79,9 @@ public abstract class DataInput implements Cloneable { /** Reads an int stored in variable-length format. Reads between one and * five bytes. Smaller values take fewer bytes. Negative numbers are not * supported. + *

+ * The format is described further in {@link DataOutput#writeVInt(int)}. + * * @see DataOutput#writeVInt(int) */ public int readVInt() throws IOException { @@ -121,7 +124,12 @@ public abstract class DataInput implements Cloneable { /** Reads a long stored in variable-length format. Reads between one and * nine bytes. Smaller values take fewer bytes. Negative numbers are not - * supported. */ + * supported. + *

+ * The format is described further in {@link DataOutput#writeVInt(int)}. + * + * @see DataOutput#writeVLong(long) + */ public long readVLong() throws IOException { /* This is the original code of this method, * but a Hotspot bug (see LUCENE-2975) corrupts the for-loop if diff --git a/lucene/core/src/java/org/apache/lucene/store/DataOutput.java b/lucene/core/src/java/org/apache/lucene/store/DataOutput.java index 80a17451c26..f068142bc5a 100644 --- a/lucene/core/src/java/org/apache/lucene/store/DataOutput.java +++ b/lucene/core/src/java/org/apache/lucene/store/DataOutput.java @@ -72,6 +72,107 @@ public abstract class DataOutput { /** Writes an int in a variable-length format. Writes between one and * five bytes. Smaller values take fewer bytes. Negative numbers are * supported, but should be avoided. + *

VByte is a variable-length format for positive integers is defined where the + * high-order bit of each byte indicates whether more bytes remain to be read. The + * low-order seven bits are appended as increasingly more significant bits in the + * resulting integer value. Thus values from zero to 127 may be stored in a single + * byte, values from 128 to 16,383 may be stored in two bytes, and so on.

+ *

VByte Encoding Example

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
ValueByte 1Byte 2Byte 3
000000000
100000001
200000010
...
12701111111
1281000000000000001
1291000000100000001
1301000001000000001
...
16,3831111111101111111
16,384100000001000000000000001
16,385100000011000000000000001
...
+ *

This provides compression while still being efficient to decode.

+ * + * @param i Smaller values take fewer bytes. Negative numbers are + * supported, but should be avoided. + * @throws IOException If there is an I/O error writing to the underlying medium. * @see DataInput#readVInt() */ public final void writeVInt(int i) throws IOException { @@ -93,6 +194,8 @@ public abstract class DataOutput { /** Writes an long in a variable-length format. Writes between one and nine * bytes. Smaller values take fewer bytes. Negative numbers are not * supported. + *

+ * The format is described further in {@link DataOutput#writeVInt(int)}. * @see DataInput#readVLong() */ public final void writeVLong(long i) throws IOException {