Fast collections

Db4o's solution for the best collection performance and lowest memory consumption is to implement them directly on top of BTrees without an intermediate "stored-object-db4o" layer (P1Object, P1Collection, P2LinkedList).

This task is still under development, but already it makes sense to be ready to switch to the new fast collections seamlessly.

Current recommendation for collection usage with db4o is:

Please, avoid the following realizations, which will make the switching more difficult:

Let's look at application design, which will allow you to upgrade your application to fast collections with the least effort.

In our example we will save a list of pilots as members of one team. To make it simple let's use the following factory class to get the proper list implementation:

CollectionFactory.java
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.f1.lists; 04 05import java.util.ArrayList; 06import java.util.List; 07 08public class CollectionFactory { 09 public static List newList(){ 10 return new ArrayList(); 11 } 12}

The concrete class returned by the CollectionFactory can be changed to any other collection implementation (fast collection) with the minimum coding effort.

We will use the following class as a team of pilots:

Team.java
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.f1.lists; 04 05import java.util.List; 06 07import com.db4odoc.f1.evaluations.Pilot; 08 09public class Team { 10 private List pilots; 11 private String name; 12 13 public Team(){ 14 pilots = CollectionFactory.newList(); 15 } 16 17 public void setName(String name){ 18 this.name = name; 19 } 20 21 public String getName(){ 22 return name; 23 } 24 25 public void addPilot(Pilot pilot){ 26 pilots.add(pilot); 27 } 28 29 public Pilot getPilot(int index){ 30 return (Pilot)pilots.get(index); 31 } 32 33 public void removePilot(int index){ 34 pilots.remove(index); 35 } 36 37 public void updatePilot(int index, Pilot newPilot){ 38 pilots.set(index, newPilot); 39 } 40}

Let's try to store our team:

CollectionExample.java: setTeam
01public static void setTeam(){ 02 new File(Util.YAPFILENAME).delete(); 03 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 04 try { 05 Team ferrariTeam = new Team(); 06 ferrariTeam.setName("Ferrari"); 07 08 Pilot pilot1 = new Pilot("Michael Schumacher", 100); 09 ferrariTeam.addPilot(pilot1); 10 Pilot pilot2 = new Pilot("David Schumacher", 98); 11 ferrariTeam.addPilot(pilot2); 12 13 db.set(ferrariTeam); 14 List protoList = CollectionFactory.newList(); 15 ObjectSet result = db.get(protoList); 16 listResult(result); 17 } finally { 18 db.close(); 19 } 20 }

If we want to update one of the pilots, we will have to retrieve the whole collection:

CollectionExample.java: updateTeam
01public static void updateTeam(){ 02 ObjectContainer db=Db4o.openFile(Util.YAPFILENAME); 03 try { 04 Query query =db.query(); 05 query.constrain(Team.class); 06 query.descend("name").constrain("Ferrari"); 07 ObjectSet result = query.execute(); 08 if (result.hasNext()) { 09 Team ferrariTeam = (Team)result.next(); 10 11 Pilot pilot = new Pilot("David Schumacher", 100); 12 ferrariTeam.updatePilot(1,pilot); 13 14 db.set(ferrariTeam); 15 } 16 List protoList = CollectionFactory.newList(); 17 result = db.get(protoList); 18 listResult(result); 19 } finally { 20 db.close(); 21 } 22 }

The idea of the new fast collection implementation is to allow select/update of collection elements without an intermediate "stored-object-db4o" layer. This will allow random activation and fast querying, thus providing a considerable performance improvement especially on big collections holding deep object graphs.