001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.tests;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.lang.reflect.Field;
024: import java.util.List;
025: import java.util.Random;
026:
027: import com.mobixess.jodb.core.JODB;
028: import com.mobixess.jodb.core.JODBConfig;
029: import com.mobixess.jodb.core.JODBSessionContainer;
030: import com.mobixess.jodb.core.index.JODBIndexingAgent;
031: import com.mobixess.jodb.core.index.JODBIndexingRootAgent;
032: import com.mobixess.jodb.core.transaction.ITransactionListener;
033: import com.mobixess.jodb.core.transaction.JODBQueryList;
034: import com.mobixess.jodb.core.transaction.JODBSession;
035: import com.mobixess.jodb.soda.api.Query;
036: import com.mobixess.jodb.tests.testobjects.ObjectA;
037:
038: public class IndexingTests {
039:
040: private static int _testCounter = 0;
041: private static String TEST_DATA_DIR = "./testData/IndexingTests/";
042:
043: /**
044: * @param args
045: * @throws Exception
046: */
047: public static void main(String[] args) throws Exception {
048: IndexingTests indexingTests = new IndexingTests();
049: indexingTests.randomAddTest();
050: indexingTests.randomAddSearchWithIndexTest();
051: indexingTests.sortingTest();
052: indexingTests.simpleIndexAdd(true);
053: indexingTests.simpleIndexAdd(false);
054: System.out.println("Test complete");
055: }
056:
057: public void simpleIndexAdd(boolean reopen) throws Exception {
058: File testFileDir = new File(TEST_DATA_DIR);
059: testFileDir.mkdirs();
060: File testFile = new File(testFileDir, SimpleAddTest.class
061: .getSimpleName()
062: + (_testCounter++) + ".jdb");
063: testFile.delete();
064: if (testFile.exists()) {
065: throw new Exception();
066: }
067: JODBSessionContainer sessionContainer = (JODBSessionContainer) JODB
068: .open(testFile);
069: sessionContainer.configureIndex(ObjectA.class, "_val2", true);
070:
071: for (int i = 1000; i >= 0; --i) {
072: ObjectA objectA = new ObjectA((byte) 2, (byte) i, null);
073: sessionContainer.set(objectA);
074: }
075:
076: //sessionContainer.printFileMap();
077:
078: sessionContainer.commit();
079:
080: if (reopen) {
081: sessionContainer.close();
082: sessionContainer = (JODBSessionContainer) JODB
083: .open(testFile);
084: }
085:
086: //System.err.println("_________________________________________________________");
087: //sessionContainer.printFileMap();
088:
089: JODBQueryList list = sessionContainer.getAllObjects();
090:
091: int total = list.size();
092: for (int i = 0; i < total; i++) {
093: ObjectA nextObj = (ObjectA) list.get(i);
094: nextObj.setVal2((short) i);
095: sessionContainer.set(nextObj);
096: }
097:
098: sessionContainer.commit();
099:
100: if (reopen) {
101: sessionContainer.close();
102: sessionContainer = (JODBSessionContainer) JODB
103: .open(testFile);
104: }
105:
106: sessionContainer.close();
107: }
108:
109: public void randomAddTest() throws Exception {
110: randomAddTest(true, false);
111: randomAddTest(true, true);
112: randomAddTest(false, false);
113: randomAddTest(false, true);
114: }
115:
116: public void randomAddTest(boolean reopen, boolean ascending)
117: throws Exception {
118: File testFileDir = new File(TEST_DATA_DIR);
119: testFileDir.mkdirs();
120: File testFile = new File(testFileDir, SimpleAddTest.class
121: .getSimpleName()
122: + (_testCounter++) + ".jdb");
123: testFile.delete();
124: if (testFile.exists()) {
125: throw new Exception();
126: }
127: JODBSessionContainer sessionContainer = (JODBSessionContainer) JODB
128: .open(testFile);
129: sessionContainer.configureIndex(ObjectA.class, "_val2", true);
130:
131: Random random = new Random(376538);
132:
133: int maxObjects = 10000;
134:
135: for (int i = maxObjects - 1; i >= 0; --i) {
136: short next = (short) random.nextInt();
137: ObjectA objectA = new ObjectA((byte) 2, next, null);
138: sessionContainer.set(objectA);
139: }
140:
141: sessionContainer.commit();
142:
143: if (reopen) {
144: sessionContainer.close();
145: sessionContainer = (JODBSessionContainer) JODB
146: .open(testFile);
147: }
148:
149: Query query = sessionContainer.query();
150: query.constrain(ObjectA.class);
151: if (ascending) {
152: query.descend("_val2").orderAscending();
153: } else {
154: query.descend("_val2").orderDescending();
155: }
156:
157: JODBQueryList list = (JODBQueryList) query.execute();
158: if (list.size() != maxObjects) {
159: throw new RuntimeException();
160: }
161:
162: ObjectA prev = null;
163: for (int i = 0; i < list.size(); i++) {
164: ObjectA current = (ObjectA) list.get(i);
165: if (prev != null) {
166: if (ascending) {
167: if (prev.getVal2() > current.getVal2()) {
168: throw new RuntimeException();
169: }
170: } else if (prev.getVal2() < current.getVal2()) {
171: JODBIndexingAgent indexingAgent = sessionContainer
172: .getIndexingAgent(ObjectA.class
173: .getDeclaredField("_val2"));
174: long prevID = sessionContainer
175: .getPersistenceStatistics(prev)
176: .getObjectID();
177: long currentID = sessionContainer
178: .getPersistenceStatistics(current)
179: .getObjectID();
180: int prevPosition = indexingAgent
181: .linearIdSearch(prevID);
182: int currentPosition = indexingAgent
183: .linearIdSearch(currentID);
184: throw new RuntimeException("" + prevID + " "
185: + prevPosition + " " + currentID + " "
186: + currentPosition);
187: }
188: }
189: prev = current;
190: }
191:
192: //System.err.println("_________________________________________________________");
193: //sessionContainer.printFileMap();
194:
195: list = sessionContainer.getAllObjects();
196:
197: int total = list.size();
198: for (int i = 0; i < total; i++) {
199: ObjectA nextObj = (ObjectA) list.get(i);
200: nextObj.setVal2((short) i);
201: sessionContainer.set(nextObj);
202: }
203:
204: sessionContainer.commit();
205:
206: if (reopen) {
207: sessionContainer.close();
208: sessionContainer = (JODBSessionContainer) JODB
209: .open(testFile);
210: }
211:
212: query = sessionContainer.query();
213: query.constrain(ObjectA.class);
214: if (ascending) {
215: query.descend("_val2").orderAscending();
216: } else {
217: query.descend("_val2").orderDescending();
218: }
219:
220: list = (JODBQueryList) query.execute();
221: if (list.size() == 0) {
222: throw new RuntimeException();
223: }
224:
225: prev = null;
226: for (int i = 0; i < list.size(); i++) {
227: ObjectA current = (ObjectA) list.get(i);
228: if (prev != null) {
229: if (ascending) {
230: if (prev.getVal2() > current.getVal2()) {
231: throw new RuntimeException();
232: }
233: } else if (prev.getVal2() < current.getVal2()) {
234: JODBIndexingAgent indexingAgent = sessionContainer
235: .getIndexingAgent(ObjectA.class
236: .getDeclaredField("_val2"));
237: long prevID = sessionContainer
238: .getPersistenceStatistics(prev)
239: .getObjectID();
240: long currentID = sessionContainer
241: .getPersistenceStatistics(current)
242: .getObjectID();
243: int prevPosition = indexingAgent
244: .linearIdSearch(prevID);
245: int currentPosition = indexingAgent
246: .linearIdSearch(currentID);
247: throw new RuntimeException(" i=" + i + " -->"
248: + prevID + " " + prevPosition + " "
249: + prev.getVal2() + " " + currentID + " "
250: + currentPosition + " " + current.getVal2());
251: }
252: }
253: prev = current;
254: }
255:
256: sessionContainer.close();
257: }
258:
259: public void sortingTest() throws Exception {
260: sortingTest(true, true);
261: sortingTest(true, false);
262: sortingTest(false, true);
263: sortingTest(false, false);
264: }
265:
266: public void sortingTest(boolean reopen, boolean ascending)
267: throws Exception {
268: File testFileDir = new File(TEST_DATA_DIR);
269: testFileDir.mkdirs();
270: File testFile = new File(testFileDir, SimpleAddTest.class
271: .getSimpleName()
272: + (_testCounter++) + ".jdb");
273: testFile.delete();
274: JODBSessionContainer sessionContainer = (JODBSessionContainer) JODB
275: .open(testFile);
276: sessionContainer.configureIndex(ObjectA.class, "_val1", true);
277: sessionContainer.configureIndex(ObjectA.class, "_val2", true);
278: ObjectA objectA1 = new ObjectA((byte) 0, (short) 2, null);
279: ObjectA objectA2 = new ObjectA((byte) 0, (short) 1, null);
280: ObjectA objectA3 = new ObjectA((byte) 0, (short) 3, null);
281:
282: sessionContainer.set(objectA1);
283: sessionContainer.set(objectA2);
284: sessionContainer.set(objectA3);
285:
286: sessionContainer.commit();
287:
288: if (reopen) {
289: sessionContainer.close();
290: sessionContainer = (JODBSessionContainer) JODB
291: .open(testFile);
292: }
293: Query query = sessionContainer.query();
294: query.constrain(ObjectA.class);
295: if (ascending) {
296: query.descend("_val1").orderAscending();
297: } else {
298: query.descend("_val1").orderDescending();
299: }
300: if (ascending) {
301: query.descend("_val2").orderAscending();
302: } else {
303: query.descend("_val2").orderDescending();
304: }
305: JODBConfig.setCacheOnSortOperations(false);
306: List list = query.execute();
307: if (list.size() == 0) {
308: throw new RuntimeException();
309: }
310:
311: ObjectA prev = null;
312: for (int i = 0; i < list.size(); i++) {
313: ObjectA current = (ObjectA) list.get(i);
314: if (prev != null) {
315: if (ascending) {
316: if (prev.getVal2() > current.getVal2()) {
317: throw new RuntimeException();
318: }
319: } else if (prev.getVal2() < current.getVal2()) {
320: throw new RuntimeException();
321: }
322: }
323: prev = current;
324: }
325:
326: if (reopen) {
327: sessionContainer.close();
328: sessionContainer = (JODBSessionContainer) JODB
329: .open(testFile);
330: }
331:
332: query = sessionContainer.query();
333: query.constrain(ObjectA.class);
334: if (ascending) {
335: query.descend("_val2").orderAscending();
336: } else {
337: query.descend("_val2").orderDescending();
338: }
339:
340: JODBConfig.setCacheOnSortOperations(true);
341: list = query.execute();
342: if (list.size() == 0) {
343: throw new RuntimeException();
344: }
345:
346: prev = null;
347: for (int i = 0; i < list.size(); i++) {
348: ObjectA current = (ObjectA) list.get(i);
349: if (prev != null) {
350: if (ascending) {
351: if (prev.getVal2() > current.getVal2()) {
352: throw new RuntimeException();
353: }
354: } else if (prev.getVal2() < current.getVal2()) {
355: throw new RuntimeException();
356: }
357: }
358: prev = current;
359: }
360: JODBConfig.setCacheOnSortOperations(true);
361: }
362:
363: public void randomAddSearchWithIndexTest() throws Exception {
364: randomAddSearchWithIndexTest(false);
365: randomAddSearchWithIndexTest(true);
366: }
367:
368: public JODBSessionContainer getContainerForFile(File file)
369: throws IOException {
370: return (JODBSessionContainer) JODB.open(file);
371: }
372:
373: public void randomAddSearchWithIndexTest(boolean reopen)
374: throws Exception {
375: File testFileDir = new File(TEST_DATA_DIR);
376: testFileDir.mkdirs();
377: File testFile = new File(testFileDir, SimpleAddTest.class
378: .getSimpleName()
379: + (_testCounter++) + ".jdb");
380: testFile.delete();
381: if (testFile.exists()) {
382: throw new Exception();
383: }
384: JODBSessionContainer sessionContainer = (JODBSessionContainer) JODB
385: .open(testFile);
386:
387: Random random = new Random(376538);
388:
389: int negatives = 0;
390: int maxObjects = 10000;
391:
392: for (int i = maxObjects - 1; i >= 0; --i) {
393: short next = (short) random.nextInt();
394: if (next < 0) {
395: negatives++;
396: }
397: ObjectA objectA = new ObjectA((byte) 2, next, null);
398: sessionContainer.set(objectA);
399: }
400:
401: if (negatives == 0) {
402: throw new RuntimeException();
403: }
404:
405: sessionContainer.commit();
406:
407: if (reopen) {
408: sessionContainer.close();
409: sessionContainer = (JODBSessionContainer) JODB
410: .open(testFile);
411: }
412:
413: Query query = sessionContainer.query();
414: query.constrain(ObjectA.class);
415:
416: query.descend("_val2").constrain(0).smaller();
417:
418: long queryStart = System.currentTimeMillis();
419: JODBQueryList list = (JODBQueryList) query.execute();
420: long noneIndexQuery = System.currentTimeMillis() - queryStart;
421: System.err.println("noneIndexQuery=" + noneIndexQuery);
422:
423: if (list.size() != negatives) {
424: throw new RuntimeException();
425: }
426:
427: sessionContainer.configureIndex(ObjectA.class, "_val2", true);
428:
429: if (reopen) {
430: sessionContainer.close();
431: sessionContainer = (JODBSessionContainer) JODB
432: .open(testFile);
433: }
434:
435: query = sessionContainer.query();
436: query.constrain(ObjectA.class);
437:
438: query.descend("_val2").constrain(0).smaller();
439:
440: queryStart = System.currentTimeMillis();
441: list = (JODBQueryList) query.execute();
442: long queryWithIndex = System.currentTimeMillis() - queryStart;
443: System.err.println("queryWithIndex=" + queryWithIndex);
444:
445: if (list.size() != negatives) {
446: throw new RuntimeException();
447: }
448:
449: sessionContainer.commit();
450:
451: for (int i = 0; i < list.size(); i++) {
452: ObjectA current = (ObjectA) list.get(i);
453: if (current.getVal2() >= 0) {
454: throw new RuntimeException();
455: }
456: }
457:
458: sessionContainer.commit();
459:
460: }
461:
462: private static class TransactionListener implements
463: ITransactionListener {
464:
465: private JODBSessionContainer _sessionContainer;
466: private Field _indexingField;
467:
468: public TransactionListener(
469: JODBSessionContainer sessionContainer,
470: Field indexingField) {
471: _sessionContainer = sessionContainer;
472: _indexingField = indexingField;
473: }
474:
475: public void onCommitFinished(Object objectToBeCommited,
476: JODBSession session) {
477: // TODO Auto-generated method stub
478: throw new RuntimeException("Not Implemented");
479: //
480: }
481:
482: public void onCommitStarted(Object objectToBeCommited,
483: JODBSession session) {
484: try {
485: JODBIndexingRootAgent agent = session
486: .getIndexingRootAgent();
487: JODBIndexingAgent indexingAgent = agent
488: .getIndexingAgent(_indexingField, session
489: .getBase());
490:
491: System.err.println("Commit started for object ");
492: } catch (IOException e) {
493: // TODO Auto-generated catch block
494: e.printStackTrace();
495: }
496: }
497:
498: public void onSet(Object object, JODBSession session) {
499: // TODO Auto-generated method stub
500: throw new RuntimeException("Not Implemented");
501: //
502: }
503:
504: }
505:
506: }
|