01: package com.db4o.f1.chapter5;
02:
03: import java.io.*;
04: import com.db4o.*;
05: import com.db4o.f1.*;
06:
07: public class DeepExample extends Util {
08: public static void main(String[] args) {
09: new File(Util.DB4OFILENAME).delete();
10: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
11: try {
12: storeCar(db);
13: db.close();
14: setCascadeOnUpdate();
15: db = Db4o.openFile(Util.DB4OFILENAME);
16: takeManySnapshots(db);
17: db.close();
18: db = Db4o.openFile(Util.DB4OFILENAME);
19: retrieveAllSnapshots(db);
20: db.close();
21: db = Db4o.openFile(Util.DB4OFILENAME);
22: retrieveSnapshotsSequentially(db);
23: retrieveSnapshotsSequentiallyImproved(db);
24: db.close();
25: setActivationDepth();
26: db = Db4o.openFile(Util.DB4OFILENAME);
27: retrieveSnapshotsSequentially(db);
28: } finally {
29: db.close();
30: }
31: }
32:
33: public static void storeCar(ObjectContainer db) {
34: Pilot pilot = new Pilot("Rubens Barrichello", 99);
35: Car car = new Car("BMW");
36: car.setPilot(pilot);
37: db.set(car);
38: }
39:
40: public static void setCascadeOnUpdate() {
41: Db4o.configure().objectClass(Car.class).cascadeOnUpdate(true);
42: }
43:
44: public static void takeManySnapshots(ObjectContainer db) {
45: ObjectSet result = db.get(Car.class);
46: Car car = (Car) result.next();
47: for (int i = 0; i < 5; i++) {
48: car.snapshot();
49: }
50: db.set(car);
51: }
52:
53: public static void retrieveAllSnapshots(ObjectContainer db) {
54: ObjectSet result = db.get(SensorReadout.class);
55: while (result.hasNext()) {
56: System.out.println(result.next());
57: }
58: }
59:
60: public static void retrieveSnapshotsSequentially(ObjectContainer db) {
61: ObjectSet result = db.get(Car.class);
62: Car car = (Car) result.next();
63: SensorReadout readout = car.getHistory();
64: while (readout != null) {
65: System.out.println(readout);
66: readout = readout.getNext();
67: }
68: }
69:
70: public static void retrieveSnapshotsSequentiallyImproved(
71: ObjectContainer db) {
72: ObjectSet result = db.get(Car.class);
73: Car car = (Car) result.next();
74: SensorReadout readout = car.getHistory();
75: while (readout != null) {
76: db.activate(readout, 1);
77: System.out.println(readout);
78: readout = readout.getNext();
79: }
80: }
81:
82: public static void setActivationDepth() {
83: Db4o.configure().objectClass(TemperatureSensorReadout.class)
84: .cascadeOnActivate(true);
85: }
86: }
|