Examining the db4o Object Database
by Rick Grehan

Listing One

(a)

public class Patient
{  private String name;
   private long patientID;
   private ArrayList weightHistory;
 ... methods for Patient ...
}
public class WeightEntry
{  private float weight;
   private long weightDate;
 ... methods for WeightEntry ...
}

(b)

public void addWeight(float _weight,
  long _timeMillis)
{
  // Create new WeightEntry
  WeightEntry _weightEntry =
    new WeightEntry(_weight, _timeMillis);
  // Attach it
  this.weightHistory.add(_weightEntry);
}


Listing Two

// Make the database replication-capable
Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);

// Open a database (Create if it does not exist)
ObjectContainer patientDB = Db4o.openFile("PATIENTA.YAP");
// Create a new Patient
Patient _patient = new Patient(
  "John Doe",
  001L);
// Add a weight
_patient.addWeight((float)190.0,
  java.lang.System.currentTimeMillis());
// Put the new patient in the database
patientDB.set(_patient);
// Commit the transaction and close the database
patientDB.commit();
patientDB.close();


Listing Three

// Set things up for replication
Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
// Open both databases
ObjectContainer patientADB = Db4o.openFile("PATIENTA.YAP");
ObjectContainer patientBDB = Db4o.openFile("PATIENTB.YAP");
// Create a ReplicationProcess object
  patientADB.ext().replicationBegin(
    patientBDB,
    new ReplicationConflictHandler() {
      public Object resolveConflict(
        ReplicationProcess replicationProcess,
        Object a,
        Object b) { return a; }
  });
//  PATIENTB
replication.setDirection(patientADB,patientBDB);
// Do the replication
Query q = patientADB.query();
ObjectSet replicationSet = q.execute();
while (replicationSet.hasNext()) {
  replication.replicate(replicationSet.next());
}
replication.commit();
// Close both databases
patientADB.close();


Listing Four

// Set things up for replication
Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
// Open both databases
ObjectContainer patientADB = Db4o.openFile("PATIENTA.YAP");
ObjectContainer patientBDB = Db4o.openFile("PATIENTB.YAP");
// Create a ReplicationProcess object
ReplicationProcess replication =
  patientBDB.ext().replicationBegin(
    patientADB,
      public Object resolveConflict(
        ReplicationProcess replicationProcess,
        Object b,
        Object a) { return b; }
  });
// Set the direction from PATIENTB to
replication.setDirection(patientBDB,patientADB);
// Do the replication
replication.whereModified(q);
ObjectSet replicationSet = q.execute();
while (replicationSet.hasNext()) {
  replication.replicate(replicationSet.next());
}
replication.commit();
// Close both databases
patientADB.close();
patientBDB.close();



2


