001: /*
002: * $Id: ObjectBTreeIndex.java,v 1.8 2005/12/20 18:32:39 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-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.engine.indexes;
042:
043: import java.io.File;
044: import java.io.IOException;
045:
046: import org.apache.commons.collections.primitives.ArrayIntList;
047: import org.apache.commons.collections.primitives.IntListIterator;
048: import org.axiondb.AxionException;
049: import org.axiondb.Column;
050: import org.axiondb.DataType;
051: import org.axiondb.Function;
052: import org.axiondb.IndexLoader;
053: import org.axiondb.Row;
054: import org.axiondb.RowIterator;
055: import org.axiondb.RowSource;
056: import org.axiondb.Table;
057: import org.axiondb.engine.ObjectBTreeIndexLoader;
058: import org.axiondb.engine.rowiterators.EmptyRowIterator;
059: import org.axiondb.engine.rowiterators.LazyRowRowIterator;
060: import org.axiondb.event.RowEvent;
061: import org.axiondb.event.TableModificationListener;
062: import org.axiondb.functions.ComparisonFunction;
063: import org.axiondb.functions.EqualFunction;
064: import org.axiondb.functions.GreaterThanFunction;
065: import org.axiondb.functions.GreaterThanOrEqualFunction;
066: import org.axiondb.functions.IsNotNullFunction;
067: import org.axiondb.functions.IsNullFunction;
068: import org.axiondb.functions.LessThanFunction;
069: import org.axiondb.functions.LessThanOrEqualFunction;
070: import org.axiondb.util.NullObject;
071: import org.axiondb.util.ObjectBTree;
072:
073: /**
074: * A {@link BaseBTreeIndex B-Tree index}over <code>Object</code> keys.
075: *
076: * @version $Revision: 1.8 $ $Date: 2005/12/20 18:32:39 $
077: * @author Dave Pekarek Krohn
078: * @author Ritesh Adval
079: * @author Charles Ye
080: */
081: public class ObjectBTreeIndex extends BaseBTreeIndex implements
082: TableModificationListener {
083:
084: public ObjectBTreeIndex(String name, Column column, boolean unique)
085: throws AxionException {
086: this (name, column, unique, null);
087: }
088:
089: public ObjectBTreeIndex(String name, Column column, boolean unique,
090: File dataDirectory) throws AxionException {
091: this (name, column, unique, 1000, dataDirectory);
092: }
093:
094: private ObjectBTreeIndex(String name, Column column,
095: boolean unique, int minimizationFactor, File dataDirectory)
096: throws AxionException {
097: super (name, column, unique);
098: try {
099: _dataDirectory = dataDirectory;
100: _minimizationFactor = minimizationFactor;
101: _tree = createTree(_dataDirectory, getName(),
102: _minimizationFactor, getDataType());
103: } catch (IOException e) {
104: throw new AxionException("Unable to create index file", e);
105: } catch (ClassNotFoundException e) {
106: throw new AxionException("Unable to create index file", e);
107: }
108: }
109:
110: public final void changeRowId(Table table, Row row, int oldId,
111: int newId) throws AxionException {
112: try {
113: int colnum = table.getColumnIndex(getIndexedColumn()
114: .getName());
115: Object key = row.get(colnum);
116: _tree.replaceId(key, oldId, newId);
117: } catch (IOException e) {
118: throw new AxionException("Unable to change row id", e);
119: } catch (ClassNotFoundException e) {
120: throw new AxionException("Unable to change row id", e);
121: }
122: }
123:
124: public final ObjectBTree getBTree() {
125: return _tree;
126: }
127:
128: public IndexLoader getIndexLoader() {
129: return LOADER;
130: }
131:
132: public final RowIterator getInorderRowIterator(RowSource source)
133: throws AxionException {
134: IntListIterator resultIds = null;
135: try {
136: resultIds = _tree.inorderIterator();
137: } catch (IOException e) {
138: throw new AxionException(
139: "Unable to retrieve values from index" + getName(),
140: e);
141: } catch (ClassNotFoundException e) {
142: throw new AxionException(
143: "Unable to retrieve values from index" + getName(),
144: e);
145: }
146:
147: return new LazyRowRowIterator(source, resultIds, _tree.size());
148: }
149:
150: public final RowIterator getRowIterator(RowSource source,
151: Function function, Object value) throws AxionException {
152: IntListIterator resultIds = null;
153: try {
154: if (function instanceof ComparisonFunction) {
155: Object convertedValue = getIndexedColumn()
156: .getDataType().convert(value);
157:
158: if (null == convertedValue) {
159: // null fails all comparisions I support
160: return EmptyRowIterator.INSTANCE;
161: }
162:
163: if (function instanceof EqualFunction) {
164: if (!isUnique()) {
165: resultIds = _tree.getAll(convertedValue);
166: } else {
167: Integer result = _tree.get(convertedValue);
168: if (result == null) {
169: return EmptyRowIterator.INSTANCE;
170: } else {
171: ArrayIntList ids = new ArrayIntList(1);
172: ids.add(result.intValue());
173: return new LazyRowRowIterator(source, ids
174: .listIterator(), 1);
175: }
176: }
177: } else if (function instanceof LessThanFunction) {
178: resultIds = _tree.getAllTo(convertedValue);
179: } else if (function instanceof LessThanOrEqualFunction) {
180: resultIds = _tree.getAllTo(getIndexedColumn()
181: .getDataType().successor(convertedValue));
182: } else if (function instanceof GreaterThanFunction) {
183: resultIds = _tree.getAllFrom(getIndexedColumn()
184: .getDataType().successor(convertedValue));
185: } else if (function instanceof GreaterThanOrEqualFunction) {
186: resultIds = _tree.getAllFrom(convertedValue);
187: } else {
188: throw new AxionException("Unsupported function "
189: + function);
190: }
191: } else if (function instanceof IsNotNullFunction) {
192: resultIds = _tree.getAllExcludingNull();
193: } else if (function instanceof IsNullFunction) {
194: value = (value == null) ? getNullKey() : value;
195: resultIds = _tree.getAll(value);
196: } else {
197: throw new AxionException("Unsupported function "
198: + function);
199: }
200: } catch (IOException e) {
201: throw new AxionException(
202: "Unable to retrieve values from index" + getName(),
203: e);
204: } catch (ClassNotFoundException e) {
205: throw new AxionException(
206: "Unable to retrieve values from index" + getName(),
207: e);
208: }
209:
210: // return new LazyRowRowIterator(source, resultIds);
211:
212: // FIXME: There should be a better way to fix concurent modification issue
213: ArrayIntList ids = new ArrayIntList();
214: while (resultIds.hasNext()) {
215: ids.add(resultIds.next());
216: }
217: return new LazyRowRowIterator(source, ids.listIterator(), ids
218: .size());
219: }
220:
221: public final void rowDeleted(RowEvent event) throws AxionException {
222: String colName = getIndexedColumn().getName();
223: int colIndex = event.getTable().getColumnIndex(colName);
224: Object key = event.getOldRow().get(colIndex);
225: int rowid = event.getOldRow().getIdentifier();
226: key = (key == null) ? getNullKey() : key;
227: try {
228: _tree.delete(key, rowid);
229: } catch (IOException e) {
230: throw new AxionException("Unable to delete from index "
231: + getName(), e);
232: } catch (ClassNotFoundException e) {
233: throw new AxionException("Unable to delete from index "
234: + getName(), e);
235: }
236: }
237:
238: // TABLE MODIFICATION LISTENER
239: public final void rowInserted(RowEvent event) throws AxionException {
240: String colName = getIndexedColumn().getName();
241: int colIndex = event.getTable().getColumnIndex(colName);
242: Object value = event.getNewRow().get(colIndex);
243: value = (value == null) ? getNullKey() : value;
244: try {
245: _tree.insert(value, event.getNewRow().getIdentifier());
246: } catch (IOException e) {
247: throw new AxionException("Unable to insert into index "
248: + getName(), e);
249: } catch (ClassNotFoundException e) {
250: throw new AxionException("Unable to insert into index "
251: + getName(), e);
252: }
253: }
254:
255: public final void rowUpdated(RowEvent event) throws AxionException {
256: rowDeleted(event);
257: rowInserted(event);
258: }
259:
260: public void truncate() throws AxionException {
261: _tree.truncate();
262: }
263:
264: protected ObjectBTree createTree(File dataDirectory, String name,
265: int minimizationFactor, DataType dataType)
266: throws IOException, ClassNotFoundException {
267: return new ObjectBTree(dataDirectory, name, minimizationFactor,
268: dataType);
269: }
270:
271: protected Object getNullKey() {
272: return NullObject.INSTANCE;
273: }
274:
275: private static final IndexLoader LOADER = new ObjectBTreeIndexLoader();
276:
277: protected int _minimizationFactor;
278: private File _dataDirectory;
279: private ObjectBTree _tree = null;
280: }
|