org.cdlib.xtf.util
Class TagArray

Object
  extended by TagArray

public class TagArray
extends Object

This class implements a resizable container for Tags, which are short typed strings that are efficiently stored. The main goal for the class is to hold millions of small strings efficiently, without loading down the garbage collector with millions of objects. Strings are stored together in large buffers rather than individual objects.

Author:
Martin Haye

Field Summary
private  int BLOCK_SIZE
           
private  byte[][] blocks
           
private  byte[] curBlock
           
private  int curBlockUsed
           
private  short nBlocks
           
private  int nTags
           
private  int nTypes
           
private  short[] tagBlock
           
private  short[] tagLength
           
private  int[] tagOffset
           
private  short[] tagSubType
           
private  byte[] tagType
           
static Tester tester
          Basic regression test
private  Hashtable typeTable
           
 
Constructor Summary
TagArray()
           
 
Method Summary
 int add(String str, int type)
          Add a tag to the array
 int add(String str, int type, int subType)
          Add a tag to the array
private  int allocateID()
          Allocate a new string identifier, expanding the arrays if necessary.
 long byteSize()
          Calculate the size in bytes of the major structures of the tag array.
private  void ensureCapacity(int nBytes)
          Checks if the current block has space for the given number of bytes.
 int findType(String t)
          Allocates a number for the given type string.
 TagChars getChars(int tag)
          Get the characters that make up a given tag.
 TagChars getChars(int tag, TagChars chars)
          Get the characters that make up a given tag, with no object allocation at all.
 String getString(int tag)
          Get the string value of the given tag
 short getSubType(int tag)
          Get the sub-type associated with a given tag (zero if none)
 int getType(int tag)
          Get the type associated with a given tag.
private  void newBlock()
          Allocates a new block, and sets curBlock to point at it.
 int next(int tag)
          Retrieve the next tag in order, if its type is the same as the given one, or -1 if there is no such tag.
 int prev(int tag)
          Retrieve the previous tag in order, if its type is the same as the given one, or -1 if there is no such tag.
 int size()
          Retrieve a count of how many tags have been added.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

private int BLOCK_SIZE

blocks

private byte[][] blocks

nBlocks

private short nBlocks

curBlock

private byte[] curBlock

curBlockUsed

private int curBlockUsed

tagType

private byte[] tagType

tagSubType

private short[] tagSubType

tagBlock

private short[] tagBlock

tagOffset

private int[] tagOffset

tagLength

private short[] tagLength

nTags

private int nTags

typeTable

private Hashtable typeTable

nTypes

private int nTypes

tester

public static final Tester tester
Basic regression test

Constructor Detail

TagArray

public TagArray()
Method Detail

findType

public int findType(String t)
Allocates a number for the given type string.

Parameters:
t - The type to find
Returns:
Its type ID

add

public final int add(String str,
                     int type)
Add a tag to the array

Parameters:
str - The string to add
type - The type ID of the new tag (from findType(String))
Returns:
The new Tag's identifier

add

public int add(String str,
               int type,
               int subType)
Add a tag to the array

Parameters:
str - The string to add
type - The type ID of the new tag (from findType(String))
subType - The sub-type of the new tag (or 0 for none)
Returns:
The new Tag's identifier

size

public int size()
Retrieve a count of how many tags have been added.


getString

public String getString(int tag)
Get the string value of the given tag


getChars

public final TagChars getChars(int tag)
Get the characters that make up a given tag. Allocates a new place to hold the value, but simply points to the data rather than copying it.


getChars

public TagChars getChars(int tag,
                         TagChars chars)
Get the characters that make up a given tag, with no object allocation at all.

Parameters:
tag - The tag to get the value of
chars - Where to store the pointer
Returns:
The same value of 'chars', for handy chaining

getType

public int getType(int tag)
Get the type associated with a given tag.


getSubType

public short getSubType(int tag)
Get the sub-type associated with a given tag (zero if none)


next

public int next(int tag)
Retrieve the next tag in order, if its type is the same as the given one, or -1 if there is no such tag.


prev

public int prev(int tag)
Retrieve the previous tag in order, if its type is the same as the given one, or -1 if there is no such tag.


byteSize

public long byteSize()
Calculate the size in bytes of the major structures of the tag array.

Returns:
Approximate size in bytes

allocateID

private int allocateID()
Allocate a new string identifier, expanding the arrays if necessary.

Returns:
The new identifier

ensureCapacity

private void ensureCapacity(int nBytes)
Checks if the current block has space for the given number of bytes. If not, adds a new block.

Parameters:
nBytes - How many bytes to check for

newBlock

private void newBlock()
Allocates a new block, and sets curBlock to point at it.