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
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02package com.db4odoc.diagnostics; 03 04import com.db4o.diagnostic.*; 05 06public class IndexDiagListener implements DiagnosticListener 07{ 08 public void onDiagnostic(Diagnostic d) { 09 if (d.getClass().equals(LoadedFromClassIndex.class)){ 10 System.out.println(d.toString()); 11 } 12 } 13}

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

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

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