@Override public BPTreeRecords createFromBlock(Block block, BlockType bType) { RecordBufferPage rbp = recordBufferConverter.createFromBlock(block, bType) ; return new BPTreeRecords(recordsMgr, rbp) ; } }
@Override final public Record internalMaxRecord(AccessPath path) { return getHighRecord() ; }
@Override final public Record internalMinRecord(AccessPath path) { return getLowRecord() ; }
protected static void check(BPTreeRecords bpr) { assertTrue(bpr.getCount() >= 0) ; assertTrue(bpr.getCount() <= bpr.getMaxSize()) ; assertEquals(bpr.getRecordBuffer().getLow(), bpr.getLowRecord()) ; assertEquals(bpr.getRecordBuffer().getHigh(), bpr.getHighRecord()) ; for ( int i = 1 ; i < bpr.getCount() ; i++ ) { Record r1 = bpr.getRecordBuffer().get(i - 1) ; Record r2 = bpr.getRecordBuffer().get(i) ; assertTrue(Record.keyLE(r1, r2)) ; } }
@Test public void bpt_records_5() { BPTreeRecords bpr = make() ; int N = bpr.getMaxSize() ; for ( int i = bpr.getMaxSize() - 1 ; i >= 0 ; i-- ) insert(bpr, (i + 0x20)) ; delete(bpr, (1 + 0x20)) ; assertEquals(N - 1, bpr.getCount()) ; check(bpr) ; delete(bpr, (2 + 0x20)) ; assertEquals(N - 2, bpr.getCount()) ; check(bpr) ; delete(bpr, bpr.getLowRecord()) ; assertEquals(N - 3, bpr.getCount()) ; check(bpr) ; delete(bpr, bpr.getHighRecord()) ; assertEquals(N - 4, bpr.getCount()) ; check(bpr) ; bpr.release() ; }
@Test public void bpt_records_2() { BPTreeRecords bpr = make() ; fill(bpr) ; int s = bpr.getCount() ; assertTrue(bpr.isFull()) ; BPTreePage z = bpr.split() ; assertTrue(z instanceof BPTreeRecords) ; assertEquals(s, z.getCount() + bpr.getCount()) ; check(bpr) ; check((BPTreeRecords)z) ; bpr.release() ; z.release() ; }
@Test public void bpt_records_6() { BPTreeRecords bpr = make() ; fill(bpr) ; // No match. assertNull(search(bpr, RecordLib.intToRecord(0x20))) ; Record r = RecordLib.intToRecord(0x32) ; Record r2 = search(bpr, r) ; assertTrue(Record.keyEQ(r, r2)) ; r = bpr.getLowRecord() ; r2 = search(bpr, r) ; assertTrue(Record.keyEQ(r, r2)) ; r = bpr.getHighRecord() ; r2 = search(bpr, r) ; assertTrue(Record.keyEQ(r, r2)) ; bpr.release() ; }
@Test public void bpt_merge_1() { BPTreeRecords bpr1 = make() ; BPTreeRecords bpr2 = make() ; insert(bpr1, 10, 20) ; insert(bpr2, 99) ; BPTreeRecords bpr3 = (BPTreeRecords)bpr1.merge(bpr2, null) ; contains(bpr1, 10, 20, 99) ; contains(bpr2) ; assertSame(bpr1, bpr3) ; bpr1.release() ; bpr2.release() ; }
private static void contains(BPTreeRecords bpr, int... values) { assertEquals(values.length, bpr.getCount()) ; for ( int i = 1 ; i < values.length ; i++ ) assertTrue(Record.compareByKeyValue(RecordLib.intToRecord(values[i]), bpr.getRecordBuffer().get(i)) == 0) ; }
@Test public void bpt_records_3() { BPTreeRecords bpr = make() ; for ( int i = 0 ; bpr.getCount() < bpr.getMaxSize() ; i++ ) insert(bpr, (i + 0x20)) ; check(bpr) ; bpr.release() ; }
/** * Split: place old high half in 'other'. Return the new (upper) * BPTreeRecords(BPTreePage). * Split is the high end of the low page. */ @Override public BPTreePage split() { BPTreeRecords other = insertNewPage() ; int splitIdx = rBuff.size() / 2 - 1 ; Record r = rBuff.get(splitIdx) ; // Only need key for checking later. int moveLen = rBuff.size() - (splitIdx + 1) ; // Number to move. // Copy high end to new. rBuff.copy(splitIdx + 1, other.getRecordBufferPage().getRecordBuffer(), 0, moveLen) ; rBuff.clear(splitIdx + 1, moveLen) ; rBuff.setSize(splitIdx + 1) ; if ( CheckingNode ) { if ( !Record.keyEQ(r, maxRecord()) ) { System.err.println(rBuff) ; System.err.println(other.rBuff) ; error("BPTreeRecords.split: Not returning expected record") ; } } return other ; }
private static<X> Iterator<X> getRecordsIterator(BPTreeRecords records, Record minRecord, Record maxRecord, RecordMapper<X> mapper) { records.bpTree.startReadBlkMgr(); Iterator<X> iter = records.getRecordBuffer().iterator(minRecord, maxRecord, mapper); records.bpTree.finishReadBlkMgr(); return iter ; }
@Override public final void checkNode() { if ( !CheckingNode ) return ; if ( rBuff.size() < 0 || rBuff.size() > rBuff.maxSize() ) error("Misized: %s", this) ; for ( int i = 1 ; i < getCount() ; i++ ) { Record r1 = rBuff.get(i - 1) ; Record r2 = rBuff.get(i) ; if ( Record.keyGT(r1, r2) ) error("Not sorted: %s", this) ; } }
@Override public Record shiftRight(BPTreePage other, Record splitKey) { // Error checking by RecordBuffer BPTreeRecords page = cast(other) ; rBuff.shiftRight(page.rBuff) ; if ( rBuff.size() == 0 ) return null ; return rBuff.getHigh() ; }
private BPTreeRecords insertNewPage() { // MVCC - link can not be maintained. if ( false /* ! bpTree.isTransaction() */ ) { BPTreeRecords other = create(rBuffPage.getLink()) ; rBuffPage.setLink(other.getId()) ; return other ; } BPTreeRecords other = create(Page.NO_ID) ; rBuffPage.setLink(Page.NO_ID) ; return other ; }
private static BPTreeRecords cast(BPTreePage page) { try { return (BPTreeRecords)page ; } catch (ClassCastException ex) { error("Wrong type: " + page) ; return null ; } }
@Override public final void checkNodeDeep() { checkNode() ; }
protected static void check(BPTreeRecords bpr) { assertTrue(bpr.getCount() >= 0) ; assertTrue(bpr.getCount() <= bpr.getMaxSize()) ; assertEquals(bpr.getRecordBuffer().getLow(), bpr.getLowRecord()) ; assertEquals(bpr.getRecordBuffer().getHigh(), bpr.getHighRecord()) ; for ( int i = 1 ; i < bpr.getCount() ; i++ ) { Record r1 = bpr.getRecordBuffer().get(i - 1) ; Record r2 = bpr.getRecordBuffer().get(i) ; assertTrue(Record.keyLE(r1, r2)) ; } }
@Test public void bpt_records_5() { BPTreeRecords bpr = make() ; int N = bpr.getMaxSize() ; for ( int i = bpr.getMaxSize() - 1 ; i >= 0 ; i-- ) insert(bpr, (i + 0x20)) ; delete(bpr, (1 + 0x20)) ; assertEquals(N - 1, bpr.getCount()) ; check(bpr) ; delete(bpr, (2 + 0x20)) ; assertEquals(N - 2, bpr.getCount()) ; check(bpr) ; delete(bpr, bpr.getLowRecord()) ; assertEquals(N - 3, bpr.getCount()) ; check(bpr) ; delete(bpr, bpr.getHighRecord()) ; assertEquals(N - 4, bpr.getCount()) ; check(bpr) ; bpr.release() ; }