This topic applies to Java version only
Note: This will not work with JDK1.1.
Native Query predicates can be optimized when they are loaded into JVM. In order to do that you should make use of db4o Enhancement Tools.
The idea is very simple:
Let's look how this is done on an example. We will use a well-known Pilot class, store it and use NQ to retrieve it:
1public static void main(String[] args) { 2
storePilots(); 3
selectPilot5Points(); 4
}
01private static void storePilots() { 02
new File(DB4O_FILE_NAME).delete(); 03
ObjectContainer container = database(configureNQ()); 04
if (container != null) { 05
try { 06
Pilot pilot; 07
for (int i = 0; i < OBJECT_COUNT; i++) { 08
pilot = new Pilot("Test Pilot #" + i, i); 09
container.set(pilot); 10
} 11
for (int i = 0; i < OBJECT_COUNT; i++) { 12
pilot = new Pilot("Professional Pilot #" + (i + 10), i + 10); 13
container.set(pilot); 14
} 15
container.commit(); 16
} catch (Db4oException ex) { 17
System.out.println("Db4o Exception: " + ex.getMessage()); 18
} catch (Exception ex) { 19
System.out.println("System Exception: " + ex.getMessage()); 20
} finally { 21
closeDatabase(); 22
} 23
} 24
}
01private static void selectPilot5Points() { 02
ObjectContainer container = database(configureNQ()); 03
if (container != null) { 04
try { 05
List<Pilot> result = container.query(new Predicate<Pilot>() { 06
public boolean match(Pilot pilot) { 07
// pilots with 5 points are included in the 08
// result 09
return pilot.getPoints() == 5; 10
} 11
}); 12
listResult(result); 13
} catch (Exception ex) { 14
System.out.println("System Exception: " + ex.getMessage()); 15
} finally { 16
closeDatabase(); 17
} 18
} 19
}
We will need to create a starter class, which will call the main method of the NQExample:
1public static void main(String[] args) throws Exception { 2
// Create class filter to point to the predicates to be optimized 3
ClassFilter filter = new ByNameClassFilter("com.db4odoc.nqoptimize.", true); 4
// Create NQ optimization class edit 5
BloatClassEdit[] edits = { new TranslateNQToSODAEdit()}; 6
URL[] urls = { new File("/work/workspaces/db4o/nqtest/bin").toURI().toURL() }; 7
// launch the application using the class edit and the filter 8
Db4oInstrumentationLauncher.launch(edits, urls, NQExample.class.getName(), new String[]{}); 9
}
That's all. Now you can run your application using NQEnhancedStarter and all the predicated will be optimized while they are loaded. This will also save time on optimization at runtime.