1 21 package com.db4o.db4ounit.common.fieldindex; 22 23 import com.db4o.*; 24 import com.db4o.QQueryBase.CreateCandidateCollectionResult; 25 import com.db4o.config.*; 26 import com.db4o.db4ounit.common.btree.*; 27 import com.db4o.db4ounit.common.foundation.IntArrays4; 28 import com.db4o.foundation.Visitor4; 29 import com.db4o.inside.btree.BTree; 30 import com.db4o.inside.classindex.BTreeClassIndexStrategy; 31 import com.db4o.inside.fieldindex.*; 32 import com.db4o.query.Query; 33 import com.db4o.reflect.ReflectClass; 34 35 import db4ounit.Assert; 36 37 public abstract class FieldIndexProcessorTestCaseBase extends 38 FieldIndexTestCaseBase { 39 40 public FieldIndexProcessorTestCaseBase() { 41 super(); 42 } 43 44 protected void configure(Configuration config) { 45 super.configure(config); 46 indexField(config,ComplexFieldIndexItem.class, "foo"); 47 indexField(config,ComplexFieldIndexItem.class, "bar"); 48 indexField(config,ComplexFieldIndexItem.class, "child"); 49 } 50 51 protected Query createComplexItemQuery() { 52 return createQuery(ComplexFieldIndexItem.class); 53 } 54 55 protected IndexedNode selectBestIndex(final Query query) { 56 final FieldIndexProcessor processor = createProcessor(query); 57 return processor.selectBestIndex(); 58 } 59 60 protected FieldIndexProcessor createProcessor(final Query query) { 61 final QCandidates candidates = getQCandidates(query); 62 return new FieldIndexProcessor(candidates); 63 } 64 65 private QCandidates getQCandidates(final Query query) { 66 final CreateCandidateCollectionResult result = ((QQuery)query).createCandidateCollection(); 67 QCandidates candidates = (QCandidates)result.candidateCollection._element; 68 return candidates; 69 } 70 71 protected void assertComplexItemIndex(String expectedFieldIndex, IndexedNode node) { 72 Assert.areSame(complexItemIndex(expectedFieldIndex), node.getIndex()); 73 } 74 75 protected BTree fieldIndexBTree(Class clazz, String fieldName) { 76 return getYapClass(clazz).getYapField(fieldName).getIndex(null); 77 } 78 79 private YapClass getYapClass(Class clazz) { 80 return stream().getYapClass(getReflectClass(clazz)); 81 } 82 83 private ReflectClass getReflectClass(Class clazz) { 84 return stream().reflector().forClass(clazz); 85 } 86 87 protected BTree classIndexBTree(Class clazz) { 88 return ((BTreeClassIndexStrategy)getYapClass(clazz).index()).btree(); 89 } 90 91 private BTree complexItemIndex(String fieldName) { 92 return fieldIndexBTree(ComplexFieldIndexItem.class, fieldName); 93 } 94 95 protected int[] mapToObjectIds(Query itemQuery, int[] foos) { 96 int[] lookingFor = IntArrays4.clone(foos); 97 98 int[] objectIds = new int[foos.length]; 99 final ObjectSet set = itemQuery.execute(); 100 while (set.hasNext()) { 101 HasFoo item = (HasFoo)set.next(); 102 for (int i = 0; i < lookingFor.length; i++) { 103 if(lookingFor[i] == item.getFoo()){ 104 lookingFor[i] = -1; 105 objectIds[i] = (int) db().getID(item); 106 break; 107 } 108 } 109 } 110 111 int index = indexOfNot(lookingFor, -1); 112 if (-1 != index) { 113 throw new IllegalArgumentException ("Foo '" + lookingFor[index] + "' not found!"); 114 } 115 116 return objectIds; 117 } 118 119 public static int indexOfNot(int[] array, int value) { 120 for (int i=0; i<array.length; ++i) { 121 if (value != array[i]) { 122 return i; 123 } 124 } 125 return -1; 126 } 127 128 protected void storeComplexItems(int[] foos, int[] bars) { 129 ComplexFieldIndexItem last = null; 130 for (int i = 0; i < foos.length; i++) { 131 last = new ComplexFieldIndexItem(foos[i], bars[i], last); 132 store(last); 133 } 134 } 135 136 protected void assertTreeInt(final int[] expectedValues, final TreeInt treeInt) { 137 final ExpectingVisitor visitor = BTreeAssert.createExpectingVisitor(expectedValues); 138 treeInt.traverse(new Visitor4() { 139 public void visit(Object obj) { 140 visitor.visit(new Integer (((TreeInt)obj)._key)); 141 } 142 }); 143 visitor.assertExpectations(); 144 } 145 146 } | Popular Tags |