org.apache.hadoop.hbase.util
Class ByteRange

java.lang.Object
  extended by org.apache.hadoop.hbase.util.ByteRange
All Implemented Interfaces:
Comparable<ByteRange>

public class ByteRange
extends Object
implements Comparable<ByteRange>

Lightweight, reusable class for specifying ranges of byte[]'s. CompareTo and equals methods are lexicographic, which is native to HBase.

This class differs from ByteBuffer:

  • On-heap bytes only
  • Implements equals, hashCode, and compareTo so that it can be used in standard java Collections, similar to String.
  • Does not maintain mark/position iterator state inside the class. Doing so leads to many bugs in complex applications.
  • Allows the addition of simple core methods like this.copyTo(that, offset).
  • Can be reused in tight loops like a major compaction which can save significant amounts of garbage.
  • (Without reuse, we throw off garbage like this thing: http://www.youtube.com/watch?v=lkmBH-MjZF4

    Mutable, and always evaluates equals, hashCode, and compareTo based on the current contents.

    Can contain convenience methods for comparing, printing, cloning, spawning new arrays, copying to other arrays, etc. Please place non-core methods into ByteRangeTool.

    We may consider converting this to an interface and creating separate implementations for a single byte[], a paged byte[] (growable byte[][]), a ByteBuffer, etc


    Constructor Summary
    ByteRange()
              construct
    ByteRange(byte[] bytes)
               
    ByteRange(byte[] bytes, int offset, int length)
               
     
    Method Summary
     ByteRange clear()
              write methods
     int compareTo(ByteRange other)
              Bitwise comparison of each byte in the array.
     ByteRange deepCopy()
              Create a new ByteRange with new backing byte[] and copy the state of this range into the new range.
     void deepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, int destinationOffset)
              Wrapper for System.arraycopy.
     void deepCopyTo(byte[] destination, int destinationOffset)
              Wrapper for System.arraycopy.
     byte[] deepCopyToNewArray()
              Instantiate a new byte[] with exact length, which is at least 24 bytes + length.
     boolean equals(Object thatObject)
              standard methods
     byte get(int index)
               
     byte[] getBytes()
               
     int getLength()
               
     int getOffset()
               
     int hashCode()
               
     boolean isEmpty()
               
    static boolean isEmpty(ByteRange range)
              static methods
     boolean notEmpty()
               
    static boolean notEmpty(ByteRange range)
               
     int numEqualPrefixBytes(ByteRange that, int thatInnerOffset)
               
     ByteRange set(byte[] bytes)
               
     ByteRange set(byte[] bytes, int offset, int length)
               
     void setLength(int length)
               
     ByteRange shallowCopySubRange(int innerOffset, int copyLength)
              Create a new ByteRange that points at this range's byte[].
     String toString()
               
     
    Methods inherited from class java.lang.Object
    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
     

    Constructor Detail

    ByteRange

    public ByteRange()
    construct


    ByteRange

    public ByteRange(byte[] bytes)

    ByteRange

    public ByteRange(byte[] bytes,
                     int offset,
                     int length)
    Method Detail

    clear

    public ByteRange clear()
    write methods


    set

    public ByteRange set(byte[] bytes)

    set

    public ByteRange set(byte[] bytes,
                         int offset,
                         int length)

    setLength

    public void setLength(int length)

    get

    public byte get(int index)
    Parameters:
    index - zero-based index
    Returns:
    single byte at index

    deepCopyToNewArray

    public byte[] deepCopyToNewArray()
    Instantiate a new byte[] with exact length, which is at least 24 bytes + length. Copy the contents of this range into it.

    Returns:
    The newly cloned byte[].

    deepCopy

    public ByteRange deepCopy()
    Create a new ByteRange with new backing byte[] and copy the state of this range into the new range. Copy the hash over if it is already calculated.

    Returns:
    Deep copy

    deepCopyTo

    public void deepCopyTo(byte[] destination,
                           int destinationOffset)
    Wrapper for System.arraycopy. Copy the contents of this range into the provided array.

    Parameters:
    destination - Copy to this array
    destinationOffset - First index in the destination array.

    deepCopySubRangeTo

    public void deepCopySubRangeTo(int innerOffset,
                                   int copyLength,
                                   byte[] destination,
                                   int destinationOffset)
    Wrapper for System.arraycopy. Copy the contents of this range into the provided array.

    Parameters:
    innerOffset - Start copying from this index in this source ByteRange. First byte copied is bytes[offset + innerOffset]
    copyLength - Copy this many bytes
    destination - Copy to this array
    destinationOffset - First index in the destination array.

    shallowCopySubRange

    public ByteRange shallowCopySubRange(int innerOffset,
                                         int copyLength)
    Create a new ByteRange that points at this range's byte[]. The new range can have different values for offset and length, but modifying the shallowCopy will modify the bytes in this range's array. Pass over the hash code if it is already cached.

    Parameters:
    innerOffset - First byte of clone will be this.offset + copyOffset.
    copyLength - Number of bytes in the clone.
    Returns:
    new ByteRange object referencing this range's byte[].

    numEqualPrefixBytes

    public int numEqualPrefixBytes(ByteRange that,
                                   int thatInnerOffset)

    getBytes

    public byte[] getBytes()

    getOffset

    public int getOffset()

    getLength

    public int getLength()

    isEmpty

    public boolean isEmpty()

    notEmpty

    public boolean notEmpty()

    isEmpty

    public static boolean isEmpty(ByteRange range)
    static methods


    notEmpty

    public static boolean notEmpty(ByteRange range)

    equals

    public boolean equals(Object thatObject)
    standard methods

    Overrides:
    equals in class Object

    hashCode

    public int hashCode()
    Overrides:
    hashCode in class Object

    compareTo

    public int compareTo(ByteRange other)
    Bitwise comparison of each byte in the array. Unsigned comparison, not paying attention to java's signed bytes.

    Specified by:
    compareTo in interface Comparable<ByteRange>

    toString

    public String toString()
    Overrides:
    toString in class Object


    Copyright © 2013 The Apache Software Foundation. All Rights Reserved.