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.internal;
022:
023: import com.db4o.foundation.*;
024:
025: /**
026: * @exclude
027: */
028: public class TransactionalReferenceSystem implements ReferenceSystem {
029:
030: final ReferenceSystem _committedReferences = new HashcodeReferenceSystem();
031:
032: private ReferenceSystem _newReferences;
033:
034: public TransactionalReferenceSystem() {
035: createNewReferences();
036: }
037:
038: public void addExistingReference(ObjectReference ref) {
039: _committedReferences.addExistingReference(ref);
040: }
041:
042: public void addExistingReferenceToIdTree(ObjectReference ref) {
043: _committedReferences.addExistingReferenceToIdTree(ref);
044: }
045:
046: public void addExistingReferenceToObjectTree(ObjectReference ref) {
047: _committedReferences.addExistingReferenceToObjectTree(ref);
048: }
049:
050: public void addNewReference(ObjectReference ref) {
051: _newReferences.addNewReference(ref);
052: }
053:
054: public void commit() {
055: traveseNewReferences(new Visitor4() {
056: public void visit(Object obj) {
057: ObjectReference oref = (ObjectReference) obj;
058: Object referent = oref.getObject();
059: if (referent != null) {
060: _committedReferences.addExistingReference(oref);
061: }
062: }
063: });
064: createNewReferences();
065: }
066:
067: public void traveseNewReferences(final Visitor4 visitor) {
068: _newReferences.traverseReferences(visitor);
069: }
070:
071: private void createNewReferences() {
072: _newReferences = new HashcodeReferenceSystem();
073: }
074:
075: public ObjectReference referenceForId(int id) {
076: ObjectReference ref = _newReferences.referenceForId(id);
077: if (ref != null) {
078: return ref;
079: }
080: return _committedReferences.referenceForId(id);
081: }
082:
083: public ObjectReference referenceForObject(Object obj) {
084: ObjectReference ref = _newReferences.referenceForObject(obj);
085: if (ref != null) {
086: return ref;
087: }
088: return _committedReferences.referenceForObject(obj);
089: }
090:
091: public void removeReference(ObjectReference ref) {
092: _newReferences.removeReference(ref);
093: _committedReferences.removeReference(ref);
094: }
095:
096: public void rollback() {
097: createNewReferences();
098: }
099:
100: public void traverseReferences(Visitor4 visitor) {
101: traveseNewReferences(visitor);
102: _committedReferences.traverseReferences(visitor);
103: }
104:
105: }
|