1 21 package com.db4o.test.replication.old; 22 23 import com.db4o.*; 24 import com.db4o.query.*; 25 import com.db4o.replication.*; 26 import com.db4o.test.*; 27 import com.db4o.tools.*; 28 29 public class ReplicateExistingFile { 30 31 public static class Task { 32 33 public String _name; 34 35 public Task(String name) { 36 _name = name; 37 } 38 39 public String name() { 40 return _name; 41 } 42 } 43 44 static final ReplicationConflictHandler _handler = new ReplicationConflictHandler() { 45 public Object resolveConflict(ReplicationProcess replicationProcess, Object a, Object b) { 46 return a; 47 } 48 }; 49 50 public void configure() { 51 Db4o.configure().objectClass(Task.class).enableReplication(false); 52 } 53 54 public void store() { 55 Test.store(new Task("old task 1")); 56 Test.store(new Task("old task 2")); 57 } 58 59 public void test() { 60 61 close(); 62 63 try { 64 Db4o.configure().objectClass(Task.class).enableReplication(true); 65 new Defragment().run(currentFileName(), true); 66 } finally { 67 Db4o.configure().objectClass(Task.class).enableReplication(false); 68 reOpen(); 69 } 70 71 ObjectContainer master = Test.objectContainer(); 72 ObjectContainer slave = Test.replica(); 73 74 replicate(master, slave); 75 76 Query q = slave.query(); 77 q.constrain(Task.class); 78 ObjectSet os = q.execute(); 79 Test.ensure(2 == os.size()); 80 81 82 } 83 84 void replicate(ObjectContainer master, ObjectContainer slave) { 85 ReplicationProcess replication = master.ext().replicationBegin(slave, _handler); 86 87 ObjectSet replicationSet = objectsToReplicate(replication, master); 88 while (replicationSet.hasNext()) { 89 replication.replicate(replicationSet.next()); 90 } 91 replication.commit(); 92 } 93 94 ObjectSet objectsToReplicate(ReplicationProcess replication, ObjectContainer master) { 95 Query q = master.query(); 97 replication.whereModified(q); 98 return q.execute(); 99 } 100 101 private String currentFileName() { 102 return Test.isClientServer() 103 ? Test.FILE_SERVER 104 : Test.FILE_SOLO; 105 } 106 107 private void close() { 108 Test.close(); 109 if (Test.isClientServer()) { 110 Test.server().close(); 111 } 112 } 113 114 private void reOpen() { 115 Test.reOpenServer(); 116 } 117 } | Popular Tags |