com.nhncorp.neptune.common.io
Class NWritableComparator

java.lang.Object
  extended by com.nhncorp.neptune.common.io.NWritableComparator
All Implemented Interfaces:
java.util.Comparator
Direct Known Subclasses:
NText.Comparator, NUTF8.Comparator

public class NWritableComparator
extends java.lang.Object
implements java.util.Comparator

A Comparator for NWritableComparables.

This base implemenation uses the natural ordering. To define alternate orderings, override compare(NWritableComparable,NWritableComparable).

One may optimize compare-intensive operations by overriding compare(byte[],int,int,byte[],int,int). Static utility methods are provided to assist in optimized implementations of this method.


Method Summary
 int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
          Optimization hook.
 int compare(NWritableComparable a, NWritableComparable b)
          Compare two WritableComparables.
 int compare(java.lang.Object a, java.lang.Object b)
           
static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
          Lexicographic order of binary data.
static void define(java.lang.Class c, NWritableComparator comparator)
          Register an optimized comparator for a NWritableComparable implementation.
static NWritableComparator get(java.lang.Class c)
          Get a comparator for a NWritableComparable implementation.
 java.lang.Class getKeyClass()
          Returns the WritableComparable implementation class.
static int hashBytes(byte[] bytes, int length)
          Compute hash for binary data.
 NWritableComparable newKey()
          Construct a new NWritableComparable instance.
static double readDouble(byte[] bytes, int start)
          Parse a double from a byte array.
static float readFloat(byte[] bytes, int start)
          Parse a float from a byte array.
static int readInt(byte[] bytes, int start)
          Parse an integer from a byte array.
static long readLong(byte[] bytes, int start)
          Parse a long from a byte array.
static int readUnsignedShort(byte[] bytes, int start)
          Parse an unsigned short from a byte array.
static int readVInt(byte[] bytes, int start)
          Reads a zero-compressed encoded integer from a byte array and returns it.
static long readVLong(byte[] bytes, int start)
          Reads a zero-compressed encoded long from a byte array and returns it.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Method Detail

get

public static NWritableComparator get(java.lang.Class c)
Get a comparator for a NWritableComparable implementation.


define

public static void define(java.lang.Class c,
                          NWritableComparator comparator)
Register an optimized comparator for a NWritableComparable implementation.


getKeyClass

public java.lang.Class getKeyClass()
Returns the WritableComparable implementation class.


newKey

public NWritableComparable newKey()
Construct a new NWritableComparable instance.


compare

public int compare(byte[] b1,
                   int s1,
                   int l1,
                   byte[] b2,
                   int s2,
                   int l2)
Optimization hook. Override this to make SequenceFile.Sorter's scream.

The default implementation reads the data into two NWritableComparables (using NWritable.readFields(DataInput), then calls compare(NWritableComparable,NWritableComparable).


compare

public int compare(NWritableComparable a,
                   NWritableComparable b)
Compare two WritableComparables.

The default implementation uses the natural ordering, calling Comparable.compareTo(Object).


compare

public int compare(java.lang.Object a,
                   java.lang.Object b)
Specified by:
compare in interface java.util.Comparator

compareBytes

public static int compareBytes(byte[] b1,
                               int s1,
                               int l1,
                               byte[] b2,
                               int s2,
                               int l2)
Lexicographic order of binary data.


hashBytes

public static int hashBytes(byte[] bytes,
                            int length)
Compute hash for binary data.


readUnsignedShort

public static int readUnsignedShort(byte[] bytes,
                                    int start)
Parse an unsigned short from a byte array.


readInt

public static int readInt(byte[] bytes,
                          int start)
Parse an integer from a byte array.


readFloat

public static float readFloat(byte[] bytes,
                              int start)
Parse a float from a byte array.


readLong

public static long readLong(byte[] bytes,
                            int start)
Parse a long from a byte array.


readDouble

public static double readDouble(byte[] bytes,
                                int start)
Parse a double from a byte array.


readVLong

public static long readVLong(byte[] bytes,
                             int start)
                      throws java.io.IOException
Reads a zero-compressed encoded long from a byte array and returns it.

Parameters:
bytes - byte array with decode long
start - starting index
Returns:
deserialized long
Throws:
java.io.IOException

readVInt

public static int readVInt(byte[] bytes,
                           int start)
                    throws java.io.IOException
Reads a zero-compressed encoded integer from a byte array and returns it.

Parameters:
bytes - byte array with the encoded integer
start - start index
Returns:
deserialized integer
Throws:
java.io.IOException