private static Scanner setupBranchScanner ( final TSDB tsdb , final byte [ ] branch_id ) { final byte [ ] start = branch_id ; final byte [ ] end = Arrays . copyOf ( branch_id , branch_id . length ) ; final Scanner scanner = tsdb . getClient ( ) . newScanner ( tsdb . treeTable ( ) ) ; scanner . setStartKey ( start ) ; // increment the tree ID so we scan the whole tree byte [ ] tree_id = new byte [ INT_WIDTH ] ; for ( int i = 0 ; i < Tree . TREE_ID_WIDTH ( ) ; i ++ ) { tree_id [ i + ( INT_WIDTH - Tree . TREE_ID_WIDTH ( ) ) ] = end [ i ] ; } int id = Bytes . getInt ( tree_id ) + 1 ; tree_id = Bytes . fromInt ( id ) ; for ( int i = 0 ; i < Tree . TREE_ID_WIDTH ( ) ; i ++ ) { end [ i ] = tree_id [ i + ( INT_WIDTH - Tree . TREE_ID_WIDTH ( ) ) ] ; } scanner . setStopKey ( end ) ; scanner . setFamily ( Tree . TREE_FAMILY ( ) ) ; // TODO - use the column filter to fetch only branches and leaves, ignore // collisions, no matches and other meta // set the regex filter // we want one branch below the current ID so we want something like: // {0, 1, 1, 2, 3, 4 } where { 0, 1 } is the tree ID, { 1, 2, 3, 4 } is the // branch // "^\\Q\000\001\001\002\003\004\\E(?:.{4})$" final StringBuilder buf = new StringBuilder ( ( start . length * 6 ) + 20 ) ; buf . append ( "(?s)" // Ensure we use the DOTALL flag. + "^\\Q" ) ; for ( final byte b : start ) { buf . append ( ( char ) ( b & 0xFF ) ) ; } buf . append ( "\\E(?:.{" ) . append ( INT_WIDTH ) . append ( "})?$" ) ; scanner . setKeyRegexp ( buf . toString ( ) , CHARSET ) ; return scanner ; }
Configures an HBase scanner to fetch the requested branch and all child branches . It uses a row key regex filter to match any rows starting with the given branch and another INT_WIDTH bytes deep . Deeper branches are ignored .