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 com.db4o.*;
024: import com.db4o.query.*;
025: import com.db4o.replication.*;
026: import com.db4o.test.*;
027: import com.db4o.tools.*;
028:
029: public class ReplicateExistingFile {
030:
031: public static class Task {
032:
033: public String _name;
034:
035: public Task(String name) {
036: _name = name;
037: }
038:
039: public String name() {
040: return _name;
041: }
042: }
043:
044: static final ReplicationConflictHandler _handler = new ReplicationConflictHandler() {
045: public Object resolveConflict(
046: ReplicationProcess replicationProcess, Object a,
047: Object b) {
048: return a;
049: }
050: };
051:
052: public void configure() {
053: Db4o.configure().objectClass(Task.class).enableReplication(
054: false);
055: }
056:
057: public void store() {
058: Test.store(new Task("old task 1"));
059: Test.store(new Task("old task 2"));
060: }
061:
062: public void test() {
063:
064: close();
065:
066: try {
067: Db4o.configure().objectClass(Task.class).enableReplication(
068: true);
069: new Defragment().run(currentFileName(), true);
070: } finally {
071: Db4o.configure().objectClass(Task.class).enableReplication(
072: false);
073: reOpen();
074: }
075:
076: ObjectContainer master = Test.objectContainer();
077: ObjectContainer slave = Test.replica();
078:
079: replicate(master, slave);
080:
081: Query q = slave.query();
082: q.constrain(Task.class);
083: ObjectSet os = q.execute();
084: Test.ensure(2 == os.size());
085:
086: }
087:
088: void replicate(ObjectContainer master, ObjectContainer slave) {
089: ReplicationProcess replication = master.ext().replicationBegin(
090: slave, _handler);
091:
092: ObjectSet replicationSet = objectsToReplicate(replication,
093: master);
094: while (replicationSet.hasNext()) {
095: replication.replicate(replicationSet.next());
096: }
097: replication.commit();
098: }
099:
100: ObjectSet objectsToReplicate(ReplicationProcess replication,
101: ObjectContainer master) {
102: // replicate all modified objects
103: Query q = master.query();
104: replication.whereModified(q);
105: return q.execute();
106: }
107:
108: private String currentFileName() {
109: return Test.isClientServer() ? Test.FILE_SERVER
110: : Test.FILE_SOLO;
111: }
112:
113: private void close() {
114: Test.close();
115: if (Test.isClientServer()) {
116: Test.server().close();
117: }
118: }
119:
120: private void reOpen() {
121: Test.reOpenServer();
122: }
123: }
|