net.opentsdb.uid
Class UniqueId

java.lang.Object
  extended by net.opentsdb.uid.UniqueId
All Implemented Interfaces:
UniqueIdInterface

public final class UniqueId
extends Object
implements UniqueIdInterface

Thread-safe implementation of the UniqueIdInterface.

Don't attempt to use equals() or hashCode() on this class.

See Also:
UniqueIdInterface

Constructor Summary
UniqueId(org.hbase.async.HBaseClient client, byte[] table, String kind, int width)
          Constructor.
 
Method Summary
 int cacheHits()
          The number of times we avoided reading from HBase thanks to the cache.
 int cacheMisses()
          The number of times we had to read from HBase and populate the cache.
 int cacheSize()
          Returns the number of elements stored in the internal cache.
 byte[] getId(String name)
          Finds the ID associated with a given name.
 String getName(byte[] id)
          Finds the name associated with a given ID.
 byte[] getOrCreateId(String name)
          Finds the ID associated with a given name or creates it.
 String kind()
          Returns what kind of Unique ID is served by this instance.
 void rename(String oldname, String newname)
          Reassigns the UID to a different name (non-atomic).
 List<String> suggest(String search)
          Attempts to find suggestions of names given a search term.
 String toString()
          Returns a human readable string representation of the object.
 short width()
          Returns the number of bytes on which each Unique ID is encoded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UniqueId

public UniqueId(org.hbase.async.HBaseClient client,
                byte[] table,
                String kind,
                int width)
Constructor.

Parameters:
client - The HBase client to use.
table - The name of the HBase table to use.
kind - The kind of Unique ID this instance will deal with.
width - The number of bytes on which Unique IDs should be encoded.
Throws:
IllegalArgumentException - if width is negative or too small/large or if kind is an empty string.
Method Detail

cacheHits

public int cacheHits()
The number of times we avoided reading from HBase thanks to the cache.


cacheMisses

public int cacheMisses()
The number of times we had to read from HBase and populate the cache.


cacheSize

public int cacheSize()
Returns the number of elements stored in the internal cache.


kind

public String kind()
Description copied from interface: UniqueIdInterface
Returns what kind of Unique ID is served by this instance.

Specified by:
kind in interface UniqueIdInterface

width

public short width()
Description copied from interface: UniqueIdInterface
Returns the number of bytes on which each Unique ID is encoded.

Specified by:
width in interface UniqueIdInterface

getName

public String getName(byte[] id)
               throws NoSuchUniqueId,
                      org.hbase.async.HBaseException
Description copied from interface: UniqueIdInterface
Finds the name associated with a given ID.

Specified by:
getName in interface UniqueIdInterface
Parameters:
id - The ID associated with that name.
Throws:
NoSuchUniqueId - if the given ID is not assigned.
org.hbase.async.HBaseException - if there is a problem communicating with HBase.
See Also:
UniqueIdInterface.getId(String), UniqueIdInterface.getOrCreateId(String)

getId

public byte[] getId(String name)
             throws NoSuchUniqueName,
                    org.hbase.async.HBaseException
Description copied from interface: UniqueIdInterface
Finds the ID associated with a given name.

The length of the byte array is fixed in advance by the implementation.

Specified by:
getId in interface UniqueIdInterface
Parameters:
name - The name to lookup in the table.
Returns:
A non-null, non-empty byte[] array.
Throws:
NoSuchUniqueName - if the name requested doesn't have an ID assigned.
org.hbase.async.HBaseException - if there is a problem communicating with HBase.
See Also:
UniqueIdInterface.getName(byte[])

getOrCreateId

public byte[] getOrCreateId(String name)
                     throws org.hbase.async.HBaseException
Description copied from interface: UniqueIdInterface
Finds the ID associated with a given name or creates it.

The length of the byte array is fixed in advance by the implementation.

Specified by:
getOrCreateId in interface UniqueIdInterface
Parameters:
name - The name to lookup in the table or to assign an ID to.
Throws:
org.hbase.async.HBaseException - if there is a problem communicating with HBase.

suggest

public List<String> suggest(String search)
                     throws org.hbase.async.HBaseException
Attempts to find suggestions of names given a search term.

Parameters:
search - The search term (possibly empty).
Returns:
A list of known valid names that have UIDs that sort of match the search term. If the search term is empty, returns the first few terms.
Throws:
org.hbase.async.HBaseException - if there was a problem getting suggestions from HBase.

rename

public void rename(String oldname,
                   String newname)
Reassigns the UID to a different name (non-atomic).

Whatever was the UID of oldname will be given to newname. oldname will no longer be assigned a UID.

Beware that the assignment change is not atommic. If two threads or processes attempt to rename the same UID differently, the result is unspecified and might even be inconsistent. This API is only here for administrative purposes, not for normal programmatic interactions.

Parameters:
oldname - The old name to rename.
newname - The new name.
Throws:
NoSuchUniqueName - if oldname wasn't assigned.
IllegalArgumentException - if newname was already assigned.
org.hbase.async.HBaseException - if there was a problem with HBase while trying to update the mapping.

toString

public String toString()
Returns a human readable string representation of the object.

Overrides:
toString in class Object