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;
022:
023: import com.db4o.ext.*;
024: import com.db4o.internal.*;
025: import com.db4o.query.*;
026:
027: /**
028: * tracks the version of the last replication between
029: * two Objectcontainers.
030: *
031: * @exclude
032: * @persistent
033: */
034: public class ReplicationRecord implements Internal4 {
035:
036: public Db4oDatabase _youngerPeer;
037: public Db4oDatabase _olderPeer;
038: public long _version;
039:
040: public ReplicationRecord() {
041: }
042:
043: public ReplicationRecord(Db4oDatabase younger, Db4oDatabase older) {
044: _youngerPeer = younger;
045: _olderPeer = older;
046: }
047:
048: public void setVersion(long version) {
049: _version = version;
050: }
051:
052: public void store(ObjectContainerBase container) {
053: container.showInternalClasses(true);
054: try {
055: Transaction trans = container.checkTransaction();
056: container.setAfterReplication(trans, this , 1, false);
057: container.commit(trans);
058: } finally {
059: container.showInternalClasses(false);
060: }
061: }
062:
063: public static ReplicationRecord beginReplication(
064: Transaction transA, Transaction transB) {
065:
066: ObjectContainerBase peerA = transA.container();
067: ObjectContainerBase peerB = transB.container();
068:
069: Db4oDatabase dbA = ((InternalObjectContainer) peerA).identity();
070: Db4oDatabase dbB = ((InternalObjectContainer) peerB).identity();
071:
072: dbB.bind(transA);
073: dbA.bind(transB);
074:
075: Db4oDatabase younger = null;
076: Db4oDatabase older = null;
077:
078: if (dbA.isOlderThan(dbB)) {
079: younger = dbB;
080: older = dbA;
081: } else {
082: younger = dbA;
083: older = dbB;
084: }
085:
086: ReplicationRecord rrA = queryForReplicationRecord(peerA,
087: transA, younger, older);
088: ReplicationRecord rrB = queryForReplicationRecord(peerB,
089: transB, younger, older);
090: if (rrA == null) {
091: if (rrB == null) {
092: return new ReplicationRecord(younger, older);
093: }
094: rrB.store(peerA);
095: return rrB;
096: }
097:
098: if (rrB == null) {
099: rrA.store(peerB);
100: return rrA;
101: }
102:
103: if (rrA != rrB) {
104: peerB.showInternalClasses(true);
105: try {
106: int id = peerB.getID(transB, rrB);
107: peerB.bind(transB, rrA, id);
108: } finally {
109: peerB.showInternalClasses(false);
110: }
111: }
112:
113: return rrA;
114: }
115:
116: public static ReplicationRecord queryForReplicationRecord(
117: ObjectContainerBase container, Transaction trans,
118: Db4oDatabase younger, Db4oDatabase older) {
119: container.showInternalClasses(true);
120: try {
121: Query q = container.query(trans);
122: q.constrain(Const4.CLASS_REPLICATIONRECORD);
123: q.descend("_youngerPeer").constrain(younger).identity();
124: q.descend("_olderPeer").constrain(older).identity();
125: ObjectSet objectSet = q.execute();
126: return objectSet.hasNext() ? (ReplicationRecord) objectSet
127: .next() : null;
128: } finally {
129: container.showInternalClasses(false);
130: }
131: }
132: }
|