net.opentsdb.tree
public final class Branch extends Object implements Comparable<Branch>
Branch IDs are hex encoded byte arrays composed of the tree ID + hash of
the display name for each previous branch. The tree ID is encoded on
Tree.TREE_ID_WIDTH()
bytes, each hash is then INT_WIDTH
bytes. So the if the tree ID width is 2 bytes and Java Integers are 4 bytes,
the root for tree # 1 is just 0001
. A child of the root could be
00001A3B190C2
and so on. These IDs are used as the row key in storage.
Branch definitions are JSON objects stored in the "branch" column of the branch ID row. Only the tree ID, path and display name are stored in the definition column to keep space down. Leaves are stored in separate columns and child branch definitions are stored in separate rows. Note that the root branch definition for a tree will be stored in the same row as the tree definition since they share the same row key.
When fetching a branch with children and leaves, a scanner is
configured with a row key regex to scan any rows that match the branch ID
plus an additional INT_WIDTH
so that when we scan, we can pick up all
of the rows with child branch definitions. Also, when loading a full branch,
any leaves for the request branch can load the associated UID names from
storage, so this can get expensive. Leaves for a child branch will not be
loaded, only leaves that belong directly to the local will. Also, children
branches of children will not be loaded. We only return one branch at a
time since the tree could be HUGE!
Storing a branch will only write the definition column for the local branch object. Child branches will not be written to storage. If you've loaded and modified children in this branch, you need to loop through the children and store them individually. Leaves belonging to this branch will be stored and collisions recorded to the given Tree object.
Constructor and Description |
---|
Branch()
Default empty constructor necessary for de/serialization
|
Branch(Branch original)
Copy constructor that creates a completely independent copy of the original
|
Branch(int tree_id)
Constructor that sets the tree ID
|
Modifier and Type | Method and Description |
---|---|
boolean |
addChild(Branch branch)
Adds a child branch to the local branch set if it doesn't exist.
|
boolean |
addLeaf(Leaf leaf,
Tree tree)
Adds a leaf to the local branch, looking for collisions
|
static byte[] |
BRANCH_QUALIFIER() |
int |
compareTo(Branch branch)
Comparator based on the
display_name to sort branches when
returning to an RPC calls |
byte[] |
compileBranchId()
Attempts to compile the branch ID for this branch.
|
boolean |
equals(Object obj)
Just compares the branch display name
|
static com.stumbleupon.async.Deferred<Branch> |
fetchBranch(TSDB tsdb,
byte[] branch_id,
boolean load_leaf_uids)
Attempts to fetch the branch, it's leaves and all child branches.
|
static com.stumbleupon.async.Deferred<Branch> |
fetchBranchOnly(TSDB tsdb,
byte[] branch_id)
Attempts to fetch only the branch definition object from storage.
|
TreeSet<Branch> |
getBranches() |
String |
getBranchId() |
int |
getDepth() |
String |
getDisplayName() |
TreeSet<Leaf> |
getLeaves() |
Map<Integer,String> |
getPath() |
int |
getTreeId() |
int |
hashCode() |
static String |
idToString(byte[] branch_id)
Converts a branch ID hash to a hex encoded, upper case string with padding
|
void |
prependParentPath(Map<Integer,String> parent_path)
Sets the path for this branch based off the path of the parent.
|
void |
setDisplayName(String display_name) |
void |
setTreeId(int tree_id) |
com.stumbleupon.async.Deferred<ArrayList<Boolean>> |
storeBranch(TSDB tsdb,
Tree tree,
boolean store_leaves)
Attempts to write the branch definition and optionally child leaves to
storage via CompareAndSets.
|
static byte[] |
stringToId(String branch_id)
Converts a hex string to a branch ID byte array (row key)
|
String |
toString() |
public Branch()
public Branch(int tree_id)
tree_id
- ID of the tree this branch is associated withpublic Branch(Branch original)
original
- The original object to copy frompublic int hashCode()
public boolean equals(Object obj)
public int compareTo(Branch branch)
display_name
to sort branches when
returning to an RPC callscompareTo
in interface Comparable<Branch>
public String toString()
public boolean addChild(Branch branch)
branch
- The branch to addIllegalArgumentException
- if the incoming branch is nullpublic boolean addLeaf(Leaf leaf, Tree tree)
leaf
- The leaf to addtree
- The tree to report to with collisionsIllegalArgumentException
- if the incoming leaf is nullpublic byte[] compileBranchId()
tree_id
, path
and display_name
must
be set. The path may be empty, which indicates this is a root branch, but
it must be a valid Map object.IllegalArgumentException
- if any required parameters are missingpublic void prependParentPath(Map<Integer,String> parent_path)
parent_path
- The map to store as the pathIllegalArgumentException
- if the parent path is nullpublic com.stumbleupon.async.Deferred<ArrayList<Boolean>> storeBranch(TSDB tsdb, Tree tree, boolean store_leaves)
tsdb
- The TSDB to use for accesstree
- The tree to record collisions tostore_leaves
- Whether or not child leaves should be written to
storageorg.hbase.async.HBaseException
- if there was an issueIllegalArgumentException
- if the tree ID was missing or data was
missingpublic static com.stumbleupon.async.Deferred<Branch> fetchBranchOnly(TSDB tsdb, byte[] branch_id)
fetchBranch(net.opentsdb.core.TSDB, byte[], boolean)
call. Useful when building trees, particularly to
fetch the root branch.tsdb
- The TSDB to use for accessbranch_id
- ID of the branch to retrieveJSONException
- if the object could not be deserializedpublic static com.stumbleupon.async.Deferred<Branch> fetchBranch(TSDB tsdb, byte[] branch_id, boolean load_leaf_uids)
tsdb
- The TSDB to use for storage accessbranch_id
- ID of the branch to retrieveload_leaf_uids
- Whether or not to load UID names for each leafJSONException
- if the object could not be deserializedpublic static String idToString(byte[] branch_id)
branch_id
- The ID to convertpublic static byte[] stringToId(String branch_id)
branch_id
- The branch ID to convertIllegalArgumentException
- if the string is not valid hexpublic static byte[] BRANCH_QUALIFIER()
public int getTreeId()
public String getBranchId()
public int getDepth()
public String getDisplayName()
public void setTreeId(int tree_id)
tree_id
- ID of the tree this branch belongs topublic void setDisplayName(String display_name)
display_name
- Public name to display