KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > replication > old > ReplicationFeatures


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.replication.old;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.ext.*;
27 import com.db4o.query.*;
28 import com.db4o.replication.*;
29 import com.db4o.test.*;
30
31 public class ReplicationFeatures {
32
33     public String JavaDoc name;
34
35     public void configure() {
36         Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
37         Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
38     }
39
40     public void storeOne() {
41     }
42
43     public void testUuidGeneration() {
44         ExtObjectContainer master = Test.objectContainer();
45
46         //Test.ensure(false);
47
//Db4oUUID uuid = master.getObjectInfo(masterObject).getUUID();
48
//Test.ensure(masterObject == master.getByUUID(uuid));
49
}
50
51     public void testOne() {
52         name = "rf1";
53         Test.store(this);
54         replicateAll();
55         checkOne("rf1");
56
57         name = "rf2";
58         Test.store(this);
59         Test.commit();
60         replicateAll();
61
62         checkOne("rf2");
63         replicateAll();
64         checkAllEqual();
65         ensureDb4oDatabaseUnicity();
66     }
67
68     public void testContainerIdentity() {
69         if (Test.isClientServer()) {
70             ExtObjectContainer clientObjectContainer = Test.objectContainer();
71             ExtObjectContainer serverObjectContainer =
72
73             Test.server().ext().objectContainer().ext();
74
75             Test.ensure(clientObjectContainer.identity().equals(serverObjectContainer.identity()));
76         }
77     }
78     
79     private void printVersion() {
80         // System.out.println("Version: " + Test.objectContainer().getObjectInfo(this).getVersion());
81
}
82
83     private void replicateAll() {
84         ExtObjectContainer peerA = Test.objectContainer();
85         ExtObjectContainer peerB = Test.replica();
86         final ReplicationProcess replication =
87
88         peerA.ext().replicationBegin(peerB, new ReplicationConflictHandler() {
89
90             public Object JavaDoc resolveConflict(ReplicationProcess process, Object JavaDoc a, Object JavaDoc b) {
91
92                 // the object was change in both ObjectContainers since
93
// the last replication
94

95                 return null;
96
97             }
98
99         });
100
101         // replication.setDirection(master, slave); //Default is bidirectional.
102

103         Query q = peerA.query();
104         q.constrain(ReplicationFeatures.class);
105         replication.whereModified(q);
106
107         ObjectSet objectSet = q.execute();
108         while (objectSet.hasNext()) {
109             Object JavaDoc masterObject = objectSet.next();
110             
111             ((ReplicationFeatures)masterObject).printVersion();
112
113             // check version numbers and decide upon direction,
114
// depending which one changed after last synchronisation
115
replication.replicate(masterObject);
116             // replication.checkConflict(masterObject); // Another option (peek).
117
}
118         replication.commit();
119         peerB.close();
120     }
121
122     private void checkAllEqual() {
123         DeepCompare comparator = new DeepCompare();
124         ExtObjectContainer master = Test.objectContainer();
125         ExtObjectContainer slave = Test.replica();
126         Query q = master.query();
127         q.constrain(ReplicationFeatures.class);
128         ObjectSet objectSet = q.execute();
129         while (objectSet.hasNext()) {
130             Object JavaDoc masterObject = objectSet.next();
131
132             Db4oUUID uuid = master.getObjectInfo(masterObject).getUUID();
133             Object JavaDoc slaveObject = slave.getByUUID(uuid);
134
135             master.activate(masterObject, Integer.MAX_VALUE);
136             slave.activate(slaveObject, Integer.MAX_VALUE);
137             Test.ensure(comparator.isEqual(masterObject, slaveObject));
138         }
139         slave.close();
140     }
141
142     private void checkOne(String JavaDoc name) {
143         ObjectContainer slave = Test.replica();
144         Query q = slave.query();
145         q.constrain(this.getClass());
146         ObjectSet objectSet = q.execute();
147         Test.ensure(objectSet.size() == 1);
148         ReplicationFeatures rf = (ReplicationFeatures) objectSet.next();
149         Test.ensure(rf.name.equals(name));
150         slave.close();
151     }
152
153     private void ensureDb4oDatabaseUnicity() {
154         Hashtable ht = new Hashtable();
155         YapStream yapStream = ((YapStream) Test.objectContainer());
156         yapStream.showInternalClasses(true);
157         Query q = Test.query();
158         q.constrain(Db4oDatabase.class);
159         ObjectSet objectSet = q.execute();
160         while (objectSet.hasNext()) {
161             Db4oDatabase d4b = (Db4oDatabase) objectSet.next();
162             Test.ensure(!ht.containsKey(d4b.i_signature));
163             ht.put(d4b.i_signature, "");
164         }
165         yapStream.showInternalClasses(false);
166     }
167
168     private void replicationSnippet() {
169
170         // open any two ObjectContainers, local or Client/Server
171
ExtObjectContainer peerA = Test.objectContainer();
172         ExtObjectContainer peerB = Test.replica();
173
174         // create a replication process with a ConflictHandler
175
final ReplicationProcess replication = peerA.ext().replicationBegin(peerB,
176             new ReplicationConflictHandler() {
177
178                 public Object JavaDoc resolveConflict(ReplicationProcess process, Object JavaDoc a,
179
180                 Object JavaDoc b) {
181                     // the object was changed in both ObjectContainers since the
182
// last time the two ObjectContainers were replicated.
183

184                     // return a or b to indicate which version you want to keep or
185
// null to replicate nothing
186
return null;
187                 }
188             });
189
190         // You could set this replication process to one-directional,
191
// It is bidirectional by default.
192
// replication.setDirection(peerA, peerB);
193

194         Query query = peerA.query();
195
196         // You can do any query that you like here
197
query.constrain(ReplicationFeatures.class);
198
199         // This method adds a special constraint to query only for
200
// objects modified since the last replication between the
201
// two ObjectContainers.
202
replication.whereModified(query);
203
204         ObjectSet objectSet = query.execute();
205         while (objectSet.hasNext()) {
206             // checks version numbers and decides upon direction,
207
// depending which one changed after last synchronisation
208
replication.replicate(objectSet.next());
209         }
210         replication.commit();
211         peerB.close();
212     }
213
214 }
Popular Tags