KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > replication > ReplicationExample


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.replication;
4
5 import java.io.IOException JavaDoc;
6
7 import com.db4o.Db4o;
8 import com.db4o.ObjectContainer;
9 import com.db4o.ObjectSet;
10 import com.db4o.drs.Replication;
11 import com.db4o.drs.ReplicationSession;
12
13
14 public class ReplicationExample {
15     public final static String JavaDoc DTFILENAME="formula1.yap";
16     public final static String JavaDoc HHFILENAME="handheld.yap";
17     
18     public static void configureReplication(){
19         Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
20         Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
21     }
22     // end configureReplication
23

24     public static void configureReplicationPilot(){
25         Db4o.configure().objectClass(Pilot.class).generateUUIDs(true);
26         Db4o.configure().objectClass(Pilot.class).generateVersionNumbers(true);
27     }
28     // end configureReplicationPilot
29

30     public static void configureForExisting(){
31         Db4o.configure().objectClass(Pilot.class).enableReplication(true);
32         try {
33             com.db4o.defragment.Defragment.defrag("sample.yap");
34         } catch (IOException JavaDoc ex){
35             System.out .println(ex.toString());
36         }
37     }
38     // end configureForExisting
39

40     public static void replicate(){
41         ObjectContainer desktop=Db4o.openFile(DTFILENAME);
42         ObjectContainer handheld=Db4o.openFile(HHFILENAME);
43         // Setup a replication session
44
ReplicationSession replication = Replication.begin(handheld, desktop);
45         
46         /*
47          * There is no need to replicate all the objects each time.
48          * objectsChangedSinceLastReplication methods gives us
49          * a list of modified objects
50          */

51         ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication();
52         
53         // Iterate changed objects, check if the name starts with "S" and replicate only those items
54
while (changed.hasNext()) {
55             replication.replicate(changed.next());
56         }
57         
58         replication.commit();
59     }
60     // end replicate
61
}
62
Popular Tags