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

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.