KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > fieldindex > FieldIndexProcessorTestCaseBase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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 JavaDoc expectedFieldIndex, IndexedNode node) {
72         Assert.areSame(complexItemIndex(expectedFieldIndex), node.getIndex());
73     }
74
75     protected BTree fieldIndexBTree(Class JavaDoc clazz, String JavaDoc fieldName) {
76         return getYapClass(clazz).getYapField(fieldName).getIndex(null);
77     }
78
79     private YapClass getYapClass(Class JavaDoc clazz) {
80         return stream().getYapClass(getReflectClass(clazz));
81     }
82
83     private ReflectClass getReflectClass(Class JavaDoc clazz) {
84         return stream().reflector().forClass(clazz);
85     }
86     
87     protected BTree classIndexBTree(Class JavaDoc clazz) {
88         return ((BTreeClassIndexStrategy)getYapClass(clazz).index()).btree();
89     }
90
91     private BTree complexItemIndex(String JavaDoc 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 JavaDoc("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 JavaDoc obj) {
140                 visitor.visit(new Integer JavaDoc(((TreeInt)obj)._key));
141             }
142         });
143         visitor.assertExpectations();
144     }
145
146 }
Popular Tags