1 2 3 package com.db4odoc.f1.identity; 4 5 import java.io.File ; 6 7 import com.db4o.Db4o; 8 import com.db4o.ObjectContainer; 9 import com.db4o.ObjectSet; 10 import com.db4o.query.Query; 11 import com.db4odoc.f1.Util; 12 13 public class IdentityExample extends Util { 14 15 16 public static void main(String [] args) { 17 setObjects(); 18 checkUniqueness(); 19 checkReferenceCache(); 20 checkReferenceCacheWithPurge(); 21 testBind(); 22 23 testCopyingWithPurge(); 24 } 25 27 public static void setObjects(){ 28 new File (Util.YAPFILENAME).delete(); 29 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 30 try { 31 Car car = new Car("BMW", new Pilot("Rubens Barrichello")); 32 db.set(car); 33 car = new Car("Ferrari", new Pilot("Michael Schumacher")); 34 db.set(car); 35 } finally { 36 db.close(); 37 } 38 } 39 41 public static void checkUniqueness(){ 42 setObjects(); 43 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 44 try { 45 ObjectSet cars = db.query(Car.class); 46 Car car = (Car)cars.get(0); 47 String pilotName = car.getPilot().getName(); 48 ObjectSet pilots = db.get(new Pilot(pilotName)); 49 Pilot pilot = (Pilot)pilots.get(0); 50 System.out.println("Retrieved objects are identical: " + (pilot == car.getPilot())); 51 } finally { 52 db.close(); 53 } 54 } 55 57 public static void checkReferenceCache(){ 58 setObjects(); 59 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 60 try { 61 ObjectSet pilots = db.query(Pilot.class); 62 Pilot pilot = (Pilot)pilots.get(0); 63 String pilotName = pilot.getName(); 64 pilot.setName("new name"); 65 System.out.println("Retrieving pilot by name: " + pilotName); 66 ObjectSet pilots1 = db.get(new Pilot(pilotName)); 67 listResult(pilots1); 68 } finally { 69 db.close(); 70 } 71 } 72 74 public static void checkReferenceCacheWithPurge(){ 75 setObjects(); 76 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 77 try { 78 ObjectSet pilots = db.query(Pilot.class); 79 Pilot pilot = (Pilot)pilots.get(0); 80 String pilotName = pilot.getName(); 81 pilot.setName("new name"); 82 System.out.println("Retrieving pilot by name: " + pilotName); 83 long pilotID = db.ext().getID(pilot); 84 if (db.ext().isCached(pilotID)){ 85 db.ext().purge(pilot); 86 } 87 ObjectSet pilots1 = db.get(new Pilot(pilotName)); 88 listResult(pilots1); 89 } finally { 90 db.close(); 91 } 92 } 93 95 public static void testCopyingWithPurge(){ 96 setObjects(); 97 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 98 try { 99 ObjectSet pilots = db.query(Pilot.class); 100 Pilot pilot = (Pilot)pilots.get(0); 101 db.ext().purge(pilot); 102 db.set(pilot); 103 pilots = db.query(Pilot.class); 104 listResult(pilots); 105 } finally { 106 db.close(); 107 } 108 } 109 111 public static void testBind(){ 112 setObjects(); 113 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 114 try { 115 Query q = db.query(); 116 q.constrain(Car.class); 117 q.descend("model").constrain("Ferrari"); 118 ObjectSet result = q.execute(); 119 Car car1 = (Car)result.get(0); 120 long IdCar1 = db.ext().getID(car1); 121 Car car2 = new Car("BMW", new Pilot("Rubens Barrichello")); 122 db.ext().bind(car2,IdCar1); 123 db.set(car2); 124 125 result = db.query(Car.class); 126 listResult(result); 127 } finally { 128 db.close(); 129 } 130 } 131 133 public static void listResult(ObjectSet result) { 134 System.out.println(result.size()); 135 while(result.hasNext()) { 136 System.out.println(result.next()); 137 } 138 } 139 } 141 | Popular Tags |