This topic applies to Java version only
Note: Instrumented optimized classes will work with JDK1.1, but the optimization process itself requires at least JDK 1.3.
In the previous topic we discussed how NQ optimization can be enabled on classes while they are loaded. In this topic we will look at even more convenient and performant way of enhancing classes to optimize NQ: during application build time.
For our example we will take the same classes as in the previous
example, with the exception of NQEnhancedStarter class, which won't be needed
for build-time enhancement. Its functionality will be fulfilled be the build
script. For this example we will create an ant script, which should be run
after the classes (or jar) is built.
For simplistic example our build script should:
This can be done with the following script:
01<?xml version="1.0"?> 02
03
<!-- 04
NQ optimization build time enhancement sample. 05
--> 06
07
<project name="nqenhance" default="buildall"> 08
09
<!-- 10
Set up the required class path for the enhancement task. 11
In a production environment, this will be composed of jars, of course. 12
--> 13
<path id="db4o.enhance.path"> 14
<pathelement path="${basedir}" /> 15
<fileset dir="lib"> 16
<include name="**/*.jar"/> 17
</fileset> 18
</path> 19
20
<!-- Define enhancement task. --> 21
<taskdef 22
name="db4o-enhance" 23
classname="com.db4o.instrumentation.ant.Db4oFileEnhancerAntTask" 24
classpathref="db4o.enhance.path" 25
loaderref="db4o.enhance.loader" /> 26
27
<typedef 28
name="native-query" 29
classname="com.db4o.nativequery.main.NQAntClassEditFactory" 30
classpathref="db4o.enhance.path" 31
loaderref="db4o.enhance.loader" /> 32
33
34
<target name="buildall"> 35
36
<!-- Create enhanced output directory--> 37
<mkdir dir="${basedir}/enhanced-bin" /> 38
<delete dir="${basedir}/enhanced-bin" quiet = "true"> 39
<include name="**/*"/> 40
</delete> 41
42
<db4o-enhance targetdir="${basedir}/enhanced-bin"> 43
44
<classpath refid="db4o.enhance.path" /> 45
<!-- Use compiled classes as an input --> 46
<sources dir="${basedir}/bin" /> 47
48
<!-- Call transparent activation enhancement --> 49
<native-query /> 50
51
</db4o-enhance> 52
53
</target> 54
55
56
57
</project>
In order to test this script:
Successfully executed build script will produce an instrumented copy of the project classes in enhanced-bin folder. You can check the results by running the following batch file from bin and enhanced-bin folders:
set
CLASSPATH=.;{$PROJECT_ROOT}\lib\db4o-x.x-java5.jar
java
com.db4odoc.nqoptimize.NQExample
Of course, the presented example is very simple and limited in functionality. In fact you can do a lot more things using the build script:
o Add TA instrumentation in the same enhancer task
o Use ClassFilter to select classes for enhancement
o Use regex to select classes for enhancement
o Use several source folders
o Use jar as the source for enhancement
An example of the above features can be found in our Project Spaces.