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.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples; 04 05import java.util.*; 06 07import com.db4o.activation.*; 08import com.db4o.collections.*; 09import com.db4o.ta.*; 10 11 12public class Team implements Activatable { 13 14 List<Pilot> _pilots = new ArrayList4<Pilot>(); 15 16 String _name; 17 18 //TA Activator 19 transient Activator _activator; 20 21 // Bind the class to an object container 22 public void bind(Activator activator) { 23 if (_activator == activator) { 24 return; 25 } 26 if (activator != null && _activator != null) { 27 throw new IllegalStateException(); 28 } 29 _activator = activator; 30 } 31 32 // activate object fields 33 public void activate(ActivationPurpose purpose) { 34 if (_activator == null) return; 35 _activator.activate(purpose); 36 } 37 38 public void addPilot(Pilot pilot) { 39 // activate before adding new pilots 40 activate(ActivationPurpose.WRITE); 41 _pilots.add(pilot); 42 } 43 44 public void listAllPilots() { 45 // activate before printing the collection members 46 activate(ActivationPurpose.READ); 47 48 for (Iterator<Pilot> iter = _pilots.iterator(); iter.hasNext();) { 49 Pilot pilot = (Pilot) iter.next(); 50 System.out.println(pilot); 51 } 52 } 53}


Pilot.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.taexamples; 04 05import com.db4o.activation.*; 06import com.db4o.ta.Activatable; 07 08public class Pilot implements Activatable { 09 10 private String _name; 11 12 transient Activator _activator; 13 14 public Pilot(String name) { 15 _name = name; 16 } 17 18 // Bind the class to an object container 19 public void bind(Activator activator) { 20 if (_activator == activator) { 21 return; 22 } 23 if (activator != null && _activator != null) { 24 throw new IllegalStateException(); 25 } 26 _activator = activator; 27 } 28 29 // activate the object fields 30 public void activate(ActivationPurpose purpose) { 31 if (_activator == null) 32 return; 33 _activator.activate(purpose); 34 } 35 36 public String getName() { 37 // even simple String needs to be activated 38 activate(ActivationPurpose.READ); 39 return _name; 40 } 41 42 public String toString() { 43 // use getName method, which already contains activation call 44 return getName(); 45 } 46}








Store and retrieve.



TAExample.java: storeCollection
01private static void storeCollection() { 02 new File(DB4O_FILE_NAME).delete(); 03 ObjectContainer container = database(configureTA()); 04 if (container != null) { 05 try { 06 Team team = new Team(); 07 for (int i = 0; i < 10; i++) { 08 team.addPilot(new Pilot("Pilot #" + i)); 09 } 10 container.store(team); 11 container.commit(); 12 } catch (Exception ex) { 13 ex.printStackTrace(); 14 } finally { 15 closeDatabase(); 16 } 17 } 18 }


TAExample.java: testCollectionActivation
01private static void testCollectionActivation() { 02 storeCollection(); 03 ObjectContainer container = database(configureTA()); 04 if (container != null) { 05 try { 06 Team team = (Team) container.queryByExample(new Team()).next(); 07 // this method will activate all the members in the collection 08 team.listAllPilots(); 09 } catch (Exception ex) { 10 ex.printStackTrace(); 11 } finally { 12 closeDatabase(); 13 } 14 } 15 }