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.internal.*;
028: import com.db4o.query.*;
029: import com.db4o.replication.*;
030: import com.db4o.test.*;
031:
032: public class ReplicationFeatures {
033:
034: public String name;
035:
036: public void configure() {
037: Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
038: Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
039: }
040:
041: public void storeOne() {
042: }
043:
044: public void testUuidGeneration() {
045: ExtObjectContainer master = Test.objectContainer();
046:
047: //Test.ensure(false);
048: //Db4oUUID uuid = master.getObjectInfo(masterObject).getUUID();
049: //Test.ensure(masterObject == master.getByUUID(uuid));
050: }
051:
052: public void testOne() {
053: name = "rf1";
054: Test.store(this );
055: replicateAll();
056: checkOne("rf1");
057:
058: name = "rf2";
059: Test.store(this );
060: Test.commit();
061: replicateAll();
062:
063: checkOne("rf2");
064: replicateAll();
065: checkAllEqual();
066: ensureDb4oDatabaseUnicity();
067: }
068:
069: public void testContainerIdentity() {
070: if (Test.isClientServer()) {
071: ExtObjectContainer clientObjectContainer = Test
072: .objectContainer();
073: ExtObjectContainer serverObjectContainer =
074:
075: Test.server().ext().objectContainer().ext();
076:
077: Test.ensure(clientObjectContainer.identity().equals(
078: serverObjectContainer.identity()));
079: }
080: }
081:
082: private void printVersion() {
083: // System.out.println("Version: " + Test.objectContainer().getObjectInfo(this).getVersion());
084: }
085:
086: private void replicateAll() {
087: ExtObjectContainer peerA = Test.objectContainer();
088: ExtObjectContainer peerB = Test.replica();
089: final ReplicationProcess replication =
090:
091: peerA.ext().replicationBegin(peerB,
092: new ReplicationConflictHandler() {
093:
094: public Object resolveConflict(
095: ReplicationProcess process, Object a,
096: Object b) {
097:
098: // the object was change in both ObjectContainers since
099: // the last replication
100:
101: return null;
102:
103: }
104:
105: });
106:
107: // replication.setDirection(master, slave); //Default is bidirectional.
108:
109: Query q = peerA.query();
110: q.constrain(ReplicationFeatures.class);
111: replication.whereModified(q);
112:
113: ObjectSet objectSet = q.execute();
114: while (objectSet.hasNext()) {
115: Object masterObject = objectSet.next();
116:
117: ((ReplicationFeatures) masterObject).printVersion();
118:
119: // check version numbers and decide upon direction,
120: // depending which one changed after last synchronisation
121: replication.replicate(masterObject);
122: // replication.checkConflict(masterObject); // Another option (peek).
123: }
124: replication.commit();
125: peerB.close();
126: }
127:
128: private void checkAllEqual() {
129: DeepCompare comparator = new DeepCompare();
130: ExtObjectContainer master = Test.objectContainer();
131: ExtObjectContainer slave = Test.replica();
132: Query q = master.query();
133: q.constrain(ReplicationFeatures.class);
134: ObjectSet objectSet = q.execute();
135: while (objectSet.hasNext()) {
136: Object masterObject = objectSet.next();
137:
138: Db4oUUID uuid = master.getObjectInfo(masterObject)
139: .getUUID();
140: Object slaveObject = slave.getByUUID(uuid);
141:
142: master.activate(masterObject, Integer.MAX_VALUE);
143: slave.activate(slaveObject, Integer.MAX_VALUE);
144: Test.ensure(comparator.isEqual(masterObject, slaveObject));
145: }
146: slave.close();
147: }
148:
149: private void checkOne(String name) {
150: ObjectContainer slave = Test.replica();
151: Query q = slave.query();
152: q.constrain(this .getClass());
153: ObjectSet objectSet = q.execute();
154: Test.ensure(objectSet.size() == 1);
155: ReplicationFeatures rf = (ReplicationFeatures) objectSet.next();
156: Test.ensure(rf.name.equals(name));
157: slave.close();
158: }
159:
160: private void ensureDb4oDatabaseUnicity() {
161: Hashtable ht = new Hashtable();
162: ObjectContainerBase yapStream = ((ObjectContainerBase) Test
163: .objectContainer());
164: yapStream.showInternalClasses(true);
165: Query q = Test.query();
166: q.constrain(Db4oDatabase.class);
167: ObjectSet objectSet = q.execute();
168: while (objectSet.hasNext()) {
169: Db4oDatabase d4b = (Db4oDatabase) objectSet.next();
170: Test.ensure(!ht.containsKey(d4b.i_signature));
171: ht.put(d4b.i_signature, "");
172: }
173: yapStream.showInternalClasses(false);
174: }
175:
176: private void replicationSnippet() {
177:
178: // open any two ObjectContainers, local or Client/Server
179: ExtObjectContainer peerA = Test.objectContainer();
180: ExtObjectContainer peerB = Test.replica();
181:
182: // create a replication process with a ConflictHandler
183: final ReplicationProcess replication = peerA.ext()
184: .replicationBegin(peerB,
185: new ReplicationConflictHandler() {
186:
187: public Object resolveConflict(
188: ReplicationProcess process,
189: Object a,
190:
191: Object b) {
192: // the object was changed in both ObjectContainers since the
193: // last time the two ObjectContainers were replicated.
194:
195: // return a or b to indicate which version you want to keep or
196: // null to replicate nothing
197: return null;
198: }
199: });
200:
201: // You could set this replication process to one-directional,
202: // It is bidirectional by default.
203: // replication.setDirection(peerA, peerB);
204:
205: Query query = peerA.query();
206:
207: // You can do any query that you like here
208: query.constrain(ReplicationFeatures.class);
209:
210: // This method adds a special constraint to query only for
211: // objects modified since the last replication between the
212: // two ObjectContainers.
213: replication.whereModified(query);
214:
215: ObjectSet objectSet = query.execute();
216: while (objectSet.hasNext()) {
217: // checks version numbers and decides upon direction,
218: // depending which one changed after last synchronisation
219: replication.replicate(objectSet.next());
220: }
221: replication.commit();
222: peerB.close();
223: }
224:
225: }
|