1 package com.db4odoc.f1.indexes; 2 3 import java.io.File ; 4 5 import com.db4o.Db4o; 6 import com.db4o.ObjectContainer; 7 import com.db4o.ObjectSet; 8 import com.db4o.query.Query; 9 import com.db4odoc.f1.Util; 10 11 12 public class IndexedExample extends Util { 13 public static void main(String [] args){ 14 IndexedExample.fillUpDB(); 15 IndexedExample.noIndex(); 16 IndexedExample.fullIndex(); 17 IndexedExample.pilotIndex(); 18 IndexedExample.pointsIndex(); 19 } 20 22 public static void noIndex() { 23 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 24 try { 25 Query query = db.query(); 26 query.constrain(Car.class); 27 query.descend("pilot").descend("points").constrain(new Integer (99)); 28 29 long t1 = System.currentTimeMillis(); 30 ObjectSet result = query.execute(); 31 long t2 = System.currentTimeMillis(); 32 long diff = t2 - t1; 33 System.out.println("Test 1: no indexes"); 34 System.out.println("Execution time="+diff + " ms"); 35 listResult(result); 36 } 37 finally { 38 db.close(); 39 } 40 } 41 43 public static void fillUpDB(){ 44 new File (Util.YAPFILENAME).delete(); 45 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 46 try { 47 for (int i=0; i<10000;i++){ 48 AddCar(db,i); 49 } 50 } 51 finally { 52 db.close(); 53 } 54 } 55 57 public static void pilotIndex() { 58 Db4o.configure().objectClass(Car.class).objectField("pilot").indexed(true); 59 Db4o.configure().objectClass(Pilot.class).objectField("points").indexed(false); 60 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 61 try { 62 Query query = db.query(); 63 query.constrain(Car.class); 64 query.descend("pilot").descend("points").constrain(new Integer (99)); 65 66 long t1 = System.currentTimeMillis(); 67 ObjectSet result = query.execute(); 68 long t2 = System.currentTimeMillis(); 69 long diff = t2 - t1; 70 System.out.println("Test 3: index on pilot"); 71 System.out.println("Execution time="+diff + " ms"); 72 listResult(result); 73 } 74 finally { 75 db.close(); 76 } 77 } 78 80 public static void pointsIndex() { 81 Db4o.configure().objectClass(Car.class).objectField("pilot").indexed(false); 82 Db4o.configure().objectClass(Pilot.class).objectField("points").indexed(true); 83 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 84 try { 85 Query query = db.query(); 86 query.constrain(Car.class); 87 query.descend("pilot").descend("points").constrain(new Integer (99)); 88 89 long t1 = System.currentTimeMillis(); 90 ObjectSet result = query.execute(); 91 long t2 = System.currentTimeMillis(); 92 long diff = t2 - t1; 93 System.out.println("Test 4: index on points"); 94 System.out.println("Execution time="+diff + " ms"); 95 listResult(result); 96 } 97 finally { 98 db.close(); 99 } 100 } 101 103 104 public static void fullIndex() { 105 Db4o.configure().objectClass(Car.class).objectField("pilot").indexed(true); 106 Db4o.configure().objectClass(Pilot.class).objectField("points").indexed(true); 107 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 108 try { 109 Query query = db.query(); 110 query.constrain(Car.class); 111 query.descend("pilot").descend("points").constrain(new Integer (99)); 112 113 long t1 = System.currentTimeMillis(); 114 ObjectSet result = query.execute(); 115 long t2 = System.currentTimeMillis(); 116 long diff = t2 - t1; 117 System.out.println("Test 2: index on pilot and points"); 118 System.out.println("Execution time="+diff + " ms"); 119 listResult(result); 120 } 121 finally { 122 db.close(); 123 } 124 } 125 127 128 private static void AddCar(ObjectContainer db, int points) 129 { 130 Car car = new Car("BMW"); 131 car.setPilot(new Pilot("Tester", points)); 132 db.set(car); 133 } 134 136 public static void listResult(ObjectSet result) { 137 System.out.println(result.size()); 138 while(result.hasNext()) { 139 System.out.println(result.next()); 140 } 141 } 142 144 } 145 | Popular Tags |