Diagnostic Messages Filter

The standard listeners can potentially produce quite a lot of messages. By writing your own DiagnosticListener you can filter that information.

On the stage of application tuning you can be interested in optimizing performance through indexing. Diagnostics can help you with that giving information about queries that are running on un-indexed fields. Having this information you can decide which queries are frequent and heavy and should be indexed, and which have little performance impact and do not need an index. Field indexes dramatically improve query performance but they may considerably reduce storage and update performance.

In order to get rid of all unnecessary diagnostic information and concentrate on indexes let's create special diagnostic listener:

IndexDiagListener.java
01package com.db4odoc.f1.diagnostics; 02 03import com.db4o.diagnostic.*; 04 05public class IndexDiagListener implements DiagnosticListener 06{ 07 public void onDiagnostic(Diagnostic d) { 08 if (d.getClass().equals(LoadedFromClassIndex.class)){ 09 System.out.println(d.toString()); 10 } 11 } 12}


The following command will install the new listener:

Java:Db4o.configure().diagnostic().addListener(new IndexDiagListener())

We can check the efficacy of IndexDiagListener using queries from the previous paragraphs:

DiagnosticExample.java: testIndexDiagnostics
01public static void testIndexDiagnostics() { 02 Db4o.configure().diagnostic().addListener(new IndexDiagListener()); 03 Db4o.configure().updateDepth(3); 04 new File(YAPFILENAME).delete(); 05 ObjectContainer db=Db4o.openFile(YAPFILENAME); 06 try { 07 Pilot pilot1 = new Pilot("Rubens Barrichello",99); 08 db.set(pilot1); 09 Pilot pilot2 = new Pilot("Michael Schumacher",100); 10 db.set(pilot2); 11 queryPilot(db); 12 setEmptyObject(db); 13 Query query = db.query(); 14 query.constrain(Pilot.class); 15 query.descend("points").constrain(new Integer(99)); 16 ObjectSet result = query.execute(); 17 listResult(result); 18 } 19 finally { 20 db.close(); 21 Db4o.configure().diagnostic().removeAllListeners(); 22 } 23 }

Potentially this piece of code triggers all the diagnostic objects, but we are getting only index warning messages due to IndexDiagListener.