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.ext.StoredField;
028: import com.db4o.foundation.Visitor4;
029: import com.db4o.internal.*;
030: import com.db4o.internal.btree.*;
031: import com.db4o.query.Query;
032: import com.db4o.reflect.ReflectClass;
033:
034: import db4ounit.Assert;
035:
036: public class FieldIndexTestCase extends FieldIndexTestCaseBase {
037:
038: private static final int[] FOOS = new int[] { 3, 7, 9, 4 };
039:
040: public static void main(String[] arguments) {
041: new FieldIndexTestCase().runSolo();
042: }
043:
044: protected void configure(Configuration config) {
045: super .configure(config);
046: }
047:
048: protected void store() {
049: storeItems(FOOS);
050: }
051:
052: public void testTraverseValues() {
053: StoredField field = yapField();
054: ExpectingVisitor expectingVisitor = new ExpectingVisitor(
055: IntArrays4.toObjectArray(FOOS));
056: field.traverseValues(expectingVisitor);
057: expectingVisitor.assertExpectations();
058: }
059:
060: public void testAllThere() throws Exception {
061: for (int i = 0; i < FOOS.length; i++) {
062: Query q = createQuery(FOOS[i]);
063: ObjectSet objectSet = q.execute();
064: Assert.areEqual(1, objectSet.size());
065: FieldIndexItem fii = (FieldIndexItem) objectSet.next();
066: Assert.areEqual(FOOS[i], fii.foo);
067: }
068: }
069:
070: public void testAccessingBTree() throws Exception {
071: BTree bTree = yapField().getIndex(trans());
072: Assert.isNotNull(bTree);
073: expectKeysSearch(bTree, FOOS);
074: }
075:
076: private void expectKeysSearch(BTree btree, int[] values) {
077: int lastValue = Integer.MIN_VALUE;
078: for (int i = 0; i < values.length; i++) {
079: if (values[i] != lastValue) {
080: final ExpectingVisitor expectingVisitor = BTreeAssert
081: .createExpectingVisitor(values[i], IntArrays4
082: .occurences(values, values[i]));
083: BTreeRange range = fieldIndexKeySearch(trans(), btree,
084: new Integer(values[i]));
085: BTreeAssert.traverseKeys(range, new Visitor4() {
086: public void visit(Object obj) {
087: FieldIndexKey fik = (FieldIndexKey) obj;
088: expectingVisitor.visit(fik.value());
089: }
090: });
091: expectingVisitor.assertExpectations();
092: lastValue = values[i];
093: }
094: }
095: }
096:
097: private FieldIndexKey fieldIndexKey(int integerPart,
098: Object composite) {
099: return new FieldIndexKey(integerPart, composite);
100: }
101:
102: private BTreeRange fieldIndexKeySearch(Transaction trans,
103: BTree btree, Object key) {
104: // SearchTarget should not make a difference, HIGHEST is faster
105: BTreeNodeSearchResult start = btree.searchLeaf(trans,
106: fieldIndexKey(0, key), SearchTarget.LOWEST);
107: BTreeNodeSearchResult end = btree.searchLeaf(trans,
108: fieldIndexKey(Integer.MAX_VALUE, key),
109: SearchTarget.LOWEST);
110: return start.createIncludingRange(end);
111: }
112:
113: private FieldMetadata yapField() {
114: ReflectClass claxx = stream().reflector().forObject(
115: new FieldIndexItem());
116: ClassMetadata yc = stream().classMetadataForReflectClass(claxx);
117: FieldMetadata yf = yc.fieldMetadataForName("foo");
118: return yf;
119: }
120:
121: }
|