001: /*
002: * $Id: MemoryTTree.java,v 1.1 2005/06/30 01:14:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.ext.indexes.ttree;
042:
043: import java.util.Comparator;
044:
045: import org.axiondb.AxionException;
046:
047: /**
048: * Memory T-Tree implemenation.
049: *
050: * @version $Revision: 1.1 $ $Date: 2005/06/30 01:14:44 $
051: * @author Pavels Andrejevs
052: */
053: public class MemoryTTree extends TTree {
054:
055: public class TTreeNode extends AbstractTTreeNode {
056:
057: private Object[] _keys;
058: private int[] _values;
059:
060: public TTreeNode(TTree tree) throws AxionException {
061: this (tree, 0);
062: }
063:
064: public TTreeNode(TTree tree, int fileId) throws AxionException {
065: super (tree, fileId);
066: }
067:
068: public void addKeyValue(Object key, int value)
069: throws AxionException {
070: setKey(_nodeSize, key);
071: setValue(_nodeSize, value);
072: _nodeSize++;
073: }
074:
075: public void copyKeysValues(int from, int to, int length)
076: throws AxionException {
077: copyKeys(from, to, length);
078: copyValues(from, to, length);
079: }
080:
081: public void insertKeyValue(int index, Object key, int value)
082: throws AxionException {
083: insertKey(index, key);
084: insertValue(index, value);
085: _nodeSize++;
086: }
087:
088: public void removeKeyValue(int index) throws AxionException {
089: removeKey(index);
090: removeValue(index);
091: _nodeSize--;
092: }
093:
094: public void setKeyValue(int index, Object key, int value)
095: throws AxionException {
096: setKey(index, key);
097: setValue(index, value);
098: }
099:
100: protected void copyKeys(int from, int to, int length)
101: throws AxionException {
102: System.arraycopy(_keys, from, _keys, to, length);
103: }
104:
105: protected final void copyValues(int from, int to, int length)
106: throws AxionException {
107: System.arraycopy(_values, from, _values, to, length);
108: }
109:
110: protected Object[] createKeys() {
111: if (_keys == null) {
112: _keys = new Object[_tree.getMaxNodeSize()];
113: }
114: return _keys;
115: }
116:
117: protected final int[] createValues() {
118: if (_values == null) {
119: _values = new int[_tree.getMaxNodeSize()];
120: }
121: return _values;
122: }
123:
124: protected void destroyKeys() {
125: _keys = null;
126: }
127:
128: protected final void destroyValues() {
129: _values = null;
130: }
131:
132: protected Object getKey(int index) throws AxionException {
133: return _keys[index];
134: }
135:
136: protected Object[] getKeys() throws AxionException {
137: return _keys;
138: }
139:
140: protected final int getValue(int index) throws AxionException {
141: return _values[index];
142: }
143:
144: protected final int[] getValues() throws AxionException {
145: return _values;
146: }
147:
148: protected void insertKey(int index, Object key)
149: throws AxionException {
150: System.arraycopy(_keys, index, _keys, index + 1, _nodeSize
151: - index);
152: _keys[index] = key;
153: }
154:
155: protected final void insertValue(int index, int value)
156: throws AxionException {
157: System.arraycopy(_values, index, _values, index + 1,
158: _nodeSize - index);
159: _values[index] = value;
160: }
161:
162: protected void removeKey(int index) throws AxionException {
163: System.arraycopy(_keys, index + 1, _keys, index, _nodeSize
164: - index - 1);
165: }
166:
167: protected final void removeValue(int index)
168: throws AxionException {
169: System.arraycopy(_values, index + 1, _values, index,
170: _nodeSize - index - 1);
171: }
172:
173: protected void setKey(int index, Object key)
174: throws AxionException {
175: _keys[index] = key;
176: }
177:
178: protected final void setValue(int index, int value)
179: throws AxionException {
180: _values[index] = value;
181: }
182: }
183:
184: public MemoryTTree(int maxNodeSize, Comparator comparator,
185: TTreeMetaData metaData) throws AxionException {
186: super (maxNodeSize, comparator, metaData);
187: }
188:
189: public AbstractTTreeNode newNode() throws AxionException {
190: return new TTreeNode(this );
191: }
192:
193: public AbstractTTreeNode newNode(int fileId) throws AxionException {
194: return new TTreeNode(this, fileId);
195: }
196:
197: }
|