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.*;
024: import com.db4o.foundation.*;
025:
026: /**
027: * @exclude
028: */
029: public class HashcodeReferenceSystem implements ReferenceSystem {
030:
031: private ObjectReference _hashCodeTree;
032:
033: private ObjectReference _idTree;
034:
035: public void addNewReference(ObjectReference ref) {
036: addReference(ref);
037: }
038:
039: public void addExistingReference(ObjectReference ref) {
040: addReference(ref);
041: }
042:
043: private void addReference(ObjectReference ref) {
044: idAdd(ref);
045: hashCodeAdd(ref);
046: }
047:
048: public void addExistingReferenceToObjectTree(ObjectReference ref) {
049: hashCodeAdd(ref);
050: }
051:
052: public void addExistingReferenceToIdTree(ObjectReference ref) {
053: idAdd(ref);
054: }
055:
056: public void commit() {
057: // do nothing
058: }
059:
060: private void hashCodeAdd(ObjectReference ref) {
061: if (Deploy.debug) {
062: Object obj = ref.getObject();
063: if (obj != null) {
064: ObjectReference existing = referenceForObject(obj);
065: if (existing != null) {
066: System.out.println("Duplicate alarm hc_Tree");
067: }
068: }
069: }
070: if (_hashCodeTree == null) {
071: ref.hc_init();
072: _hashCodeTree = ref;
073: return;
074: }
075: _hashCodeTree = _hashCodeTree.hc_add(ref);
076: }
077:
078: private void idAdd(ObjectReference ref) {
079: if (DTrace.enabled) {
080: DTrace.ID_TREE_ADD.log(ref.getID());
081: }
082: if (Deploy.debug) {
083: ObjectReference existing = referenceForId(ref.getID());
084: if (existing != null) {
085: System.out.println("Duplicate alarm id_Tree:"
086: + ref.getID());
087: }
088: }
089: if (_idTree == null) {
090: ref.hc_init();
091: _idTree = ref;
092: return;
093: }
094: _idTree = _idTree.id_add(ref);
095: }
096:
097: public ObjectReference referenceForId(int id) {
098: if (DTrace.enabled) {
099: DTrace.GET_YAPOBJECT.log(id);
100: }
101: if (_idTree == null) {
102: return null;
103: }
104: if (!ObjectReference.isValidId(id)) {
105: return null;
106: }
107: return _idTree.id_find(id);
108: }
109:
110: public ObjectReference referenceForObject(Object obj) {
111: if (_hashCodeTree == null) {
112: return null;
113: }
114: return _hashCodeTree.hc_find(obj);
115: }
116:
117: public void removeReference(ObjectReference ref) {
118: if (DTrace.enabled) {
119: DTrace.REFERENCE_REMOVED.log(ref.getID());
120: }
121: if (_hashCodeTree != null) {
122: _hashCodeTree = _hashCodeTree.hc_remove(ref);
123: }
124: if (_idTree != null) {
125: _idTree = _idTree.id_remove(ref.getID());
126: }
127: }
128:
129: public void rollback() {
130: // do nothing
131: }
132:
133: public void traverseReferences(final Visitor4 visitor) {
134: if (_hashCodeTree == null) {
135: return;
136: }
137: _hashCodeTree.hc_traverse(visitor);
138: }
139:
140: }
|