Load Time Enhancement

This topic applies to Java version only

The following code is used to store and retrieve MaintenanceQueue objects containing references to Car, Pilot and Id objects:

EnhancerMain.java: main
01public static void main(String[] args) { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer db = Db4o.openFile(configuration(), DB4O_FILE_NAME); 04 MaintenanceQueue<Car> queue = null; 05 for(int number = 0; number < DEPTH; number++) { 06 queue = MaintenanceQueue.add(queue, new Car("Car" + number, 07 new Pilot("Pilot #" + number, new Id("110021" + number)))); 08 } 09 db.set(queue); 10 db.close(); 11 12 db = Db4o.openFile(configuration(), DB4O_FILE_NAME); 13 EventRegistry registry = EventRegistryFactory.forObjectContainer(db); 14 registry.activated().addListener(new EventListener4() { 15 public void onEvent(Event4 event, EventArgs args) { 16 ObjectEventArgs objArgs = (ObjectEventArgs) args; 17 System.out.println("ACTIVATED: " + objArgs.object()); 18 } 19 }); 20 ((ObjectContainerBase)db).getNativeQueryHandler().addListener(new Db4oQueryExecutionListener() { 21 public void notifyQueryExecuted(NQOptimizationInfo info) { 22 System.out.println(info); 23 } 24 }); 25 26 List<MaintenanceQueue<Car>> result = db.query(new Predicate<MaintenanceQueue<Car>>() { 27 @Override 28 public boolean match(MaintenanceQueue<Car> queue) { 29 return queue.value().getModel().equals("Car0"); 30 } 31 }); 32 System.out.println(result.size()); 33 //for (Iterator<MaintenanceQueue<Car>> i = result.iterator(); i.hasNext();){ 34 MaintenanceQueue<Car> carQueue = result.get(0); 35 Car car = carQueue.value(); 36 System.out.println(car); 37 38 Pilot pilot = car.getPilot(); 39 System.out.println(pilot); 40 while (carQueue.hasNext()){ 41 carQueue = carQueue.next(); 42 car = carQueue.value(); 43 System.out.println(car); 44 45 pilot = car.getPilot(); 46 System.out.println(pilot); 47 } 48 //} 49 db.close(); 50 new File(DB4O_FILE_NAME).delete(); 51 }
EnhancerMain.java: configuration
1private static Configuration configuration() { 2 Configuration config = Db4o.newConfiguration(); 3 config.add(new TransparentActivationSupport()); 4 // NOTE: required for load time instrumentation! 5 config.reflectWith(new JdkReflector(EnhancerMain.class.getClassLoader())); 6 return config; 7 }

Please, run this method to see that in TA mode all the objects are fully activated immediately. Also NQ info reports that the queries run dynamically optimized.

In order to use TA advantages (lazy activation), we launch the application through an instrumenting classloader. The following configuration options are available:

EnhancerStarter.java: main
1public static void main(String[] args) throws Exception { 2 ClassFilter filter = new ByNameClassFilter("enhancement.", true); 3 BloatClassEdit[] edits = { new TranslateNQToSODAEdit() , new InjectTransparentActivationEdit(filter) }; 4 URL[] urls = { new File("/work/workspaces/db4o/tatest/bin").toURI().toURL() }; 5 Db4oInstrumentationLauncher.launch(edits, urls, EnhancerMain.class.getName(), new String[]{}); 6 }

Try this code now - if everything is correct you will see that the objects are getting activated as they are requested. NQ info also should say that the queries are preoptimized.

Note that for load time instrumentation to work, the application code has to make sure db4o operates on the appropriate classloader for the persistent model classes.