8. Deep graphs


We have already seen how db4o handles object associations, but our running example is still quite flat and simple, compared to real-world domain models. In particular we haven't seen how db4o behaves in the presence of recursive structures. We will emulate such a structure by replacing our history list with a linked list implicitely provided by the SensorReadout class.

package com.db4odoc.f1.chapter6;
import java.util.*;
public class SensorReadout {
    private Date time;
    private Car car;
    private String description;
    private SensorReadout next;
    protected SensorReadout(Date time,Car car,String description) {
        this.time=time;
        this.car=car;
        this.description=description;
        this.next=null;
    }
    public Car getCar() {
        return car;
    }
    public Date getTime() {
        return time;
    }
    public String getDescription() {
        return description;
    }
    public SensorReadout getNext() {
        return next;
    }
    
    public void append(SensorReadout readout) {
        if(next==null) {
            next=readout;
        }
        else {
            next.append(readout);
        }
    }
    
    public int countElements() {
        return (next==null ? 1 : next.countElements()+1);
    }
    
    public String toString() {
        return car+" : "+time+" : "+description;
    }
}


Our car only maintains an association to a 'head' sensor readout now.

package com.db4odoc.f1.chapter6;
import java.util.*;
public class Car {
    private String model;
    private Pilot pilot;
    private SensorReadout history;
    public Car(String model) {
        this.model=model;
        this.pilot=null;
        this.history=null;
    }
    public Pilot getPilot() {
        return pilot;
    }
    public void setPilot(Pilot pilot) {
        this.pilot=pilot;
    }
    public String getModel() {
        return model;
    }
    public SensorReadout getHistory() {
        return history;
    }
    
    public void snapshot() {        
        appendToHistory(new TemperatureSensorReadout(
                new Date(),this,"oil",pollOilTemperature()));
        appendToHistory(new TemperatureSensorReadout(
                new Date(),this,"water",pollWaterTemperature()));
        appendToHistory(new PressureSensorReadout(
                new Date(),this,"oil",pollOilPressure()));
    }
    protected double pollOilTemperature() {
        return 0.1*countHistoryElements();
    }
    protected double pollWaterTemperature() {
        return 0.2*countHistoryElements();
    }
    protected double pollOilPressure() {
        return 0.3*countHistoryElements();
    }
    public String toString() {
        return model+"["+pilot+"]/"+countHistoryElements();
    }
    
    private int countHistoryElements() {
        return (history==null ? 0 : history.countElements());
    }
    
