1 2 3 package com.db4odoc.f1.ios; 4 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.io.RandomAccessFile ; 8 9 import com.db4o.Db4o; 10 import com.db4o.ObjectContainer; 11 import com.db4o.ObjectSet; 12 import com.db4o.io.MemoryIoAdapter; 13 import com.db4o.io.RandomAccessFileAdapter; 14 import com.db4odoc.f1.Util; 15 import com.db4odoc.f1.uuids.Pilot; 16 17 public class IOExample extends Util { 18 19 public static void main(String [] args) { 20 setObjects(); 21 getObjectsInMem(); 22 getObjects(); 23 testLoggingAdapter(); 24 } 25 27 public static void setObjects(){ 28 new File (Util.YAPFILENAME).delete(); 29 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 30 try { 31 Pilot pilot = new Pilot("Rubens Barrichello"); 32 db.set(pilot); 33 } finally { 34 db.close(); 35 } 36 } 37 39 public static void getObjectsInMem(){ 40 System.out.println("Setting up in-memory database"); 41 MemoryIoAdapter adapter = new MemoryIoAdapter(); 42 try { 43 RandomAccessFile raf = new RandomAccessFile (Util.YAPFILENAME,"r"); 44 adapter.growBy(100); 45 46 int len = (int)raf.length(); 47 byte[] b = new byte[len]; 48 raf.read(b,0,len); 49 adapter.put(Util.YAPFILENAME, b); 50 raf.close(); 51 } catch (Exception ex){ 52 System.out.println("Exception: " + ex.getMessage()); 53 } 54 55 Db4o.configure().io(adapter); 56 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 57 try { 58 ObjectSet result=db.get(Pilot.class); 59 System.out.println("Read stored results through memory file"); 60 listResult(result); 61 Pilot pilotNew = new Pilot("Michael Schumacher"); 62 db.set(pilotNew); 63 System.out.println("New pilot added"); 64 } finally { 65 db.close(); 66 } 67 System.out.println("Writing the database back to disc"); 68 byte[] dbstream = adapter.get(Util.YAPFILENAME); 69 try { 70 RandomAccessFile file = new RandomAccessFile (Util.YAPFILENAME,"rw"); 71 file.write(dbstream); 72 file.close(); 73 } catch (IOException ioex) { 74 System.out.println("Exception: " + ioex.getMessage()); 75 } 76 Db4o.configure().io(new RandomAccessFileAdapter()); 77 } 78 80 public static void getObjects(){ 81 Db4o.configure().io(new RandomAccessFileAdapter()); 82 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 83 try { 84 ObjectSet result=db.get(Pilot.class); 85 System.out.println("Read stored results through disc file"); 86 listResult(result); 87 } finally { 88 db.close(); 89 } 90 } 91 93 public static void testLoggingAdapter(){ 94 Db4o.configure().io(new LoggingAdapter()); 95 ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 96 try { 97 Pilot pilot = new Pilot("Michael Schumacher"); 98 db.set(pilot); 99 System.out.println("New pilot added"); 100 } finally { 101 db.close(); 102 } 103 104 db = Db4o.openFile(Util.YAPFILENAME); 105 try { 106 ObjectSet result=db.get(Pilot.class); 107 listResult(result); 108 } finally { 109 db.close(); 110 } 111 Db4o.configure().io(new RandomAccessFileAdapter()); 112 } 113 115 public static void listResult(ObjectSet result) { 116 System.out.println(result.size()); 117 while(result.hasNext()) { 118 System.out.println(result.next()); 119 } 120 } 121 } 123 | Popular Tags |