KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc _name;
34
35         public Task(String JavaDoc name) {
36             _name = name;
37         }
38         
39         public String JavaDoc name() {
40             return _name;
41         }
42     }
43     
44     static final ReplicationConflictHandler _handler = new ReplicationConflictHandler() {
45         public Object JavaDoc resolveConflict(ReplicationProcess replicationProcess, Object JavaDoc a, Object JavaDoc 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         // replicate all modified objects
96
Query q = master.query();
97         replication.whereModified(q);
98         return q.execute();
99     }
100     
101     private String JavaDoc 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