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.foundation.Iterator4;
024: import com.db4o.internal.*;
025: import com.db4o.internal.fieldindex.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.Assert;
029:
030: public class IndexedNodeTestCase extends
031: FieldIndexProcessorTestCaseBase {
032:
033: public static void main(String[] args) {
034: new IndexedNodeTestCase().runSolo();
035: }
036:
037: protected void store() {
038: storeItems(new int[] { 3, 4, 7, 9 });
039: storeComplexItems(new int[] { 3, 4, 7, 9 }, new int[] { 2, 2,
040: 8, 8 });
041: }
042:
043: public void testTwoLevelDescendOr() {
044: Query query = createComplexItemQuery();
045: Constraint c1 = query.descend("child").descend("foo")
046: .constrain(new Integer(4)).smaller();
047: Constraint c2 = query.descend("child").descend("foo")
048: .constrain(new Integer(4)).greater();
049: c1.or(c2);
050:
051: assertSingleOrNode(query);
052: }
053:
054: public void testMultipleOrs() {
055: Query query = createComplexItemQuery();
056: Constraint c1 = query.descend("foo").constrain(new Integer(4))
057: .smaller();
058: for (int i = 0; i < 5; i++) {
059: Constraint c2 = query.descend("foo").constrain(
060: new Integer(4)).greater();
061: c1 = c1.or(c2);
062: }
063: assertSingleOrNode(query);
064: }
065:
066: public void testDoubleDescendingOnIndexedNodes() {
067: final Query query = createComplexItemQuery();
068: query.descend("child").descend("foo").constrain(new Integer(3));
069: query.descend("bar").constrain(new Integer(2));
070:
071: final IndexedNode index = selectBestIndex(query);
072: assertComplexItemIndex("foo", index);
073:
074: Assert.isFalse(index.isResolved());
075:
076: IndexedNode result = index.resolve();
077: Assert.isNotNull(result);
078: assertComplexItemIndex("child", result);
079:
080: Assert.isTrue(result.isResolved());
081: Assert.isNull(result.resolve());
082:
083: assertComplexItems(new int[] { 4 }, result.toTreeInt());
084: }
085:
086: public void testTripleDescendingOnQuery() {
087: final Query query = createComplexItemQuery();
088: query.descend("child").descend("child").descend("foo")
089: .constrain(new Integer(3));
090:
091: final IndexedNode index = selectBestIndex(query);
092: assertComplexItemIndex("foo", index);
093:
094: Assert.isFalse(index.isResolved());
095:
096: IndexedNode result = index.resolve();
097: Assert.isNotNull(result);
098: assertComplexItemIndex("child", result);
099:
100: Assert.isFalse(result.isResolved());
101: result = result.resolve();
102: Assert.isNotNull(result);
103: assertComplexItemIndex("child", result);
104:
105: assertComplexItems(new int[] { 7 }, result.toTreeInt());
106: }
107:
108: private void assertComplexItems(final int[] expectedFoos,
109: final TreeInt found) {
110: Assert.isNotNull(found);
111: assertTreeInt(mapToObjectIds(createComplexItemQuery(),
112: expectedFoos), found);
113: }
114:
115: private void assertSingleOrNode(Query query) {
116: Iterator4 nodes = createProcessor(query).collectIndexedNodes();
117: Assert.isTrue(nodes.moveNext());
118:
119: OrIndexedLeaf node = (OrIndexedLeaf) nodes.current();
120: Assert.isNotNull(node);
121:
122: Assert.isFalse(nodes.moveNext());
123: }
124: }
|