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.cs
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02 03using System; 04using System.Diagnostics; 05using Db4objects.Db4o.Diagnostic; 06 07namespace Db4objects.Db4odoc.Diagnostics 08{ 09 public class IndexDiagListener: DiagnosticToConsole 10 { 11 override public void OnDiagnostic(Db4objects.Db4o.Diagnostic.IDiagnostic d) 12 { 13 if (d.GetType().Equals(typeof(Db4objects.Db4o.Diagnostic.LoadedFromClassIndex))) 14 { 15 System.Console.WriteLine(d.ToString()); 16 } 17 } 18 } 19}

IndexDiagListener.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02 03Imports System 04Imports System.Diagnostics 05Imports Db4objects.Db4o 06Imports Db4objects.Db4o.Diagnostic 07 08Namespace Db4objects.Db4odoc.Diagnostics 09 Public Class IndexDiagListener 10 11 Implements IDiagnosticListener 12 Public Sub OnDiagnostic(ByVal d As IDiagnostic) Implements IDiagnosticListener.OnDiagnostic 13 If TypeOf d Is LoadedFromClassIndex Then 14 System.Console.WriteLine(d) 15 End If 16 End Sub 17 End Class 18End Namespace


The following command will install the new listener:

c#:

configuration.Diagnostic().AddListener(new IndexDiagListener())

VB:

configuration.Diagnostic().AddListener(new IndexDiagListener())

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

DiagnosticExample.cs: TestIndexDiagnostics
01public static void TestIndexDiagnostics() { 02 Db4oFactory.Configure().Diagnostic().RemoveAllListeners(); 03 Db4oFactory.Configure().Diagnostic().AddListener(new IndexDiagListener()); 04 Db4oFactory.Configure().UpdateDepth(3); 05 File.Delete(YapFileName); 06 IObjectContainer db=Db4oFactory.OpenFile(YapFileName); 07 try { 08 Pilot pilot1 = new Pilot("Rubens Barrichello",99); 09 db.Set(pilot1); 10 Pilot pilot2 = new Pilot("Michael Schumacher",100); 11 db.Set(pilot2); 12 QueryPilot(db); 13 SetEmptyObject(db); 14 IQuery query = db.Query(); 15 query.Constrain(typeof(Pilot)); 16 query.Descend("_points").Constrain("99"); 17 IObjectSet result = query.Execute(); 18 ListResult(result); 19 } 20 finally { 21 db.Close(); 22 } 23 }

DiagnosticExample.vb: TestIndexDiagnostics
01Public Shared Sub TestIndexDiagnostics() 02 Db4oFactory.Configure().Diagnostic().RemoveAllListeners() 03 Db4oFactory.Configure().Diagnostic().AddListener(New IndexDiagListener()) 04 Db4oFactory.Configure().UpdateDepth(3) 05 File.Delete(YapFileName) 06 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 07 Try 08 Dim pilot1 As Pilot = New Pilot("Rubens Barrichello", 99) 09 db.Set(pilot1) 10 Dim pilot2 As Pilot = New Pilot("Michael Schumacher", 100) 11 db.Set(pilot2) 12 QueryPilot(db) 13 SetEmptyObject(db) 14 Dim query As IQuery = db.Query() 15 query.Constrain(GetType(Pilot)) 16 query.Descend("_points").Constrain("99") 17 Dim result As IObjectSet = query.Execute() 18 ListResult(result) 19 Finally 20 db.Close() 21 End Try 22 End Sub

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