KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > f1 > chapter21 > IndexedExample


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