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.concurrency;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.ext.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class CreateIndexInheritedTestCase extends
032: Db4oClientServerTestCase {
033:
034: public static void main(String[] args) {
035: new CreateIndexInheritedTestCase().runConcurrency();
036: }
037:
038: public int i_int;
039:
040: public CreateIndexInheritedTestCase() {
041: }
042:
043: public CreateIndexInheritedTestCase(int a_int) {
044: i_int = a_int;
045: }
046:
047: protected void store() {
048: store(new CreateIndexFor("a"));
049: store(new CreateIndexFor("c"));
050: store(new CreateIndexFor("b"));
051: store(new CreateIndexFor("f"));
052: store(new CreateIndexFor("e"));
053:
054: store(new CreateIndexFor(1));
055: store(new CreateIndexFor(5));
056: store(new CreateIndexFor(7));
057: store(new CreateIndexFor(3));
058: store(new CreateIndexFor(2));
059: store(new CreateIndexFor(3));
060: }
061:
062: protected void configure(Configuration config) {
063: config.objectClass(CreateIndexInheritedTestCase.class)
064: .objectField("i_int").indexed(true);
065: config.objectClass(CreateIndexFor.class).objectField("i_name")
066: .indexed(true);
067: }
068:
069: public void conc1(ExtObjectContainer oc) throws Exception {
070: tQueryB(oc);
071: tQueryInts(oc, 5);
072: }
073:
074: public void conc2(ExtObjectContainer oc) {
075: oc.set(new CreateIndexFor("d"));
076: tQueryB(oc);
077: tUpdateB(oc);
078: oc.set(new CreateIndexFor("z"));
079: oc.set(new CreateIndexFor("y"));
080: }
081:
082: public void check2(ExtObjectContainer oc) {
083: tQueryB(oc);
084: tQueryInts(oc, 5 + threadCount() * 3);
085: }
086:
087: private void tQueryInts(ExtObjectContainer oc, int expectedZeroSize) {
088: Query q = oc.query();
089: q.constrain(CreateIndexFor.class);
090: q.descend("i_int").constrain(new Integer(0));
091: int zeroSize = q.execute().size();
092: Assert.areEqual(expectedZeroSize, zeroSize);
093:
094: q = oc.query();
095: q.constrain(CreateIndexFor.class);
096: q.descend("i_int").constrain(new Integer(4)).greater().equal();
097: tExpectInts(q, new int[] { 5, 7 });
098:
099: q = oc.query();
100: q.constrain(CreateIndexFor.class);
101: q.descend("i_int").constrain(new Integer(4)).greater();
102: tExpectInts(q, new int[] { 5, 7 });
103:
104: q = oc.query();
105: q.constrain(CreateIndexFor.class);
106: q.descend("i_int").constrain(new Integer(3)).greater();
107: tExpectInts(q, new int[] { 5, 7 });
108:
109: q = oc.query();
110: q.constrain(CreateIndexFor.class);
111: q.descend("i_int").constrain(new Integer(3)).greater().equal();
112: tExpectInts(q, new int[] { 3, 3, 5, 7 });
113:
114: q = oc.query();
115: q.constrain(CreateIndexFor.class);
116: q.descend("i_int").constrain(new Integer(2)).greater().equal();
117: tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
118: q = oc.query();
119:
120: q = oc.query();
121: q.constrain(CreateIndexFor.class);
122: q.descend("i_int").constrain(new Integer(2)).greater();
123: tExpectInts(q, new int[] { 3, 3, 5, 7 });
124:
125: q = oc.query();
126: q.constrain(CreateIndexFor.class);
127: q.descend("i_int").constrain(new Integer(1)).greater().equal();
128: tExpectInts(q, new int[] { 1, 2, 3, 3, 5, 7 });
129:
130: q = oc.query();
131: q.constrain(CreateIndexFor.class);
132: q.descend("i_int").constrain(new Integer(1)).greater();
133: tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
134:
135: q = oc.query();
136: q.constrain(CreateIndexFor.class);
137: q.descend("i_int").constrain(new Integer(4)).smaller();
138: tExpectInts(q, new int[] { 1, 2, 3, 3 }, zeroSize);
139:
140: q = oc.query();
141: q.constrain(CreateIndexFor.class);
142: q.descend("i_int").constrain(new Integer(4)).smaller().equal();
143: tExpectInts(q, new int[] { 1, 2, 3, 3 }, zeroSize);
144:
145: q = oc.query();
146: q.constrain(CreateIndexFor.class);
147: q.descend("i_int").constrain(new Integer(3)).smaller();
148: tExpectInts(q, new int[] { 1, 2 }, zeroSize);
149:
150: q = oc.query();
151: q.constrain(CreateIndexFor.class);
152: q.descend("i_int").constrain(new Integer(3)).smaller().equal();
153: tExpectInts(q, new int[] { 1, 2, 3, 3 }, zeroSize);
154:
155: q = oc.query();
156: q.constrain(CreateIndexFor.class);
157: q.descend("i_int").constrain(new Integer(2)).smaller().equal();
158: tExpectInts(q, new int[] { 1, 2 }, zeroSize);
159: q = oc.query();
160:
161: q = oc.query();
162: q.constrain(CreateIndexFor.class);
163: q.descend("i_int").constrain(new Integer(2)).smaller();
164: tExpectInts(q, new int[] { 1 }, zeroSize);
165:
166: q = oc.query();
167: q.constrain(CreateIndexFor.class);
168: q.descend("i_int").constrain(new Integer(1)).smaller().equal();
169: tExpectInts(q, new int[] { 1 }, zeroSize);
170:
171: q = oc.query();
172: q.constrain(CreateIndexFor.class);
173: q.descend("i_int").constrain(new Integer(1)).smaller();
174: tExpectInts(q, new int[] {}, zeroSize);
175:
176: }
177:
178: private void tExpectInts(Query q, int[] ints, int zeroSize) {
179: ObjectSet res = q.execute();
180: Assert.areEqual(ints.length + zeroSize, res.size());
181: while (res.hasNext()) {
182: CreateIndexFor ci = (CreateIndexFor) res.next();
183: for (int i = 0; i < ints.length; i++) {
184: if (ints[i] == ci.i_int) {
185: ints[i] = 0;
186: break;
187: }
188: }
189: }
190: for (int i = 0; i < ints.length; i++) {
191: Assert.areEqual(0, ints[i]);
192: }
193: }
194:
195: private void tExpectInts(Query q, int[] ints) {
196: tExpectInts(q, ints, 0);
197: }
198:
199: private void tQueryB(ExtObjectContainer oc) {
200: ObjectSet res = query(oc, "b");
201: Assert.areEqual(1, res.size());
202: CreateIndexFor ci = (CreateIndexFor) res.next();
203: Assert.areEqual("b", ci.i_name);
204: }
205:
206: private void tUpdateB(ExtObjectContainer oc) {
207: ObjectSet res = query(oc, "b");
208: CreateIndexFor ci = (CreateIndexFor) res.next();
209: ci.i_name = "j";
210: oc.set(ci);
211: res = query(oc, "b");
212: Assert.areEqual(0, res.size());
213: res = query(oc, "j");
214: Assert.areEqual(1, res.size());
215: ci.i_name = "b";
216: oc.set(ci);
217: tQueryB(oc);
218: }
219:
220: private ObjectSet query(ExtObjectContainer oc, String n) {
221: Query q = oc.query();
222: q.constrain(CreateIndexFor.class);
223: q.descend("i_name").constrain(n);
224: return q.execute();
225: }
226:
227: public static class CreateIndexFor extends
228: CreateIndexInheritedTestCase {
229:
230: public String i_name;
231:
232: public CreateIndexFor() {
233: }
234:
235: public CreateIndexFor(String name) {
236: this .i_name = name;
237: }
238:
239: public CreateIndexFor(int a_int) {
240: super(a_int);
241: }
242:
243: }
244:
245: }
|