00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.database.fastobjectdb.db.test;
00030
00031 import java.io.BufferedReader;
00032 import java.io.File;
00033 import java.io.FileReader;
00034 import java.util.HashMap;
00035
00036 import org.odbms.Constraint;
00037 import org.odbms.ObjectSet;
00038 import org.odbms.Query;
00039 import org.openmobileis.common.util.collection.Array;
00040 import org.openmobileis.common.util.log.LogManager;
00041 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00042 import org.openmobileis.database.fastobjectdb.FODBIntIndexDescriptor;
00043 import org.openmobileis.database.fastobjectdb.FODBLongIndexDescriptor;
00044 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00045 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00046 import org.openmobileis.database.fastobjectdb.db.BackwardUniqueIndexIterator;
00047 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00048 import org.openmobileis.database.fastobjectdb.db.ForwardUniqueIndexIterator;
00049 import org.openmobileis.database.fastobjectdb.db.crypto.AcmeBlowfishCypher;
00050 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00051
00059 public final class TestFastObjectDB {
00060 private static int NB_INSERT_BOUCLE = 1;
00061
00062 private static int NB_SEARCH_CREATED_DATA = 30;
00063
00064 private HashMap keyMap = new HashMap(500);
00065
00066 private HashMap objectMap = new HashMap(500);
00067
00068 private FastObjectDB db = null;
00069
00073 public TestFastObjectDB() {
00074 super();
00075 }
00076
00077 protected void testTree(Array inputDataList, FastObjectDB db) throws FODBException {
00078
00079 for (int i = 0; i < inputDataList.size(); i++) {
00080 String key = (String) inputDataList.get(i);
00081 try {
00082
00083 Query q = db.query();
00084 q.constrain(TestData.class);
00085 Query q2 = q.descend("getKey()");
00086 q2.constrain(key).equal();
00087 ObjectSet set = q.execute();
00088 if (set.size() == 0) {
00089 throw new FODBException("ERROR Key not found :" + key);
00090 } else if (!((TestData) set.next()).getKey().equals(key)) {
00091 throw new FODBException("ERROR Key Not egal to value :" + key);
00092 }
00093 } catch (FODBException ex) {
00094 LogManager.traceError(0, "Bad KEY READ :" + key);
00095 throw ex;
00096 }
00097 }
00098 }
00099
00100 protected void testRomvedTree(Array inputDataList, FastObjectDB db) throws FODBException {
00101
00102 for (int i = 0; i < inputDataList.size(); i++) {
00103 String key = (String) inputDataList.get(i);
00104 try {
00105
00106 Query q = db.query();
00107 q.constrain(TestData.class);
00108 Query q2 = q.descend("getKey()");
00109 q2.constrain(key).equal();
00110 ObjectSet set = q.execute();
00111 if (set.size() != 0) {
00112 throw new FODBException("ERROR Key found :" + key);
00113 }
00114 } catch (FODBException ex) {
00115 LogManager.traceError(0, "Bad KEY READ :" + key);
00116 throw ex;
00117 }
00118 }
00119 }
00120
00121 private void testInitIndexIterator(String collectionName, String memberName, Array objList) throws FODBException {
00122 ForwardUniqueIndexIterator iterf = db.getCollection(collectionName).getForwardIndexIterator(memberName);
00123 for (int i = 0; i < NB_SEARCH_CREATED_DATA; i++) {
00124 iterf.initCurrentNode(new Integer(i));
00125 for (int j = i + 1; j < NB_SEARCH_CREATED_DATA; j++) {
00126 TestSearchData data = (TestSearchData) iterf.next();
00127 if (data.getKey() != j) {
00128 throw new FODBException("testInitIndexIterator error bad iteration");
00129 }
00130 }
00131 }
00132 BackwardUniqueIndexIterator iterb = db.getCollection(collectionName).getBackwardIndexIterator(memberName);
00133 for (int i = 0; i < NB_SEARCH_CREATED_DATA; i++) {
00134 iterb.initCurrentNode(new Integer(i));
00135 for (int j = i - 1; j >= 0; j--) {
00136 TestSearchData data = (TestSearchData) iterb.prev();
00137 if (data.getKey() != j) {
00138 throw new FODBException("testInitIndexIterator error bad iteration");
00139 }
00140 }
00141 }
00142
00143 }
00144
00145 private void testIndexIterator(String collectionName, String memberName, Array objList) throws FODBException {
00146 ForwardUniqueIndexIterator iterf = db.getCollection(collectionName).getForwardIndexIterator(memberName);
00147 Array indexobj = new Array();
00148 while (iterf.hasNext()) {
00149 indexobj.add(iterf.next());
00150 }
00151
00152 if (indexobj.size() != objList.size()) {
00153 throw new FODBException("testIndexIterator bad size");
00154 }
00155 for (int i = 0; i < indexobj.size(); i++) {
00156 Object obj = objList.get(i);
00157 if (!indexobj.contains(obj)) {
00158 throw new FODBException("testIndexIterator obj not found :" + obj);
00159 }
00160 }
00161
00162
00163 BackwardUniqueIndexIterator iterb = db.getCollection(collectionName).getBackwardIndexIterator(memberName);
00164 indexobj = new Array();
00165 while (iterb.hasPrev()) {
00166 indexobj.add(iterb.prev());
00167 }
00168
00169 if (indexobj.size() != objList.size()) {
00170 throw new FODBException("testIndexIterator bad size");
00171 }
00172 for (int i = 0; i < indexobj.size(); i++) {
00173 Object obj = objList.get(i);
00174 if (!indexobj.contains(obj)) {
00175 throw new FODBException("testIndexIterator obj not found :" + obj);
00176 }
00177 }
00178 }
00179
00180 private void addKeyToMap(String key) {
00181 String begin = null;
00182 if (key.length() >= TestData.NB_BEGIN_KEY_LETTER) {
00183 begin = key.substring(0, TestData.NB_BEGIN_KEY_LETTER);
00184 } else {
00185 begin = key;
00186 }
00187 Integer inter = (Integer) keyMap.get(begin);
00188 if (inter == null) {
00189 inter = new Integer(0);
00190 } else {
00191 keyMap.remove(begin);
00192 }
00193 int old = inter.intValue();
00194 old++;
00195 keyMap.put(begin, new Integer(old));
00196 }
00197
00198 private void removeKeyToMap(String key) {
00199 String begin = null;
00200 if (key.length() >= TestData.NB_BEGIN_KEY_LETTER) {
00201 begin = key.substring(0, TestData.NB_BEGIN_KEY_LETTER);
00202 } else {
00203 begin = key;
00204 }
00205 Integer inter = (Integer) keyMap.get(begin);
00206 if (inter == null) {
00207 return;
00208 } else {
00209 keyMap.remove(begin);
00210 }
00211 int old = inter.intValue();
00212 old--;
00213 if (old == 0) {
00214 keyMap.remove(begin);
00215 } else {
00216 keyMap.put(begin, new Integer(old));
00217 }
00218 }
00219
00220 private int getNbKeyForBegin(String begin) {
00221 Integer inter = (Integer) keyMap.get(begin);
00222 if (inter == null)
00223 return 0;
00224 else
00225 return inter.intValue();
00226 }
00227
00228 public void testFindLike(Array inputDataList, FastObjectDB db) throws FODBException {
00229 for (int i = 0; i < inputDataList.size(); i++) {
00230 String key = (String) inputDataList.get(i);
00231 String begin = null;
00232 if (key.length() >= TestData.NB_BEGIN_KEY_LETTER) {
00233 begin = key.substring(0, TestData.NB_BEGIN_KEY_LETTER);
00234 } else {
00235 begin = key;
00236 }
00237
00238 Query q = db.query();
00239 q.constrain(TestData.class);
00240 Query q2 = q.descend("getKey()");
00241 q2.constrain(begin).like();
00242 ObjectSet set = q.execute();
00243 int listnb = this.getNbKeyForBegin(begin);
00244 if (listnb != set.size()) {
00245 throw new FODBException("ERROR BAD NUMBER of KEY LIKE FOUND :" + key);
00246 } else {
00247 for (int j = 0; j < set.size(); j++) {
00248 TestData objkey = (TestData) set.next();
00249 if (!objkey.getKey().startsWith(begin)) {
00250 throw new FODBException("ERROR BAD FIND LIKE VALUE FOR KEY :" + key);
00251 }
00252
00253 }
00254 }
00255 }
00256
00257 }
00258
00259 public void testMultipleIndex(Array inputDataList, FastObjectDB db) throws FODBException {
00260 for (int i = 0; i < inputDataList.size(); i++) {
00261 String key = (String) inputDataList.get(i);
00262 String begin = null;
00263 if (key.length() >= TestData.NB_BEGIN_KEY_LETTER) {
00264 begin = key.substring(0, TestData.NB_BEGIN_KEY_LETTER);
00265 } else {
00266 begin = key;
00267 }
00268
00269 Query q = db.query();
00270 q.constrain(TestData.class);
00271 Query q2 = q.descend("getDebKey()");
00272 q2.constrain(begin).equal();
00273 ObjectSet set = q.execute();
00274 int listnb = this.getNbKeyForBegin(begin);
00275 if (listnb != set.size()) {
00276 throw new FODBException("ERROR BAD NUMBER of KEY LIKE FOUND :" + key);
00277 } else {
00278 while (set.hasNext()) {
00279 TestData objkey = (TestData) set.next();
00280 if (!objkey.getKey().startsWith(begin)) {
00281 throw new FODBException("ERROR BAD FIND LIKE VALUE FOR KEY :" + key);
00282 }
00283 }
00284 }
00285 }
00286
00287 }
00288
00289 public void openDB() {
00290 try {
00291 String tempdir = System.getProperty("user.dir") + File.separator;
00292 db = FastObjectDB.open(tempdir, "testdb");
00293 if (!db.isCollectionExist("TESTDATA")) {
00294 db.createCollection("TESTDATA", TestData.class);
00295 FODBStringIndexDescriptor KeyDescriptor = new FODBStringIndexDescriptor("KEY", FODBIndexDescriptor.UNIQUE, "getKey()", 12, 15);
00296 db.addIndex("TESTDATA", KeyDescriptor);
00297 FODBStringIndexDescriptor debKeyDescriptor = new FODBStringIndexDescriptor("DEBKEY", FODBIndexDescriptor.MULTIPLE, "getDebKey()", 10,
00298 TestData.NB_BEGIN_KEY_LETTER, 5);
00299 db.addIndex("TESTDATA", debKeyDescriptor);
00300 }
00301 FODBCollection col = db.getCollection("TESTDATA");
00302 col.setCollectionCypher(new AcmeBlowfishCypher());
00303 if (!db.isCollectionExist("TESTSEARCH")) {
00304 db.createCollection("TESTSEARCH", TestSearchData.class);
00305 FODBIntIndexDescriptor KeyDescriptor = new FODBIntIndexDescriptor("KEY", FODBIndexDescriptor.UNIQUE, "getKey()", 5);
00306 db.addIndex("TESTSEARCH", KeyDescriptor);
00307 FODBIntIndexDescriptor debKeyDescriptor = new FODBIntIndexDescriptor("DIZ", FODBIndexDescriptor.MULTIPLE, "getDizaine()", 5, 5);
00308 db.addIndex("TESTSEARCH", debKeyDescriptor);
00309 }
00310 if (!db.isCollectionExist("TESTDATAWITHARRAY")) {
00311 db.createCollection("TESTDATAWITHARRAY", TestDataWithArray.class);
00312 FODBStringIndexDescriptor intIdDescriptor = new FODBStringIndexDescriptor("ID1", FODBIndexDescriptor.UNIQUE, "getId()", 10, 50);
00313 db.addIndex("TESTDATAWITHARRAY", intIdDescriptor);
00314 FODBLongIndexDescriptor LongIdDescriptor = new FODBLongIndexDescriptor("ID2", FODBIndexDescriptor.UNIQUE, "getIdLong()", 10);
00315 db.addIndex("TESTDATAWITHARRAY", LongIdDescriptor);
00316 FODBStringIndexDescriptor StrDescriptor2 = new FODBStringIndexDescriptor("ELEMENTSTRING", FODBIndexDescriptor.MULTIPLE, "getStringList()", 10, 10);
00317 db.addIndex("TESTDATAWITHARRAY", StrDescriptor2);
00318 FODBIntIndexDescriptor IntDescriptor = new FODBIntIndexDescriptor("ELEMENTINT", FODBIndexDescriptor.MULTIPLE, "getIntList()", 10, 10);
00319 db.addIndex("TESTDATAWITHARRAY", IntDescriptor);
00320 FODBLongIndexDescriptor LongDescriptor = new FODBLongIndexDescriptor("ELEMENTLONG", FODBIndexDescriptor.MULTIPLE, "getLongList()", 15, 64);
00321 db.addIndex("TESTDATAWITHARRAY", LongDescriptor);
00322 }
00323 } catch (Throwable ex) {
00324 ex.printStackTrace();
00325 }
00326 }
00327
00328 public void testDBSearch() {
00329 try {
00330 Array listdata = new Array(NB_SEARCH_CREATED_DATA);
00331 for (int i = 0; i < NB_SEARCH_CREATED_DATA; i++) {
00332 TestSearchData data = new TestSearchData(i);
00333 db.add("TESTSEARCH", data);
00334 listdata.add(data);
00335
00336 }
00337 this.testIndexIterator("TESTSEARCH", "getKey()", listdata);
00338 this.testInitIndexIterator("TESTSEARCH", "getKey()", listdata);
00339
00340
00341 Query q = db.query();
00342 q.constrain(TestSearchData.class);
00343 Query subq = q.descend("getKey()");
00344 ObjectSet set = q.execute();
00345 this.showSet(set, "QUERY select all : ");
00346
00347
00348 q = db.query();
00349 q.constrain(TestSearchData.class);
00350 subq = q.descend("getKey()");
00351 subq.constrain(new Integer(1)).equal();
00352 set = q.execute();
00353 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getKey=1");
00354
00355
00356 q = db.query();
00357 q.constrain(TestSearchData.class);
00358 subq = q.descend("getKey()");
00359 subq.constrain(new Integer(14)).smaller();
00360 set = q.execute();
00361 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getKey<14");
00362
00363
00364 q = db.query();
00365 q.constrain(TestSearchData.class);
00366 subq = q.descend("getKey()");
00367 subq.constrain(new Integer(24)).greater();
00368 set = q.execute();
00369 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getKey>24");
00370
00371
00372 q = db.query();
00373 q.constrain(TestSearchData.class);
00374 subq = q.descend("getKey()");
00375 Constraint c = subq.constrain(new Integer(16)).greater();
00376 Constraint d = subq.constrain(new Integer(23)).smaller();
00377 c.and(d);
00378 set = q.execute();
00379 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getkey>16 and getkey<23");
00380
00381
00382 q = db.query();
00383 q.constrain(TestSearchData.class);
00384 subq = q.descend("getKey()");
00385 c = subq.constrain(new Integer(16)).greater();
00386 Query subq2 = subq.descend("getDizaine()");
00387 d = subq2.constrain(new Integer(2)).equal();
00388 c.and(d);
00389 set = q.execute();
00390 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getkey>16 and getDizaine=2");
00391
00392
00393 q = db.query();
00394 q.constrain(TestSearchData.class);
00395 subq = q.descend("getKey()");
00396 c = subq.constrain(new Integer(16)).smaller();
00397 subq2 = subq.descend("getDizaine()");
00398 d = subq2.constrain(new Integer(2)).equal();
00399 c.or(d);
00400 set = q.execute();
00401 this.showSet(set, "QUERY SELECT * FROM TESTSEARCH where getkey<16 OR getDizaine=2");
00402
00403
00404 for (int i = 0; i < NB_SEARCH_CREATED_DATA; i++) {
00405 db.delete("TESTSEARCH", new TestSearchData(i));
00406 }
00407 } catch (Throwable ex) {
00408 ex.printStackTrace();
00409 }
00410
00411 }
00412
00413 private void showSet(ObjectSet set, String info) {
00414 LogManager.traceInfo(0, info);
00415 while (set.hasNext()) {
00416 LogManager.traceInfo(0, set.next());
00417 }
00418 }
00419
00420 public void TestDBAndIndex() {
00421 try {
00422
00423 Array inputDataList = new Array(100);
00424 Array inputTestDataList = new Array(100);
00425 String tempdir = System.getProperty("user.dir") + File.separator;
00426 BufferedReader reader = new BufferedReader(new FileReader(tempdir + "inputdata.txt"));
00427
00428 String line = null;
00429 LogManager.traceInfo(0, "Begin ADD");
00430 while ((line = reader.readLine()) != null) {
00431 int lineIndex = line.indexOf(' ');
00432 if (lineIndex != -1) {
00433 String key = line.substring(0, lineIndex);
00434 TestData data = new TestData(key);
00435 db.add("TESTDATA", data);
00436 inputTestDataList.add(data);
00437 inputDataList.add(key);
00438 this.addKeyToMap(key);
00439 objectMap.put(key, key);
00440
00441 }
00442
00443 }
00444 LogManager.traceInfo(0, "END ADD");
00445 this.testIndexIterator("TESTDATA", "getKey()", inputTestDataList);
00446 LogManager.traceInfo(0, "END ADD");
00447 this.testFindLike(inputDataList, db);
00448 this.testTree(inputDataList, this.db);
00449 this.testMultipleIndex(inputDataList, this.db);
00450 LogManager.traceInfo(0, "END TEST");
00451
00452 LogManager.traceInfo(0, "BEGIN DELETE");
00453 Array removeList = (Array) inputDataList.clone();
00454 int increment = 100;
00455 while (inputDataList.size() != 0) {
00456 for (int i = 0; i < inputDataList.size(); i = i + increment) {
00457 String key = ((String) inputDataList.get(i));
00458 db.delete("TESTDATA", new TestData(key));
00459 inputTestDataList.remove(i);
00460 inputDataList.remove(i);
00461 this.removeKeyToMap(key);
00462
00463
00464 }
00465 this.testTree(inputDataList, this.db);
00466 this.testMultipleIndex(inputDataList, this.db);
00467 this.testFindLike(inputDataList, this.db);
00468 increment = increment / 2;
00469 if (increment <= 0)
00470 increment = 1;
00471 }
00472 LogManager.traceInfo(0, "END DELETE");
00473
00474 this.testRomvedTree(removeList, db);
00475
00476 } catch (Throwable ex) {
00477 ex.printStackTrace();
00478 }
00479 }
00480
00481 public void testDataWithArray() {
00482 try {
00483 TestDataWithArray fruits = new TestDataWithArray("Fruits", 1000L);
00484 fruits.addElement("Fraise");
00485 fruits.addElement("Cerise");
00486 fruits.addElement("Pomme");
00487 fruits.addElement("Banane");
00488 for (int i = 0; i < 2; i++)
00489 fruits.addElement(i);
00490 for (long l = 1980L; l < 1984L; l++)
00491 fruits.addElement(l);
00492 db.add("TESTDATAWITHARRAY", fruits);
00493
00494 TestDataWithArray legumes = new TestDataWithArray("Legumes", 2000L);
00495 legumes.addElement("Patate");
00496 legumes.addElement("Haricot");
00497 legumes.addElement("Courgette");
00498 legumes.addElement("Concombre");
00499 legumes.addElement("Epinard");
00500 for (int i = 10; i < 12; i++)
00501 legumes.addElement(i);
00502 for (long l = 2005L; l < 2009L; l++)
00503 legumes.addElement(l);
00504 db.add("TESTDATAWITHARRAY", legumes);
00505
00506 Query q = db.query();
00507 q.constrain(TestDataWithArray.class);
00508 Query subq = q.descend("getId()");
00509 ObjectSet set = q.execute();
00510 this.showSet(set, "Tous les objets");
00511
00512 q = db.query();
00513 q.constrain(TestDataWithArray.class);
00514 subq = q.descend("getId()");
00515 subq.constrain("Fruits").equal();
00516 set = q.execute();
00517 this.showSet(set,"Les Fruits");
00518
00519 q = db.query();
00520 q.constrain(TestDataWithArray.class);
00521 subq = q.descend("getStringList()");
00522 set = q.execute();
00523 this.showSet(set,"Toutes les String");
00524
00525 q = db.query();
00526 q.constrain(TestDataWithArray.class);
00527 subq = q.descend("getStringList()");
00528 subq.constrain("Pomme").equal();
00529 set = q.execute();
00530 this.showSet(set,"Est-ce que la Pomme c'est un fruit ?");
00531
00532 q = db.query();
00533 q.constrain(TestDataWithArray.class);
00534 subq = q.descend("getIntList()");
00535 set = q.execute();
00536 this.showSet(set, "Tous les entiers");
00537
00538 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00539 subq.constrain(new Integer(1)).equal(); set = q.execute();
00540 this.showSet(set,"Groupe qui contient 1 (fruits)");
00541
00542 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00543 subq.constrain(new Integer(10)).equal(); set = q.execute();
00544 this.showSet(set,"Groupe qui contient 10 (legumes)");
00545
00546 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00547 subq.constrain(new Integer(10)).greater(); set = q.execute();
00548 this.showSet(set,"Groupe qui contient > 10 (legumes)");
00549
00550 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00551 subq.constrain(new Integer(15)).smaller(); set = q.execute();
00552 this.showSet(set,"Groupe qui contient < 15 (fruits et legumes)");
00553
00554 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00555 subq.constrain(new Integer(5)).smaller(); set = q.execute();
00556 this.showSet(set,"Groupe qui contient < 5 (fruits)");
00557
00558 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00559 subq.constrain(new Integer(9)).equal(); set = q.execute();
00560 this.showSet(set,"Groupe qui contient 9 (aucun)");
00561
00562 q = db.query();
00563 q.constrain(TestDataWithArray.class);
00564 subq = q.descend("getLongList()");
00565 subq.constrain(new Long(1981L)).smaller();
00566 set = q.execute();
00567 this.showSet(set, "L'objet qui inférieur à 1981L (fruits)");
00568
00569
00570 db.deleteWithId("TESTDATAWITHARRAY", "Fruits");
00571
00572 System.out.println("db.deleteWithId(\"TESTDATAWITHARRAY\",\"Fruits\");");
00573
00574 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00575 subq.constrain(new Integer(5)).equal(); set = q.execute();
00576 this.showSet(set,"Groupe qui contient 5 (fruits)");
00577
00578 q.constrain(TestDataWithArray.class); subq = q.descend("getIntList()");
00579 subq.constrain(new Integer(12)).equal(); set = q.execute();
00580 this.showSet(set,"Groupe qui contient 12 (legumes)");
00581
00582 db.deleteWithId("TESTDATAWITHARRAY", "Legumes");
00583 System.out.println("db.deleteWithId(\"TESTDATAWITHARRAY\",\"Legumes\");");
00584 } catch (Throwable ex) {
00585 ex.printStackTrace();
00586 }
00587 }
00588
00589 public static void main(String[] args) {
00590 LogManager.getInstance(null);
00591 TestFastObjectDB test = new TestFastObjectDB();
00592 test.openDB();
00593 test.TestDBAndIndex();
00594 test.testDBSearch();
00595
00596 test.testDataWithArray();
00597 }
00598 }