1 21 package com.db4o.db4ounit.common.btree; 22 23 import com.db4o.*; 24 import com.db4o.db4ounit.common.foundation.IntArrays4; 25 import com.db4o.inside.btree.*; 26 27 import db4ounit.extensions.AbstractDb4oTestCase; 28 import db4ounit.extensions.fixtures.*; 29 30 public class BTreeSearchTestCase extends AbstractDb4oTestCase implements 31 OptOutDefragSolo, OptOutCS { 32 33 protected static final int BTREE_NODE_SIZE = 4; 34 35 public static void main(String [] arguments) { 36 new BTreeSearchTestCase().runSolo(); 37 } 38 39 public void test() throws Exception { 40 cycleIntKeys(new int[] { 3, 5, 5, 5, 7, 10, 11, 12, 12, 14 }); 41 cycleIntKeys(new int[] { 3, 5, 5, 5, 5, 7, 10, 11, 12, 12, 14 }); 42 cycleIntKeys(new int[] { 3, 5, 5, 5, 5, 5, 7, 10, 11, 12, 12, 14 }); 43 cycleIntKeys(new int[] { 3, 3, 5, 5, 5, 7, 10, 11, 12, 12, 14, 14 }); 44 cycleIntKeys(new int[] { 3, 3, 3, 5, 5, 5, 7, 10, 11, 12, 12, 14, 14, 45 14 }); 46 } 47 48 private void cycleIntKeys(int[] values) throws Exception { 49 BTree btree = BTreeAssert.createIntKeyBTree(stream(), 0, 50 BTREE_NODE_SIZE); 51 for (int i = 0; i < 5; i++) { 52 btree = cycleIntKeys(btree, values); 53 } 54 } 55 56 private BTree cycleIntKeys(BTree btree, int[] values) throws Exception { 57 for (int i = 0; i < values.length; i++) { 58 btree.add(trans(), new Integer (values[i])); 59 } 60 expectKeysSearch(trans(), btree, values); 61 62 btree.commit(trans()); 63 64 int id = btree.getID(); 65 66 stream().commit(); 67 68 reopen(); 69 70 btree = BTreeAssert.createIntKeyBTree(stream(), id, BTREE_NODE_SIZE); 71 72 expectKeysSearch(trans(), btree, values); 73 74 for (int i = 0; i < values.length; i++) { 75 btree.remove(trans(), new Integer (values[i])); 76 } 77 78 BTreeAssert.assertEmpty(trans(), btree); 79 80 btree.commit(trans()); 81 82 BTreeAssert.assertEmpty(trans(), btree); 83 84 return btree; 85 } 86 87 private void expectKeysSearch(Transaction trans, BTree btree, int[] keys) { 88 int lastValue = Integer.MIN_VALUE; 89 for (int i = 0; i < keys.length; i++) { 90 if (keys[i] != lastValue) { 91 ExpectingVisitor expectingVisitor = BTreeAssert 92 .createExpectingVisitor(keys[i], IntArrays4.occurences( 93 keys, keys[i])); 94 BTreeRange range = btree.search(trans, new Integer (keys[i])); 95 BTreeAssert.traverseKeys(range, expectingVisitor); 96 expectingVisitor.assertExpectations(); 97 lastValue = keys[i]; 98 } 99 } 100 } 101 } 102 | Popular Tags |