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.ix;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.freespace.*;
026:
027: /**
028: * @exclude
029: */
030: public abstract class IxTree extends Tree implements Visitor4 {
031:
032: IndexTransaction _fieldTransaction;
033:
034: int _version;
035:
036: int _nodes = 1;
037:
038: IxTree(IndexTransaction a_ft) {
039: _fieldTransaction = a_ft;
040: _version = a_ft.i_version;
041: }
042:
043: public Tree add(final Tree a_new, final int a_cmp) {
044: if (a_cmp < 0) {
045: if (_subsequent == null) {
046: _subsequent = a_new;
047: } else {
048: _subsequent = _subsequent.add(a_new);
049: }
050: } else {
051: if (_preceding == null) {
052: _preceding = a_new;
053: } else {
054: _preceding = _preceding.add(a_new);
055: }
056: }
057: return balanceCheckNulls();
058: }
059:
060: void beginMerge() {
061: _preceding = null;
062: _subsequent = null;
063: setSizeOwn();
064: }
065:
066: public Object deepClone(Object a_param) {
067: IxTree tree = (IxTree) this .shallowClone();
068: tree._fieldTransaction = (IndexTransaction) a_param;
069: tree._nodes = _nodes;
070: return tree;
071: }
072:
073: final Indexable4 handler() {
074: return _fieldTransaction.i_index._handler;
075: }
076:
077: final Index4 index() {
078: return _fieldTransaction.i_index;
079: }
080:
081: /**
082: * Overridden in IxFileRange
083: * Only call directly after compare()
084: */
085: int[] lowerAndUpperMatch() {
086: return null;
087: }
088:
089: public final int nodes() {
090: return _nodes;
091: }
092:
093: public void setSizeOwn() {
094: super .setSizeOwn();
095: _nodes = 1;
096: }
097:
098: public void setSizeOwnPrecedingSubsequent() {
099: super .setSizeOwnPrecedingSubsequent();
100: _nodes = 1 + _preceding.nodes() + _subsequent.nodes();
101: }
102:
103: public void setSizeOwnPreceding() {
104: super .setSizeOwnPreceding();
105: _nodes = 1 + _preceding.nodes();
106: }
107:
108: public void setSizeOwnSubsequent() {
109: super .setSizeOwnSubsequent();
110: _nodes = 1 + _subsequent.nodes();
111: }
112:
113: public final void setSizeOwnPlus(Tree tree) {
114: super .setSizeOwnPlus(tree);
115: _nodes = 1 + tree.nodes();
116: }
117:
118: public final void setSizeOwnPlus(Tree tree1, Tree tree2) {
119: super .setSizeOwnPlus(tree1, tree2);
120: _nodes = 1 + tree1.nodes() + tree2.nodes();
121: }
122:
123: int slotLength() {
124: return handler().linkLength() + Const4.INT_LENGTH;
125: }
126:
127: final LocalObjectContainer stream() {
128: return trans().file();
129: }
130:
131: final LocalTransaction trans() {
132: return _fieldTransaction.i_trans;
133: }
134:
135: public abstract void visit(Object obj);
136:
137: public abstract void visit(Visitor4 visitor,
138: int[] a_lowerAndUpperMatch);
139:
140: public abstract void visitAll(IntObjectVisitor visitor);
141:
142: public abstract void freespaceVisit(FreespaceVisitor visitor,
143: int index);
144:
145: public abstract int write(Indexable4 a_handler,
146: StatefulBuffer a_writer);
147:
148: public void visitFirst(FreespaceVisitor visitor) {
149: if (_preceding != null) {
150: ((IxTree) _preceding).visitFirst(visitor);
151: if (visitor.visited()) {
152: return;
153: }
154: }
155: freespaceVisit(visitor, 0);
156: if (visitor.visited()) {
157: return;
158: }
159: if (_subsequent != null) {
160: ((IxTree) _subsequent).visitFirst(visitor);
161: if (visitor.visited()) {
162: return;
163: }
164: }
165: }
166:
167: public void visitLast(FreespaceVisitor visitor) {
168: if (_subsequent != null) {
169: ((IxTree) _subsequent).visitLast(visitor);
170: if (visitor.visited()) {
171: return;
172: }
173: }
174: freespaceVisit(visitor, 0);
175: if (visitor.visited()) {
176: return;
177: }
178: if (_preceding != null) {
179: ((IxTree) _preceding).visitLast(visitor);
180: if (visitor.visited()) {
181: return;
182: }
183: }
184: }
185:
186: protected Tree shallowCloneInternal(Tree tree) {
187: IxTree ixTree = (IxTree) super .shallowCloneInternal(tree);
188: ixTree._fieldTransaction = _fieldTransaction;
189: ixTree._version = _version;
190: ixTree._nodes = _nodes;
191: return ixTree;
192: }
193:
194: public Object key() {
195: throw new NotImplementedException();
196: }
197: }
|