public BTreeNode<Key,Value> getFirstLeafNode(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node .isBranch() ) { node = node.getChild(tx, 0); } return node; }
/** * Returns the right most leaf from the current btree graph. * @throws IOException */ private BTreeNode<Key,Value> getRightLeaf(Transaction tx) throws IOException { BTreeNode<Key,Value> cur = this; while(cur.isBranch()) { cur = cur.getChild(tx, cur.keys.length); } return cur; }
/** * Returns the left most leaf from the current btree graph. * @throws IOException */ private BTreeNode<Key,Value> getLeftLeaf(Transaction tx) throws IOException { BTreeNode<Key,Value> cur = this; while(cur.isBranch()) { cur = cur.getChild(tx, 0); } return cur; }
private static <Key,Value> BTreeNode<Key, Value> getLeafNode(Transaction tx, final BTreeNode<Key, Value> node, Key key) throws IOException { BTreeNode<Key, Value> current = node; while( true ) { if( current.isBranch() ) { int idx = Arrays.binarySearch(current.keys, key); idx = idx < 0 ? -(idx + 1) : idx + 1; BTreeNode<Key, Value> child = current.getChild(tx, idx); // A little cycle detection for sanity's sake if( child == node ) { throw new IOException("BTree corrupted: Cylce detected."); } current = child; } else { break; } } return current; }
public int getMinLeafDepth(Transaction tx, int depth) throws IOException { depth++; if( isBranch() ) { int min = Integer.MAX_VALUE; for(int i=0 ; i < children.length; i++) { min = Math.min(min, getChild(tx, i).getMinLeafDepth(tx, depth)); } return min; } else { // print(depth*2, "- "+page.getPageId()); return depth; } }
public int getMaxLeafDepth(Transaction tx, int depth) throws IOException { depth++; if( isBranch() ) { int v = 0; for(int i=0 ; i < children.length; i++) { v = Math.max(v, getChild(tx, i).getMaxLeafDepth(tx, depth)); } depth = v; } return depth; }
public Map.Entry<Key,Value> getFirst(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node .isBranch() ) { node = node.getChild(tx, 0); } if( node.values.length>0 ) { return new KeyValueEntry(node.keys[0], node.values[0]); } else { return null; } }
public Map.Entry<Key,Value> getLast(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node.isBranch() ) { node = node.getChild(tx, node.children.length-1); } if( node.values.length>0 ) { int idx = node.values.length-1; return new KeyValueEntry(node.keys[idx], node.values[idx]); } else { return null; } }
public void visit(Transaction tx, BTreeVisitor<Key, Value> visitor) throws IOException { if (visitor == null) { throw new IllegalArgumentException("Visitor cannot be null"); } if( isBranch() ) { for(int i=0; i < this.children.length; i++) { Key key1 = null; if( i!=0 ) { key1 = keys[i-1]; } Key key2 = null; if( i!=this.children.length-1 ) { key2 = keys[i]; } if( visitor.isInterestedInKeysBetween(key1, key2) ) { BTreeNode<Key, Value> child = getChild(tx, i); child.visit(tx, visitor); } } } else { visitor.visit(Arrays.asList(keys), Arrays.asList(values)); } }
/** * Returns the left most leaf from the current btree graph. * @throws IOException */ private BTreeNode<Key,Value> getLeftPeer(Transaction tx, BTreeNode<Key,Value> x) throws IOException { BTreeNode<Key,Value> cur = x; while( cur.parent !=null ) { if( cur.parent.children[0] == cur.getPageId() ) { cur = cur.parent; } else { for( int i=0; i < cur.parent.children.length; i ++) { if( cur.parent.children[i]==cur.getPageId() ) { return cur.parent.getChild(tx, i-1); } } throw new AssertionError("page "+x+" was decendent of "+cur.getPageId()); } } return null; }
public void printStructure(Transaction tx, PrintWriter out, String prefix) throws IOException { if( prefix.length()>0 && parent == null ) { throw new IllegalStateException("Cycle back to root node detected."); } if (parent == null) { prefix += "|"; out.println(prefix + getPageId()); } if( isBranch() ) { for(int i=0 ; i < children.length; i++) { BTreeNode<Key, Value> child = getChild(tx, i); if( i == children.length-1) { out.println(prefix+"\\- "+child.getPageId()+(child.isBranch()?" ("+child.children.length+")":"")); child.printStructure(tx, out, prefix+" "); } else { out.println(prefix+"|- "+child.getPageId()+(child.isBranch()?" ("+child.children.length+")":"")+" : "+keys[i]); child.printStructure(tx, out, prefix+" "); } } } }
int idx = Arrays.binarySearch(keys, key); idx = idx < 0 ? -(idx + 1) : idx + 1; BTreeNode<Key, Value> child = getChild(tx, idx); if( child.getPageId() == index.getPageId() ) { throw new IOException("BTree corrupted: Cycle detected."); if( idx > 0 ) { previousLeaf = getChild(tx, idx-1).getRightLeaf(tx); } else { child = getChild(tx, 0); keys = child.keys; children = child.children;
/** * Returns the right most leaf from the current btree graph. * @throws IOException */ private BTreeNode<Key,Value> getRightLeaf(Transaction tx) throws IOException { BTreeNode<Key,Value> cur = this; while(cur.isBranch()) { cur = cur.getChild(tx, cur.keys.length); } return cur; }
/** * Returns the left most leaf from the current btree graph. * @throws IOException */ private BTreeNode<Key,Value> getLeftLeaf(Transaction tx) throws IOException { BTreeNode<Key,Value> cur = this; while(cur.isBranch()) { cur = cur.getChild(tx, 0); } return cur; }
public int getMaxLeafDepth(Transaction tx, int depth) throws IOException { depth++; if( isBranch() ) { int v = 0; for(int i=0 ; i < children.length; i++) { v = Math.max(v, getChild(tx, i).getMaxLeafDepth(tx, depth)); } depth = v; } return depth; }
public int getMaxLeafDepth(Transaction tx, int depth) throws IOException { depth++; if( isBranch() ) { int v = 0; for(int i=0 ; i < children.length; i++) { v = Math.max(v, getChild(tx, i).getMaxLeafDepth(tx, depth)); } depth = v; } return depth; }
public int getMinLeafDepth(Transaction tx, int depth) throws IOException { depth++; if( isBranch() ) { int min = Integer.MAX_VALUE; for(int i=0 ; i < children.length; i++) { min = Math.min(min, getChild(tx, i).getMinLeafDepth(tx, depth)); } return min; } else { // print(depth*2, "- "+page.getPageId()); return depth; } }
public Map.Entry<Key,Value> getFirst(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node .isBranch() ) { node = node.getChild(tx, 0); } if( node.values.length>0 ) { return new KeyValueEntry(node.keys[0], node.values[0]); } else { return null; } }
public Map.Entry<Key,Value> getLast(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node.isBranch() ) { node = node.getChild(tx, node.children.length-1); } if( node.values.length>0 ) { int idx = node.values.length-1; return new KeyValueEntry(node.keys[idx], node.values[idx]); } else { return null; } }
public Map.Entry<Key,Value> getLast(Transaction tx) throws IOException { BTreeNode<Key, Value> node = this; while( node.isBranch() ) { node = node.getChild(tx, node.children.length-1); } if( node.values.length>0 ) { int idx = node.values.length-1; return new KeyValueEntry(node.keys[idx], node.values[idx]); } else { return null; } }