KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > ReplicationRecord


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;
22
23 import com.db4o.ext.*;
24 import com.db4o.query.*;
25
26 /**
27  * tracks the version of the last replication between
28  * two Objectcontainers.
29  *
30  * @exclude
31  * @persistent
32  */

33 public class ReplicationRecord implements Internal4{
34    
35     public Db4oDatabase _youngerPeer;
36     public Db4oDatabase _olderPeer;
37     public long _version;
38     
39     public ReplicationRecord(){
40     }
41     
42     public ReplicationRecord(Db4oDatabase younger, Db4oDatabase older){
43         _youngerPeer = younger;
44         _olderPeer = older;
45     }
46     
47     public void setVersion(long version){
48         _version = version;
49     }
50     
51     public void store(YapStream stream){
52         stream.showInternalClasses(true);
53         Transaction ta = stream.checkTransaction(null);
54         stream.setAfterReplication(ta, this, 1, false);
55         stream.commit();
56         stream.showInternalClasses(false);
57     }
58     
59     public static ReplicationRecord beginReplication(Transaction transA, Transaction transB){
60         
61         YapStream peerA = transA.stream();
62         YapStream peerB = transB.stream();
63         
64         Db4oDatabase dbA = peerA.identity();
65         Db4oDatabase dbB = peerB.identity();
66         
67         dbB.bind(transA);
68         dbA.bind(transB);
69         
70         Db4oDatabase younger = null;
71         Db4oDatabase older = null;
72         
73         if(dbA.isOlderThan(dbB)){
74             younger = dbB;
75             older = dbA;
76         }else{
77             younger = dbA;
78             older = dbB;
79         }
80         
81         ReplicationRecord rrA = queryForReplicationRecord(peerA, younger, older);
82         ReplicationRecord rrB = queryForReplicationRecord(peerB, younger, older);
83         if(rrA == null){
84             if(rrB == null){
85                 return new ReplicationRecord(younger, older);
86             }
87             rrB.store(peerA);
88             return rrB;
89         }
90         
91         if(rrB == null){
92             rrA.store(peerB);
93             return rrA;
94         }
95         
96         if(rrA != rrB){
97             peerB.showInternalClasses(true);
98             int id = peerB.getID1(rrB);
99             peerB.bind1(transB, rrA, id);
100             peerB.showInternalClasses(false);
101         }
102         
103         return rrA;
104     }
105     
106     public static ReplicationRecord queryForReplicationRecord(YapStream stream, Db4oDatabase younger, Db4oDatabase older) {
107         ReplicationRecord res = null;
108         stream.showInternalClasses(true);
109         Query q = stream.query();
110         q.constrain(YapConst.CLASS_REPLICATIONRECORD);
111         q.descend("_youngerPeer").constrain(younger).identity();
112         q.descend("_olderPeer").constrain(older).identity();
113         ObjectSet objectSet = q.execute();
114         if(objectSet.hasNext()){
115             res = (ReplicationRecord)objectSet.next();
116         }
117         stream.showInternalClasses(false);
118         return res;
119     }
120 }
121
122
Popular Tags