001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.fieldindex;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.db4ounit.common.btree.*;
026: import com.db4o.db4ounit.common.foundation.IntArrays4;
027: import com.db4o.foundation.Visitor4;
028: import com.db4o.internal.*;
029: import com.db4o.internal.btree.*;
030: import com.db4o.internal.classindex.*;
031: import com.db4o.internal.fieldindex.*;
032: import com.db4o.internal.query.processor.*;
033: import com.db4o.internal.query.processor.QQueryBase.*;
034: import com.db4o.query.Query;
035: import com.db4o.reflect.ReflectClass;
036:
037: import db4ounit.Assert;
038:
039: public abstract class FieldIndexProcessorTestCaseBase extends
040: FieldIndexTestCaseBase {
041:
042: public FieldIndexProcessorTestCaseBase() {
043: super ();
044: }
045:
046: protected void configure(Configuration config) {
047: super .configure(config);
048: indexField(config, ComplexFieldIndexItem.class, "foo");
049: indexField(config, ComplexFieldIndexItem.class, "bar");
050: indexField(config, ComplexFieldIndexItem.class, "child");
051: }
052:
053: protected Query createComplexItemQuery() {
054: return createQuery(ComplexFieldIndexItem.class);
055: }
056:
057: protected IndexedNode selectBestIndex(final Query query) {
058: final FieldIndexProcessor processor = createProcessor(query);
059: return processor.selectBestIndex();
060: }
061:
062: protected FieldIndexProcessor createProcessor(final Query query) {
063: final QCandidates candidates = getQCandidates(query);
064: return new FieldIndexProcessor(candidates);
065: }
066:
067: private QCandidates getQCandidates(final Query query) {
068: final CreateCandidateCollectionResult result = ((QQuery) query)
069: .createCandidateCollection();
070: QCandidates candidates = (QCandidates) result.candidateCollection._element;
071: return candidates;
072: }
073:
074: protected void assertComplexItemIndex(String expectedFieldIndex,
075: IndexedNode node) {
076: Assert.areSame(complexItemIndex(expectedFieldIndex), node
077: .getIndex());
078: }
079:
080: protected BTree fieldIndexBTree(Class clazz, String fieldName) {
081: return getYapClass(clazz).fieldMetadataForName(fieldName)
082: .getIndex(null);
083: }
084:
085: private ClassMetadata getYapClass(Class clazz) {
086: return stream().classMetadataForReflectClass(
087: getReflectClass(clazz));
088: }
089:
090: private ReflectClass getReflectClass(Class clazz) {
091: return stream().reflector().forClass(clazz);
092: }
093:
094: protected BTree classIndexBTree(Class clazz) {
095: return ((BTreeClassIndexStrategy) getYapClass(clazz).index())
096: .btree();
097: }
098:
099: private BTree complexItemIndex(String fieldName) {
100: return fieldIndexBTree(ComplexFieldIndexItem.class, fieldName);
101: }
102:
103: protected int[] mapToObjectIds(Query itemQuery, int[] foos) {
104: int[] lookingFor = IntArrays4.clone(foos);
105:
106: int[] objectIds = new int[foos.length];
107: final ObjectSet set = itemQuery.execute();
108: while (set.hasNext()) {
109: HasFoo item = (HasFoo) set.next();
110: for (int i = 0; i < lookingFor.length; i++) {
111: if (lookingFor[i] == item.getFoo()) {
112: lookingFor[i] = -1;
113: objectIds[i] = (int) db().getID(item);
114: break;
115: }
116: }
117: }
118:
119: int index = indexOfNot(lookingFor, -1);
120: if (-1 != index) {
121: throw new IllegalArgumentException("Foo '"
122: + lookingFor[index] + "' not found!");
123: }
124:
125: return objectIds;
126: }
127:
128: public static int indexOfNot(int[] array, int value) {
129: for (int i = 0; i < array.length; ++i) {
130: if (value != array[i]) {
131: return i;
132: }
133: }
134: return -1;
135: }
136:
137: protected void storeComplexItems(int[] foos, int[] bars) {
138: ComplexFieldIndexItem last = null;
139: for (int i = 0; i < foos.length; i++) {
140: last = new ComplexFieldIndexItem(foos[i], bars[i], last);
141: store(last);
142: }
143: }
144:
145: protected void assertTreeInt(final int[] expectedValues,
146: final TreeInt treeInt) {
147: final ExpectingVisitor visitor = BTreeAssert
148: .createExpectingVisitor(expectedValues);
149: treeInt.traverse(new Visitor4() {
150: public void visit(Object obj) {
151: visitor.visit(new Integer(((TreeInt) obj)._key));
152: }
153: });
154: visitor.assertExpectations();
155: }
156:
157: }
|