001: package com.db4o.f1.chapter3;
002:
003: import java.io.*;
004: import java.util.*;
005: import com.db4o.*;
006: import com.db4o.f1.*;
007: import com.db4o.query.*;
008:
009: public class CollectionsExample extends Util {
010: public static void main(String[] args) {
011: new File(Util.DB4OFILENAME).delete();
012: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
013: try {
014: storeFirstCar(db);
015: storeSecondCar(db);
016: retrieveAllSensorReadout(db);
017: retrieveSensorReadoutQBE(db);
018: retrieveCarQBE(db);
019: retrieveCollections(db);
020: retrieveArrays(db);
021: retrieveAllSensorReadoutNative(db);
022: retrieveSensorReadoutNative(db);
023: retrieveCarNative(db);
024: retrieveSensorReadoutQuery(db);
025: retrieveCarQuery(db);
026: db.close();
027: updateCarPart1();
028: db = Db4o.openFile(Util.DB4OFILENAME);
029: updateCarPart2(db);
030: updateCollection(db);
031: db.close();
032: deleteAllPart1();
033: db = Db4o.openFile(Util.DB4OFILENAME);
034: deleteAllPart2(db);
035: } finally {
036: db.close();
037: }
038: }
039:
040: public static void storeFirstCar(ObjectContainer db) {
041: Car car1 = new Car("Ferrari");
042: Pilot pilot1 = new Pilot("Michael Schumacher", 100);
043: car1.setPilot(pilot1);
044: db.set(car1);
045: }
046:
047: public static void storeSecondCar(ObjectContainer db) {
048: Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
049: Car car2 = new Car("BMW");
050: car2.setPilot(pilot2);
051: car2.snapshot();
052: car2.snapshot();
053: db.set(car2);
054: }
055:
056: public static void retrieveAllSensorReadout(ObjectContainer db) {
057: SensorReadout proto = new SensorReadout(null, null, null);
058: ObjectSet results = db.get(proto);
059: listResult(results);
060: }
061:
062: public static void retrieveAllSensorReadoutNative(ObjectContainer db) {
063: ObjectSet results = db.query(new Predicate() {
064: public boolean match(SensorReadout candidate) {
065: return true;
066: }
067: });
068: listResult(results);
069: }
070:
071: public static void retrieveSensorReadoutQBE(ObjectContainer db) {
072: SensorReadout proto = new SensorReadout(
073: new double[] { 0.3, 0.1 }, null, null);
074: ObjectSet results = db.get(proto);
075: listResult(results);
076: }
077:
078: public static void retrieveSensorReadoutNative(ObjectContainer db) {
079: ObjectSet results = db.query(new Predicate() {
080: public boolean match(SensorReadout candidate) {
081: return Arrays.binarySearch(candidate.getValues(), 0.3) >= 0
082: && Arrays.binarySearch(candidate.getValues(),
083: 1.0) >= 0;
084: }
085: });
086: listResult(results);
087: }
088:
089: public static void retrieveCarQBE(ObjectContainer db) {
090: SensorReadout protoreadout = new SensorReadout(new double[] {
091: 0.6, 0.2 }, null, null);
092: List protohistory = new ArrayList();
093: protohistory.add(protoreadout);
094: Car protocar = new Car(null, protohistory);
095: ObjectSet result = db.get(protocar);
096: listResult(result);
097: }
098:
099: public static void retrieveCarNative(ObjectContainer db) {
100: ObjectSet results = db.query(new Predicate() {
101: public boolean match(Car candidate) {
102: List history = candidate.getHistory();
103: for (int i = 0; i < history.size(); i++) {
104: SensorReadout readout = (SensorReadout) history
105: .get(i);
106: if (Arrays.binarySearch(readout.getValues(), 0.6) >= 0
107: || Arrays.binarySearch(readout.getValues(),
108: 0.2) >= 0)
109: return true;
110: }
111: return false;
112: }
113: });
114: listResult(results);
115: }
116:
117: public static void retrieveCollections(ObjectContainer db) {
118: ObjectSet result = db.get(new ArrayList());
119: listResult(result);
120: }
121:
122: public static void retrieveArrays(ObjectContainer db) {
123: ObjectSet result = db.get(new double[] { 0.6, 0.4 });
124: listResult(result);
125: }
126:
127: public static void retrieveSensorReadoutQuery(ObjectContainer db) {
128: Query query = db.query();
129: query.constrain(SensorReadout.class);
130: Query valuequery = query.descend("values");
131: valuequery.constrain(new Double(0.3));
132: valuequery.constrain(new Double(0.1));
133: ObjectSet result = query.execute();
134: listResult(result);
135: }
136:
137: public static void retrieveCarQuery(ObjectContainer db) {
138: Query query = db.query();
139: query.constrain(Car.class);
140: Query historyquery = query.descend("history");
141: historyquery.constrain(SensorReadout.class);
142: Query valuequery = historyquery.descend("values");
143: valuequery.constrain(new Double(0.3));
144: valuequery.constrain(new Double(0.1));
145: ObjectSet result = query.execute();
146: listResult(result);
147: }
148:
149: public static void updateCarPart1() {
150: Db4o.configure().objectClass(Car.class).cascadeOnUpdate(true);
151: }
152:
153: public static void updateCarPart2(ObjectContainer db) {
154: ObjectSet results = db.query(new Predicate() {
155: public boolean match(Car candidate) {
156: return true;
157: }
158: });
159: Car car = (Car) results.next();
160: car.snapshot();
161: db.set(car);
162: retrieveAllSensorReadoutNative(db);
163: }
164:
165: public static void updateCollection(ObjectContainer db) {
166: ObjectSet results = db.query(new Predicate() {
167: public boolean match(Car candidate) {
168: return true;
169: }
170: });
171: Car car = (Car) results.next();
172: car.getHistory().remove(0);
173: db.set(car.getHistory());
174: results = db.query(new Predicate() {
175: public boolean match(Car candidate) {
176: return true;
177: }
178: });
179: while (results.hasNext()) {
180: car = (Car) results.next();
181: for (int idx = 0; idx < car.getHistory().size(); idx++) {
182: System.out.println(car.getHistory().get(idx));
183: }
184: }
185: }
186:
187: public static void deleteAllPart1() {
188: Db4o.configure().objectClass(Car.class).cascadeOnDelete(true);
189: }
190:
191: public static void deleteAllPart2(ObjectContainer db) {
192: ObjectSet cars = db.query(new Predicate() {
193: public boolean match(Car candidate) {
194: return true;
195: }
196: });
197: while (cars.hasNext()) {
198: db.delete(cars.next());
199: }
200: ObjectSet readouts = db.query(new Predicate() {
201: public boolean match(SensorReadout candidate) {
202: return true;
203: }
204: });
205: while (readouts.hasNext()) {
206: db.delete(readouts.next());
207: }
208: }
209: }
|