001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.replication.old;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.ext.*;
027: import com.db4o.foundation.*;
028: import com.db4o.query.*;
029: import com.db4o.replication.*;
030: import com.db4o.test.*;
031:
032: public class R0to4Runner {
033:
034: private ExtObjectContainer _peerA;
035: private ExtObjectContainer _peerB;
036:
037: private static ReplicationConflictHandler _ignoreConflictHandler;
038:
039: private static final int LINKERS = 4;
040:
041: public void configure() {
042:
043: Db4o.configure().objectClass(R0.class).objectField("name")
044: .indexed(true);
045:
046: uUIDsOn(R0.class);
047: uUIDsOn(R1.class);
048: uUIDsOn(R2.class);
049: uUIDsOn(R3.class);
050: uUIDsOn(R4.class);
051:
052: _ignoreConflictHandler = new ReplicationConflictHandler() {
053: public Object resolveConflict(
054: ReplicationProcess replicationProcess, Object a,
055: Object b) {
056: return null;
057: }
058: };
059: }
060:
061: private void uUIDsOn(Class clazz) {
062: Db4o.configure().objectClass(clazz).generateUUIDs(true);
063: Db4o.configure().objectClass(clazz)
064: .generateVersionNumbers(true);
065: }
066:
067: public void store() {
068: _peerA = Test.objectContainer();
069:
070: R0Linker lCircles = new R0Linker();
071: lCircles.setNames("circles");
072: lCircles.linkCircles();
073: lCircles.store(_peerA);
074:
075: R0Linker lList = new R0Linker();
076: lList.setNames("list");
077: lList.linkList();
078: lList.store(_peerA);
079:
080: R0Linker lThis = new R0Linker();
081: lThis.setNames("this");
082: lThis.linkThis();
083: lThis.store(_peerA);
084:
085: R0Linker lBack = new R0Linker();
086: lBack.setNames("back");
087: lBack.linkBack();
088: lBack.store(_peerA);
089:
090: }
091:
092: public void test() {
093: _peerA = Test.objectContainer();
094: openReplica();
095:
096: ensureCount(_peerA, LINKERS);
097:
098: copyAllToB();
099: replicateNoneModified();
100:
101: modifyR4(_peerA);
102:
103: openReplica();
104: ensureR4Different();
105:
106: openReplica();
107: replicateR4();
108:
109: openReplica();
110: ensureR4Same();
111:
112: }
113:
114: private void openReplica() {
115: _peerB = Test.replica();
116: }
117:
118: private void ensureR4Different() {
119: compareR4(_peerB, _peerA, false);
120: }
121:
122: private void ensureR4Same() {
123: compareR4(_peerB, _peerA, true);
124: compareR4(_peerA, _peerB, true);
125: }
126:
127: private void compareR4(ObjectContainer ocA, ObjectContainer ocB,
128: boolean same) {
129: Query q = ocA.query();
130: q.constrain(R4.class);
131: ObjectSet objectSet = q.execute();
132: while (objectSet.hasNext()) {
133: R4 r4 = (R4) objectSet.next();
134: Query qb = ocB.query();
135: qb.constrain(R4.class);
136: qb.descend("name").constrain(r4.name);
137: int expectedSize = same ? 1 : 0;
138: int foundSize = qb.execute().size();
139: Test.ensure(foundSize == expectedSize);
140: if (foundSize != expectedSize) {
141: System.out.println(foundSize);
142: }
143: }
144: }
145:
146: private void modifyR4(ObjectContainer oc) {
147: Query q = oc.query();
148: q.constrain(R4.class);
149: ObjectSet objectSet = q.execute();
150: while (objectSet.hasNext()) {
151: R4 r4 = (R4) objectSet.next();
152: r4.name = r4.name + "_";
153: oc.set(r4);
154: }
155: oc.commit();
156: }
157:
158: private void copyAllToB() {
159: Test.ensure(replicateAll(false) == LINKERS * 5);
160: }
161:
162: private void replicateNoneModified() {
163: Test.ensure(replicateAll() == 0);
164: }
165:
166: private int replicateAll() {
167: return replicateAll(true);
168: }
169:
170: private void replicateR4() {
171: Test.ensure(replicateAll(true) == LINKERS);
172: }
173:
174: private int replicateAll(boolean modifiedOnly) {
175: Collection4 allR0 = new Collection4();
176: ReplicationProcess replication = _peerA.replicationBegin(
177: _peerB, _ignoreConflictHandler);
178: Query q = _peerA.query();
179: q.constrain(R0.class);
180: if (modifiedOnly) {
181: replication.whereModified(q);
182: }
183: ObjectSet objectSet = q.execute();
184: int replicated = 0;
185: while (objectSet.hasNext()) {
186: R0 r0 = (R0) objectSet.next();
187: allR0.add(r0);
188: replication.replicate(r0);
189: replicated++;
190: }
191: replication.commit();
192: ensureCount(_peerA, LINKERS);
193: ensureCount(_peerB, LINKERS);
194: Iterator4 i = allR0.iterator();
195: while (i.moveNext()) {
196: R0 r0 = (R0) i.current();
197: ObjectInfo infoA = _peerA.getObjectInfo(r0);
198: ObjectInfo infoB = _peerB.getObjectInfo(r0);
199: Db4oUUID uuidA = infoA.getUUID();
200: Db4oUUID uuidB = infoB.getUUID();
201: Test.ensure(uuidA.getLongPart() == uuidB.getLongPart());
202: byte[] sigA = uuidA.getSignaturePart();
203: byte[] sigB = uuidB.getSignaturePart();
204: Test.ensure(Arrays.equals(sigA, sigB));
205: }
206:
207: // reopen replication file
208: _peerB = Test.replica();
209:
210: i = allR0.iterator();
211: while (i.moveNext()) {
212: R0 r0 = (R0) i.current();
213: ObjectInfo infoA = _peerA.getObjectInfo(r0);
214: ObjectInfo infoB = _peerB.getObjectInfo(r0);
215: Db4oUUID uuidA = infoA.getUUID();
216: R0 r0B = (R0) _peerB.getByUUID(uuidA);
217: Test.ensure(r0B != null);
218: _peerB.activate(r0B, 1);
219: Test.ensure(r0B.name.equals(r0.name));
220: }
221:
222: return replicated;
223: }
224:
225: private void ensureCount(ObjectContainer oc, int linkers) {
226: ensureCount(oc, R0.class, linkers * 5);
227: ensureCount(oc, R1.class, linkers * 4);
228: ensureCount(oc, R2.class, linkers * 3);
229: ensureCount(oc, R3.class, linkers * 2);
230: ensureCount(oc, R4.class, linkers * 1);
231: }
232:
233: private void ensureCount(ObjectContainer oc, Class clazz, int count) {
234: Query q = oc.query();
235: q.constrain(clazz);
236: Test.ensure(q.execute().size() == count);
237: }
238:
239: }
|