/** Create the java structures to correspond to * the supplied block managers for the persistent storage. * Initialize the persistent storage to the empty B+Tree if it does not exist. */ public static BPlusTree createNonTxn(BPlusTreeParams params, BufferChannel chan, BlockMgr blkMgrNodes, BlockMgr blkMgrLeaves) { // Allocate a random ComponentId BPlusTree bpt = create(null, params, chan, blkMgrNodes, blkMgrLeaves) ; bpt.nonTransactional() ; return bpt ; }
@Override public void clear() { Record[] records = new Record[SLICE] ; while(true) { Iterator<Record> iter = iterator() ; int i = 0 ; for ( i = 0 ; i < SLICE ; i++ ) { if ( ! iter.hasNext() ) break ; Record r = iter.next() ; records[i] = r ; } if ( i == 0 ) break ; for ( int j = 0 ; j < i ; j++ ) { delete(records[j]) ; records[j] = null ; } } }
@Override public boolean contains(Record record) { Record r = find(record) ; return r != null ; }
@Override public <X> Iterator<X> iterator(Record minRec, Record maxRec, RecordMapper<X> mapper) { startReadBlkMgr() ; BPTreeNode root = getRootRead() ; releaseRootRead(root) ; finishReadBlkMgr() ; return iterator(root, minRec, maxRec, mapper); }
/** Reset an existing B+Tree with different storage units. * For each, null means "use same as original" */ public static BPlusTree rebuild(BPlusTree bpt, BufferChannel chan, BlockMgr blkMgrNodes, BlockMgr blkMgrLeaves) { if ( chan == null ) chan = bpt.getStateManager().getBufferChannel() ; if ( blkMgrNodes == null ) blkMgrNodes = bpt.getNodeManager().getBlockMgr() ; if ( blkMgrLeaves == null ) blkMgrLeaves = bpt.getNodeManager().getBlockMgr() ; BPlusTree bpt2 = attach(bpt.getComponentId(), bpt.getParams(), true, chan, blkMgrNodes, blkMgrLeaves) ; return bpt2 ; }
@Override protected void runOneTest(int testCount, int order, int size) { // //System.err.println("runOneTest("+order+","+size+")") ; BPlusTree bpt = BPlusTreeFactory.makeMem(order, Sys.SizeOfInt, 0) ; bpt = BPlusTreeFactory.addTracking(bpt) ; bpt.nonTransactional() ; IndexTestLib.randTest(bpt, 5*size, size, true); bpt.close() ; // Transaction. // BPlusTree bpt = BPlusTreeFactory.makeMem(order, SystemLz.SizeOfInt, 0) ; // Journal journal = Journal.create(Location.mem()) ; // Transactional holder = TransactionalFactory.create(journal, bpt) ; // holder.begin(ReadWrite.WRITE); // IndexTestLib.randTest(bpt, 5*size, size, true); // holder.commit() ; // holder.end() ; } }
@Test public void bptree_txn_02() { BPlusTree bpt = createBPTree() ; int outerRootIdx1 = bpt.getRootId() ; Transactional thing = transactional(bpt) ; Txn.executeWrite(thing, () -> { int rootIdx1 = bpt.getRootId() ; assertEquals("Inside txn (1)", outerRootIdx1, rootIdx1); IndexTestLib.add(bpt, 1) ; int rootIdx2 = bpt.getRootId() ; assertNotEquals("Inside txn (2)", rootIdx1, rootIdx2); IndexTestLib.add(bpt, 2, 3, 4) ; int rootIdx3 = bpt.getRootId() ; assertEquals("Inside txn (3)", rootIdx2, rootIdx3); } ) ; int outerRootIdx2 = bpt.getRootId() ; assertNotEquals("After txn", outerRootIdx1, outerRootIdx2); }
@Test public void bptree_txn_01() { BPlusTree bpt = createBPTree() ; assertNotNull(bpt.getComponentId()) ; int outerRootIdx1 = bpt.getRootId() ; Transactional thing = transactional(bpt) ; Txn.executeWrite(thing, () -> { IndexTestLib.add(bpt, 1, 2, 3, 4) ; } ); int outerRootIdx2 = bpt.getRootId() ; assertNotEquals("After txn", outerRootIdx1, outerRootIdx2); }
protected void testClearX(int N) { int[] keys = new int[N] ; // Slice is 1000. for ( int i = 0 ; i < keys.length ; i++ ) keys[i] = i ; BPlusTree rIndex = makeRangeIndex(2, 2) ; add(rIndex, keys) ; rIndex.dump() ; if ( N > 0 ) assertFalse(rIndex.isEmpty()) ; List<Record> x = intToRecord(keys, RecordLib.TestRecordLength) ; for ( int i = 0 ; i < keys.length ; i++ ) { System.out.println(i+": "+x.get(i)) ; rIndex.delete(x.get(i)) ; } assertTrue(rIndex.isEmpty()) ; }
BPlusTree bpt = new BPlusTree(cid, params) ; BPTStateMgr stateMgr = new BPTStateMgr(rootData) ; blkMgrNodes.resetAlloc(stateMgr.getNodeBlocksLimit()); bpt.init(stateMgr, nodeManager, recordsMgr) ; if ( BPT.CheckingNode ) { nodeManager.startRead(); BPTreeNode root = nodeManager.getRead(bpt.getRootId(), BPlusTreeParams.RootParent) ; root.checkNodeDeep() ; nodeManager.release(root) ;
@Override public Iterator<Record> iterator() { return iterator(noMin, noMax) ; }
@Test public void bptree_txn_10() { BPlusTree bpt1 = createBPTree() ; BPlusTree bpt2 = createBPTree() ; assertNotEquals(bpt1.getComponentId(), bpt2.getComponentId()) ; Transactional thing = transactional(bpt1, bpt2) ; Txn.executeWrite(thing, () -> { IndexTestLib.add(bpt1, 1, 2, 3) ; IndexTestLib.add(bpt2, 4, 5) ; } ); Txn.executeRead(thing, ()->{ IndexTestLib.testIndexContents(bpt2, 4, 5); IndexTestLib.testIndexContents(bpt1, 1, 2, 3); } ); }
@Override public <X> Iterator<X> iterator(Record minRec, Record maxRec, RecordMapper<X> mapper) { startReadBlkMgr() ; BPTreeNode root = getRootRead() ; releaseRootRead(root) ; finishReadBlkMgr() ; return iterator(root, minRec, maxRec, mapper); }
/** Reset an existing B+Tree with different storage units. * For each, null means "use same as original" */ public static BPlusTree rebuild(BPlusTree bpt, BufferChannel chan, BlockMgr blkMgrNodes, BlockMgr blkMgrLeaves) { if ( chan == null ) chan = bpt.getStateManager().getBufferChannel() ; if ( blkMgrNodes == null ) blkMgrNodes = bpt.getNodeManager().getBlockMgr() ; if ( blkMgrLeaves == null ) blkMgrLeaves = bpt.getNodeManager().getBlockMgr() ; BPlusTree bpt2 = attach(bpt.getComponentId(), bpt.getParams(), true, chan, blkMgrNodes, blkMgrLeaves) ; return bpt2 ; }
@Test public void bptree_txn_02() { BPlusTree bpt = createBPTree() ; int outerRootIdx1 = bpt.getRootId() ; Transactional thing = transactional(bpt) ; Txn.executeWrite(thing, () -> { int rootIdx1 = bpt.getRootId() ; assertEquals("Inside txn (1)", outerRootIdx1, rootIdx1); IndexTestLib.add(bpt, 1) ; int rootIdx2 = bpt.getRootId() ; assertNotEquals("Inside txn (2)", rootIdx1, rootIdx2); IndexTestLib.add(bpt, 2, 3, 4) ; int rootIdx3 = bpt.getRootId() ; assertEquals("Inside txn (3)", rootIdx2, rootIdx3); } ) ; int outerRootIdx2 = bpt.getRootId() ; assertNotEquals("After txn", outerRootIdx1, outerRootIdx2); }
@Test public void bptree_txn_01() { BPlusTree bpt = createBPTree() ; assertNotNull(bpt.getComponentId()) ; int outerRootIdx1 = bpt.getRootId() ; Transactional thing = transactional(bpt) ; Txn.executeWrite(thing, () -> { IndexTestLib.add(bpt, 1, 2, 3, 4) ; } ); int outerRootIdx2 = bpt.getRootId() ; assertNotEquals("After txn", outerRootIdx1, outerRootIdx2); }
protected void testClearX(int N) { int[] keys = new int[N] ; // Slice is 1000. for ( int i = 0 ; i < keys.length ; i++ ) keys[i] = i ; BPlusTree rIndex = makeRangeIndex(2, 2) ; add(rIndex, keys) ; rIndex.dump() ; if ( N > 0 ) assertFalse(rIndex.isEmpty()) ; List<Record> x = intToRecord(keys, RecordLib.TestRecordLength) ; for ( int i = 0 ; i < keys.length ; i++ ) { System.out.println(i+": "+x.get(i)) ; rIndex.delete(x.get(i)) ; } assertTrue(rIndex.isEmpty()) ; }
BPlusTree bpt = new BPlusTree(cid, params) ; BPTStateMgr stateMgr = new BPTStateMgr(rootData) ; blkMgrNodes.resetAlloc(stateMgr.getNodeBlocksLimit()); bpt.init(stateMgr, nodeManager, recordsMgr) ; if ( BPT.CheckingNode ) { nodeManager.startRead(); BPTreeNode root = nodeManager.getRead(bpt.getRootId(), BPlusTreeParams.RootParent) ; root.checkNodeDeep() ; nodeManager.release(root) ;
@Override public Iterator<Record> iterator() { return iterator(noMin, noMax) ; }
@Test public void bptree_txn_10() { BPlusTree bpt1 = createBPTree() ; BPlusTree bpt2 = createBPTree() ; assertNotEquals(bpt1.getComponentId(), bpt2.getComponentId()) ; Transactional thing = transactional(bpt1, bpt2) ; Txn.executeWrite(thing, () -> { IndexTestLib.add(bpt1, 1, 2, 3) ; IndexTestLib.add(bpt2, 4, 5) ; } ); Txn.executeRead(thing, ()->{ IndexTestLib.testIndexContents(bpt2, 4, 5); IndexTestLib.testIndexContents(bpt1, 1, 2, 3); } ); }