org.apache.pig.data.utils
Class SedesHelper.Varint

java.lang.Object
  extended by org.apache.pig.data.utils.SedesHelper.Varint
Enclosing class:
SedesHelper

public static class SedesHelper.Varint
extends Object

Encodes signed and unsigned values using a common variable-length scheme, found for example in Google's Protocol Buffers. It uses fewer bytes to encode smaller values, but will use slightly more bytes to encode large values.

Signed values are further encoded using so-called zig-zag encoding in order to make them "compatible" with variable-length encoding.

This is taken from mahout-core, and is included to avoid having to pull in the entirety of Mahout.


Method Summary
static int readSignedVarInt(DataInput in)
           
static long readSignedVarLong(DataInput in)
           
static int readUnsignedVarInt(DataInput in)
           
static long readUnsignedVarLong(DataInput in)
           
static void writeSignedVarInt(int value, DataOutput out)
           
static void writeSignedVarLong(long value, DataOutput out)
          Encodes a value using the variable-length encoding from Google Protocol Buffers.
static void writeUnsignedVarInt(int value, DataOutput out)
           
static void writeUnsignedVarLong(long value, DataOutput out)
          Encodes a value using the variable-length encoding from Google Protocol Buffers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeSignedVarLong

public static void writeSignedVarLong(long value,
                                      DataOutput out)
                               throws IOException
Encodes a value using the variable-length encoding from Google Protocol Buffers. It uses zig-zag encoding to efficiently encode signed values. If values are known to be nonnegative, writeUnsignedVarLong(long, DataOutput) should be used.

Parameters:
value - value to encode
out - to write bytes to
Throws:
IOException - if DataOutput throws IOException

writeUnsignedVarLong

public static void writeUnsignedVarLong(long value,
                                        DataOutput out)
                                 throws IOException
Encodes a value using the variable-length encoding from Google Protocol Buffers. Zig-zag is not used, so input must not be negative. If values can be negative, use writeSignedVarLong(long, DataOutput) instead. This method treats negative input as like a large unsigned value.

Parameters:
value - value to encode
out - to write bytes to
Throws:
IOException - if DataOutput throws IOException

writeSignedVarInt

public static void writeSignedVarInt(int value,
                                     DataOutput out)
                              throws IOException
Throws:
IOException
See Also:
writeSignedVarLong(long, DataOutput)

writeUnsignedVarInt

public static void writeUnsignedVarInt(int value,
                                       DataOutput out)
                                throws IOException
Throws:
IOException
See Also:
writeUnsignedVarLong(long, DataOutput)

readSignedVarLong

public static long readSignedVarLong(DataInput in)
                              throws IOException
Parameters:
in - to read bytes from
Returns:
decode value
Throws:
IOException - if DataInput throws IOException
IllegalArgumentException - if variable-length value does not terminate after 9 bytes have been read
See Also:
writeSignedVarLong(long, DataOutput)

readUnsignedVarLong

public static long readUnsignedVarLong(DataInput in)
                                throws IOException
Parameters:
in - to read bytes from
Returns:
decode value
Throws:
IOException - if DataInput throws IOException
IllegalArgumentException - if variable-length value does not terminate after 9 bytes have been read
See Also:
writeUnsignedVarLong(long, DataOutput)

readSignedVarInt

public static int readSignedVarInt(DataInput in)
                            throws IOException
Throws:
IllegalArgumentException - if variable-length value does not terminate after 5 bytes have been read
IOException - if DataInput throws IOException
See Also:
readSignedVarLong(DataInput)

readUnsignedVarInt

public static int readUnsignedVarInt(DataInput in)
                              throws IOException
Throws:
IllegalArgumentException - if variable-length value does not terminate after 5 bytes have been read
IOException - if DataInput throws IOException
See Also:
readUnsignedVarLong(DataInput)


Copyright © 2007-2012 The Apache Software Foundation