Collection Example

Db4o provides proprietary implementations for Map and List interfaces. Both implementations, when instantiated as a result of a query, are transparently activated when internal members are required to perform an operation. Db4o implementations provide an important advantage over JDK collections when running in transparent activation mode, based on the ability to control their activation.

ArrayList4 implements the List interface using an array to store elements. When an ArrayList4 instance is activated all the elements of the array are loaded into memory. On the other hand, ArrayMap4 implements the Map interface using two arrays to store keys and values. When an ArrayMap4 instance is activated all the elements of the arrays are loaded into memory.

We will use a Team class with a collection of Pilot objects:





Team.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using System.Collections.Generic; 03using System.Collections; 04 05using Db4objects.Db4o; 06using Db4objects.Db4o.Activation; 07using Db4objects.Db4o.TA; 08using Db4objects.Db4o.Collections; 09 10namespace Db4ojects.Db4odoc.TAExamples 11{ 12 public class Team : IActivatable 13 { 14 15 IList<Pilot> _pilots = new ArrayList4<Pilot>(); 16 17 18 19 string _name; 20 21 //TA Activator 22 [System.NonSerialized] 23 IActivator _activator; 24 25 26 // Bind the class to an object container 27 public void Bind(IActivator activator) 28 { 29 if (_activator == activator) 30 { 31 return; 32 } 33 if (activator != null && null != _activator) 34 { 35 throw new System.InvalidOperationException(); 36 } 37 _activator = activator; 38 } 39 40 // activate object fields 41 public void Activate(ActivationPurpose purpose) 42 { 43 if (_activator == null) return; 44 _activator.Activate(purpose); 45 } 46 47 public void AddPilot(Pilot pilot) 48 { 49 // activate before adding new pilots 50 Activate(ActivationPurpose.Read); 51 _pilots.Add(pilot); 52 } 53 54 public int Size() 55 { 56 // activate before returning 57 Activate(ActivationPurpose.Read); 58 return _pilots.Count; 59 } 60 // end Size 61 62 public IList<Pilot> Pilots 63 { 64 get 65 { 66 Activate(ActivationPurpose.Read); 67 return _pilots; 68 } 69 } 70 // end Pilots 71 72 } 73}


Pilot.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using Db4objects.Db4o; 03using Db4objects.Db4o.Activation; 04using Db4objects.Db4o.TA; 05 06namespace Db4ojects.Db4odoc.TAExamples 07{ 08 public class Pilot : IActivatable 09 { 10 private string _name; 11 12 [System.NonSerialized] 13 IActivator _activator; 14 15 public Pilot(string name) 16 { 17 _name = name; 18 } 19 20 // Bind the class to an object container 21 public void Bind(IActivator activator) 22 { 23 if (_activator == activator) 24 { 25 return; 26 } 27 if (activator != null && null != _activator) 28 { 29 throw new System.InvalidOperationException(); 30 } 31 _activator = activator; 32 } 33 34 // activate the object fields 35 public void Activate(ActivationPurpose purpose) 36 { 37 if (_activator == null) 38 return; 39 _activator.Activate(purpose); 40 } 41 42 public string Name 43 { 44 get 45 { 46 // even simple string needs to be activated 47 Activate(ActivationPurpose.Read); 48 return _name; 49 } 50 } 51 52 public override string ToString() 53 { 54 // use Name property, which already contains activation call 55 return Name; 56 } 57 } 58 59}






Team.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports System.Collections 03Imports Db4objects.Db4o 04Imports Db4objects.Db4o.Activation 05Imports Db4objects.Db4o.TA 06Imports Db4objects.Db4o.Collections 07 08 09Namespace Db4ojects.Db4odoc.TAExamples 10 11 Public Class Team 12 Implements IActivatable 13 14 Private _pilots As IList(Of Pilot) = New ArrayList4(Of Pilot) 15 Private _name As String 16 17 ' TA Activator 18 <Transient()> _ 19 Private _activator As IActivator 20 21 Public ReadOnly Property Pilots() As IList(Of Pilot) 22 Get 23 Activate(ActivationPurpose.Read) 24 Return _pilots 25 End Get 26 End Property 27 28 29 ' Bind the class to an object container 30 Public Sub Bind(ByVal activator As IActivator) Implements IActivatable.Bind 31 If _activator Is activator Then 32 Return 33 End If 34 If Not (activator Is Nothing Or _activator Is Nothing) Then 35 Throw New System.InvalidOperationException() 36 End If 37 _activator = activator 38 End Sub 39 40 ' activate object fields 41 Public Sub Activate(ByVal purpose As ActivationPurpose) Implements IActivatable.Activate 42 If _activator Is Nothing Then 43 Return 44 End If 45 _activator.Activate(ActivationPurpose.Read) 46 End Sub 47 48 Public Sub AddPilot(ByVal pilot As Pilot) 49 ' activate before adding new pilots 50 Activate(ActivationPurpose.Read) 51 _pilots.Add(pilot) 52 End Sub 53 54 Public Function Size() As Integer 55 ' activate before returning 56 Activate(ActivationPurpose.Read) 57 Return _pilots.Count 58 End Function 59 60 End Class 61End Namespace


Pilot.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o 03Imports Db4objects.Db4o.Activation 04Imports Db4objects.Db4o.TA 05 06Namespace Db4ojects.Db4odoc.TAExamples 07 08 Public Class Pilot 09 Implements IActivatable 10 Private _name As String 11 <Transient()> Private _activator As Db4objects.Db4o.Activation.IActivator 12 13 Public Sub New(ByVal name As String) 14 _name = name 15 End Sub 16 17 ' Bind the class to an object container 18 Public Sub Bind(ByVal activator As Activation.IActivator) Implements IActivatable.Bind 19 If _activator Is activator Then 20 Return 21 End If 22 If Not (activator Is Nothing Or _activator Is Nothing) Then 23 Throw New System.InvalidOperationException() 24 End If 25 _activator = activator 26 End Sub 27 28 ' activate the object fields 29 Public Sub Activate(ByVal purpose As ActivationPurpose) Implements IActivatable.Activate 30 If _activator Is Nothing Then 31 Return 32 End If 33 _activator.Activate(ActivationPurpose.Read) 34 End Sub 35 36 Public ReadOnly Property Name() As String 37 Get 38 ' even simple string needs to be activated 39 Activate(ActivationPurpose.Read) 40 Return _name 41 End Get 42 End Property 43 44 Public Overloads Overrides Function ToString() As String 45 ' use Name property, which already contains activation call 46 Return Name 47 End Function 48 49 End Class 50End Namespace




Store and retrieve.





TAExample.cs: StoreCollection
01private static void StoreCollection() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer container = Database(ConfigureTA()); 05 if (container != null) 06 { 07 try 08 { 09 Team team = new Team(); 10 for (int i = 0; i < 10; i++) 11 { 12 team.AddPilot(new Pilot("Pilot #" + i)); 13 } 14 container.Set(team); 15 container.Commit(); 16 } 17 catch (Exception ex) 18 { 19 System.Console.WriteLine(ex.StackTrace); 20 } 21 finally 22 { 23 CloseDatabase(); 24 } 25 } 26 }


TAExample.cs: TestCollectionActivation
01private static void TestCollectionActivation() 02 { 03 StoreCollection(); 04 IObjectContainer container = Database(ConfigureTA()); 05 if (container != null) 06 { 07 try 08 { 09 Team team = (Team)container.Get(new Team()).Next(); 10 for (int j = 0; j < team.Size(); j++) 11 { 12 System.Console.WriteLine(team.Pilots[j]); 13 } 14 } 15 catch (Exception ex) 16 { 17 System.Console.WriteLine(ex.StackTrace); 18 } 19 finally 20 { 21 CloseDatabase(); 22 } 23 } 24 }






TAExample.vb: StoreCollection
01Private Shared Sub StoreCollection() 02 File.Delete(Db4oFileName) 03 Dim container As IObjectContainer = Database(ConfigureTA) 04 If Not (container Is Nothing) Then 05 Try 06 Dim team As Team = New Team 07 Dim i As Integer = 0 08 While i < 10 09 team.AddPilot(New Pilot("Pilot #" + i.ToString())) 10 i = i + 1 11 End While 12 container.Set(team) 13 container.Commit() 14 Catch ex As Exception 15 System.Console.WriteLine(ex.StackTrace) 16 Finally 17 CloseDatabase() 18 End Try 19 End If 20 End Sub


TAExample.vb: TestCollectionActivation
01Private Shared Sub TestCollectionActivation() 02 StoreCollection() 03 Dim container As IObjectContainer = Database(ConfigureTA) 04 If Not (container Is Nothing) Then 05 Try 06 Dim team As Team = CType(container.Get(New Team).Next, Team) 07 Dim j As Integer 08 For j = 0 To team.Size - 1 09 System.Console.WriteLine(team.Pilots(j)) 10 Next 11 12 Catch ex As Exception 13 System.Console.WriteLine(ex.StackTrace) 14 Finally 15 CloseDatabase() 16 End Try 17 End If 18 End Sub