Thank you Carl,
> If you want to measure the difference between indexed
> queries and non-indexed queries in one app, you should
> test storing and retrieving objects of different classes.
I cut the example to only one searching method - fullIndex:
import java.io.File;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.query.Query;
class Car {
private String model;
private Pilot pilot;
public Car(String model) {
this.model=model;
this.pilot=null;
}
public Pilot getPilot() {
return pilot;
}
public void setPilot(Pilot pilot) {
this.pilot = pilot;
}
public String getModel() {
return model;
}
public String toString() {
return model+"["+pilot+"]";
}
}
class Pilot {
private String name;
private int points;
public Pilot(final String name, final int points) {
this.name = name;
this.points = points;
}
public int getPoints() {
return points;
}
public void addPoints(int points) {
this.points+=points;
}
public String getName() {
return name;
}
public String toString() {
return name+"/"+points;
}
}
public class FullIndexedExample{
private static final int COUNTQUERIES = 20;
private static ObjectContainer db;
public static void main(String[] args) {
fillUpDB();
fullIndex();
}
public static void fillUpDB(){
new File("formule1.yap").delete();
db = Db4o.openFile("formule1.yap");
int count = 1000;
try {
for (int i=0; i<count;i++){
addCar(i);
}
db.commit();
}
finally {
db.close();
}
}
public static void fullIndex() {
Db4o.configure().objectClass(Car.class).objectField("pilot").indexed(true);
Db4o.configure().objectClass(Pilot.class).objectField("points").indexed(true);
db=Db4o.openFile("formule1.yap");
try {
long avgTime = avgTime();
System.out.println("Test: index on pilot and points");
System.out.println("Execution time=" + avgTime + " ms");
}
finally {
db.close();
}
}
private static void addCar(final int points){
Car car = new Car("BMW");
car.setPilot(new Pilot("pilot" + points, points));
db.set(car);
}
private static long avgTime(){
Query query = db.query();
query.constrain(Car.class);
query.descend("pilot").descend("points").constrain(99);
long sumType = 0;
for(int i = 0; i<COUNTQUERIES; i++){
long t1 = System.currentTimeMillis();
query.execute();
long t2 = System.currentTimeMillis();
long diff = t2 - t1;
sumType += diff;
}
return sumType/COUNTQUERIES;
}
}
I watched in Object Manager into formule1.yap and both fields were indexed. But result is average searching time 37 ms among 1000 objects. When I tried to change field pilot from Pilot class to String, searching time was 0ms. So I see problem in my example is in indexing associated class Pilot.
What I do bad?
Jan Chodura