KickJava   Java API By Example, From Geeks To Geeks.

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


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.foundation.*;
28 import com.db4o.query.*;
29 import com.db4o.replication.*;
30 import com.db4o.test.*;
31
32 public class R0to4Runner {
33
34     private ExtObjectContainer _peerA;
35     private ExtObjectContainer _peerB;
36
37     private static ReplicationConflictHandler _ignoreConflictHandler;
38
39     private static final int LINKERS = 4;
40
41     public void configure() {
42
43         Db4o.configure().objectClass(R0.class).objectField("name").indexed(true);
44
45         uUIDsOn(R0.class);
46         uUIDsOn(R1.class);
47         uUIDsOn(R2.class);
48         uUIDsOn(R3.class);
49         uUIDsOn(R4.class);
50
51         _ignoreConflictHandler = new ReplicationConflictHandler() {
52             public Object JavaDoc resolveConflict(ReplicationProcess replicationProcess, Object JavaDoc a, Object JavaDoc b) {
53                 return null;
54             }
55         };
56     }
57
58     private void uUIDsOn(Class JavaDoc clazz) {
59         Db4o.configure().objectClass(clazz).generateUUIDs(true);
60         Db4o.configure().objectClass(clazz).generateVersionNumbers(true);
61     }
62
63     public void store() {
64         _peerA = Test.objectContainer();
65
66         R0Linker lCircles = new R0Linker();
67         lCircles.setNames("circles");
68         lCircles.linkCircles();
69         lCircles.store(_peerA);
70
71         R0Linker lList = new R0Linker();
72         lList.setNames("list");
73         lList.linkList();
74         lList.store(_peerA);
75
76         R0Linker lThis = new R0Linker();
77         lThis.setNames("this");
78         lThis.linkThis();
79         lThis.store(_peerA);
80
81         R0Linker lBack = new R0Linker();
82         lBack.setNames("back");
83         lBack.linkBack();
84         lBack.store(_peerA);
85
86     }
87
88     public void test() {
89         _peerA = Test.objectContainer();
90         openReplica();
91
92         ensureCount(_peerA, LINKERS);
93
94         copyAllToB();
95         replicateNoneModified();
96
97         modifyR4(_peerA);
98
99         openReplica();
100         ensureR4Different();
101
102         openReplica();
103         replicateR4();
104
105         openReplica();
106         ensureR4Same();
107
108     }
109
110     private void openReplica() {
111         _peerB = Test.replica();
112     }
113
114     private void ensureR4Different() {
115         compareR4(_peerB, _peerA, false);
116     }
117
118     private void ensureR4Same() {
119         compareR4(_peerB, _peerA, true);
120         compareR4(_peerA, _peerB, true);
121     }
122
123     private void compareR4(ObjectContainer ocA, ObjectContainer ocB, boolean same) {
124         Query q = ocA.query();
125         q.constrain(R4.class);
126         ObjectSet objectSet = q.execute();
127         while (objectSet.hasNext()) {
128             R4 r4 = (R4) objectSet.next();
129             Query qb = ocB.query();
130             qb.constrain(R4.class);
131             qb.descend("name").constrain(r4.name);
132             int expectedSize = same ? 1 : 0;
133             int foundSize = qb.execute().size();
134             Test.ensure(foundSize == expectedSize);
135             if (foundSize != expectedSize) {
136                 System.out.println(foundSize);
137             }
138         }
139     }
140
141     private void modifyR4(ObjectContainer oc) {
142         Query q = oc.query();
143         q.constrain(R4.class);
144         ObjectSet objectSet = q.execute();
145         while (objectSet.hasNext()) {
146             R4 r4 = (R4) objectSet.next();
147             r4.name = r4.name + "_";
148             oc.set(r4);
149         }
150         oc.commit();
151     }
152
153     private void copyAllToB() {
154         Test.ensure(replicateAll(false) == LINKERS * 5);
155     }
156
157     private void replicateNoneModified() {
158         Test.ensure(replicateAll() == 0);
159     }
160
161     private int replicateAll() {
162         return replicateAll(true);
163     }
164
165     private void replicateR4() {
166         Test.ensure(replicateAll(true) == LINKERS);
167     }
168
169     private int replicateAll(boolean modifiedOnly) {
170         Collection4 allR0 = new Collection4();
171         ReplicationProcess replication = _peerA.replicationBegin(_peerB, _ignoreConflictHandler);
172         Query q = _peerA.query();
173         q.constrain(R0.class);
174         if (modifiedOnly) {
175             replication.whereModified(q);
176         }
177         ObjectSet objectSet = q.execute();
178         int replicated = 0;
179         while (objectSet.hasNext()) {
180             R0 r0 = (R0)objectSet.next();
181             allR0.add(r0);
182             replication.replicate(r0);
183             replicated++;
184         }
185         replication.commit();
186         ensureCount(_peerA, LINKERS);
187         ensureCount(_peerB, LINKERS);
188         Iterator4 i = allR0.iterator();
189         while(i.moveNext()){
190             R0 r0 = (R0)i.current();
191             ObjectInfo infoA = _peerA.getObjectInfo(r0);
192             ObjectInfo infoB = _peerB.getObjectInfo(r0);
193             Db4oUUID uuidA = infoA.getUUID();
194             Db4oUUID uuidB = infoB.getUUID();
195             Test.ensure(uuidA.getLongPart() == uuidB.getLongPart());
196             byte[] sigA = uuidA.getSignaturePart();
197             byte[] sigB = uuidB.getSignaturePart();
198             Test.ensure(Arrays.equals(sigA, sigB));
199         }
200         
201         // reopen replication file
202
_peerB = Test.replica();
203         
204         i = allR0.iterator();
205         while(i.moveNext()){
206             R0 r0 = (R0)i.current();
207             ObjectInfo infoA = _peerA.getObjectInfo(r0);
208             ObjectInfo infoB = _peerB.getObjectInfo(r0);
209             Db4oUUID uuidA = infoA.getUUID();
210             R0 r0B = (R0) _peerB.getByUUID(uuidA);
211             Test.ensure(r0B != null);
212             _peerB.activate(r0B, 1);
213             Test.ensure(r0B.name.equals(r0.name));
214         }
215         
216         return replicated;
217     }
218
219     private void ensureCount(ObjectContainer oc, int linkers) {
220         ensureCount(oc, R0.class, linkers * 5);
221         ensureCount(oc, R1.class, linkers * 4);
222         ensureCount(oc, R2.class, linkers * 3);
223         ensureCount(oc, R3.class, linkers * 2);
224         ensureCount(oc, R4.class, linkers * 1);
225     }
226
227     private void ensureCount(ObjectContainer oc, Class JavaDoc clazz, int count) {
228         Query q = oc.query();
229         q.constrain(clazz);
230         Test.ensure(q.execute().size() == count);
231     }
232
233 }
234
Popular Tags