    private void appendToHistory(SensorReadout readout) {
        if(history==null) {
            history=readout;
        }
        else {
            history.append(readout);
        }
    }
}



    8.1. Storing and updating


    No surprises here.

    // storeCar
    Pilot pilot = new Pilot("Rubens Barrichello", 99);
    Car car = new Car("BMW");
    car.setPilot(pilot);
    db.store(car);


    Now we would like to build a sensor readout chain. We already know about the update depth trap, so we configure this first.

    // takeManySnapshots
    EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
    config.common().objectClass(Car.class).cascadeOnUpdate(true);
    ObjectContainer db = Db4oEmbedded.openFile(config, DB4OFILENAME);
    ObjectSet result = db.queryByExample(Car.class);
    if (result.hasNext()) {
        Car car = (Car) result.next();
        for (int i = 0; i < 5; i++) {
            car.snapshot();
        }
        db.store(car);
    }
    db.close();



    8.2. Retrieving


    Now that we have a sufficiently deep structure, we'll retrieve it from the database and traverse it.

    First let's verify that we indeed have taken lots of snapshots.

    // retrieveAllSnapshots
    ObjectSet result = db.queryByExample(SensorReadout.class);
    while (result.hasNext()) {
        System.out.println(result.next());
    }


    All these readouts belong to one linked list, so we should be able to access them all by just traversing our list structure.

    // retrieveSnapshotsSequentially
    ObjectSet result = db.queryByExample(Car.class);
    Car car = (Car) result.next();
    SensorReadout readout = car.getHistory();
    while (readout != null) {
        System.out.println(readout);
        readout = readout.getNext();
    }


    Ouch! What's happening here?



      8.2.1. Activation depth


      Deja vu - this is just the other side of the update depth issue.

      db4o cannot track when you are traversing references from objects retrieved from the database. So it would always have to return 'complete' object graphs on retrieval - in the worst case this would boil down to pulling the whole database content into memory for a single query.

      This is absolutely undesirable in most situations, so db4o provides a mechanism to give the client fine-grained control over how much he wants to pull out of the database when asking for an object. This mechanism is called activation depth and works quite similar to our familiar update depth.

      The default activation depth for any object is 5, so our example above runs into nulls after traversing 5 references.

      We can dynamically ask objects to activate their member references. This allows us to retrieve each single sensor readout in the list from the database just as needed.

      // retrieveSnapshotsSequentiallyImproved
      ObjectSet result = db.queryByExample(Car.class);
      Car car = (Car) result.next();
      SensorReadout readout = car.getHistory();
      while (readout != null) {
          db.activate(readout, 1);
          System.out.println(readout);
          readout = readout.getNext();
      }


      Note that 'cut' references may also influence the behavior of your objects: in this case the length of the list is calculated dynamically, and therefor constrained by activation depth.

      Instead of dynamically activating subgraph elements, you can configure activation depth statically, too. We can tell our SensorReadout class objects to cascade activation automatically, for example.

      // retrieveSnapshotsSequentiallyCascade
      EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
      config.common().objectClass(TemperatureSensorReadout.class).cascadeOnActivate(
              true);
      ObjectContainer db = Db4oEmbedded.openFile(config, DB4OFILENAME);
      ObjectSet result = db.queryByExample(Car.class);
      if (result.hasNext()) {
          Car car = (Car) result.next();
          SensorReadout readout = car.getHistory();
          while (readout != null) {
              System.out.println(readout);
              readout = readout.getNext();
          }
      }
      db.close();


      You have to be very careful, though. Activation issues are tricky. Db4o provides a wide range of configuration features to control activation depth at a very fine-grained level. You'll find those triggers in com.db4o.config.Configuration and the associated ObjectClass and ObjectField classes.

      Don't forget to clean up the database.

      // deleteAll
      ObjectSet result=db.queryByExample(new Object());
      while(result.hasNext()) {
          db.delete(result.next());
      }



    8.3. Conclusion


    Now we should have the tools at hand to work with arbitrarily complex object graphs. But so far we have only been working forward, hoping that the changes we apply to our precious data pool are correct. What if we have to roll back to a previous state due to some failure? In the next chapter  we will introduce the db4o transaction concept.


    8.4. Full source


    package com.db4odoc.f1.chapter6;
    import com.db4o.Db4oEmbedded;
    import com.db4o.ObjectContainer;
    import com.db4o.ObjectSet;
    import com.db4o.config.EmbeddedConfiguration;
    import com.db4odoc.f1.Util;
    import java.io.File;
    public class DeepExample extends Util {
        final static String DB4OFILENAME = System.getProperty("user.home") + "/formula1.db4o";
        public static void main(String[] args) {
            new File(DB4OFILENAME).delete();
            ObjectContainer db = Db4oEmbedded.openFile(DB4OFILENAME);
            storeCar(db);
            db.close();
            db = Db4oEmbedded.openFile(DB4OFILENAME);
            db.close();
            takeManySnapshots();
            db = Db4oEmbedded.openFile(DB4OFILENAME);
            retrieveAllSnapshots(db);
            db.close();
            db = Db4oEmbedded.openFile(DB4OFILENAME);
            retrieveSnapshotsSequentially(db);
            retrieveSnapshotsSequentiallyImproved(db);
            db.close();
            retrieveSnapshotsSequentiallyCascade();
        }
        public static void storeCar(ObjectContainer db) {
            Pilot pilot = new Pilot("Rubens Barrichello", 99);
            Car car = new Car("BMW");
            car.setPilot(pilot);
            db.store(car);
        }
        public static void takeManySnapshots() {
            EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
            config.common().objectClass(Car.class).cascadeOnUpdate(true);
            ObjectContainer db = Db4oEmbedded.openFile(config, DB4OFILENAME);
            ObjectSet result = db.queryByExample(Car.class);
            if (result.hasNext()) {
                Car car = (Car) result.next();
                for (int i = 0; i < 5; i++) {
                    car.snapshot();
                }
                db.store(car);
            }
            db.close();
        }
        public static void retrieveAllSnapshots(ObjectContainer db) {
            ObjectSet result = db.queryByExample(SensorReadout.class);
            while (result.hasNext()) {
                System.out.println(result.next());
            }
        }
        public static void retrieveSnapshotsSequentially(ObjectContainer db) {
            ObjectSet result = db.queryByExample(Car.class);
            Car car = (Car) result.next();
            SensorReadout readout = car.getHistory();
            while (readout != null) {
                System.out.println(readout);
                readout = readout.getNext();
            }
        }
        public static void retrieveSnapshotsSequentiallyCascade() {
            EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
            config.common().objectClass(TemperatureSensorReadout.class).cascadeOnActivate(
                    true);
            ObjectContainer db = Db4oEmbedded.openFile(config, DB4OFILENAME);
            ObjectSet result = db.queryByExample(Car.class);
            if (result.hasNext()) {
                Car car = (Car) result.next();
                SensorReadout readout = car.getHistory();
                while (readout != null) {
                    System.out.println(readout);
                    readout = readout.getNext();
                }
            }
            db.close();
        }
        public static void retrieveSnapshotsSequentiallyImproved(ObjectContainer db) {
            ObjectSet result = db.queryByExample(Car.class);
            Car car = (Car) result.next();
            SensorReadout readout = car.getHistory();
            while (readout != null) {
                db.activate(readout, 1);
                System.out.println(readout);
                readout = readout.getNext();
            }
        }
    }