001: /*
002: * $Id: IntArrayIndex.java,v 1.4 2005/03/12 02:10:41 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004-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.util.List;
044:
045: import org.apache.commons.collections.primitives.ArrayIntList;
046: import org.apache.commons.collections.primitives.IntList;
047: import org.apache.commons.collections.primitives.adapters.IntListList;
048: import org.axiondb.AxionException;
049: import org.axiondb.Column;
050: import org.axiondb.IndexLoader;
051: import org.axiondb.engine.IntArrayIndexLoader;
052:
053: /**
054: * An {@link BaseArrayIndex array index}over integer keys.
055: *
056: * @version $Revision: 1.4 $ $Date: 2005/03/12 02:10:41 $
057: * @author Rodney Waldhoff
058: */
059: public class IntArrayIndex extends BaseArrayIndex {
060:
061: public IntArrayIndex(String name, Column column, boolean unique) {
062: super (name, column, unique);
063: _keys = new ArrayIntList();
064: }
065:
066: public IntArrayIndex(String name, Column column, boolean unique,
067: IntList keys, IntList values) {
068: super (name, column, unique, values);
069: _keys = keys;
070: }
071:
072: public IndexLoader getIndexLoader() {
073: return LOADER;
074: }
075:
076: public List getKeyList() {
077: return IntListList.wrap(_keys);
078: }
079:
080: public void truncate() throws AxionException {
081: super .truncate();
082: if (_keys != null) {
083: _keys.clear();
084: }
085: }
086:
087: protected int find(int seeking, boolean required) {
088: int high = _keys.size();
089: int low = 0;
090: int cur = 0;
091: boolean found = false;
092: while (low < high) {
093: cur = (high + low) / 2;
094: if (_keys.get(cur) == seeking) {
095: found = true;
096: break;
097: } else if (_keys.get(cur) > seeking) {
098: high = cur;
099: } else { // if(_keys.getInt(cur) < seeking)
100: if (low == cur) {
101: cur++;
102: }
103: low = cur;
104: }
105: }
106: if (!isUnique()) {
107: while (cur > 0 && seeking == _keys.get(cur - 1)) {
108: cur--;
109: }
110: }
111: if (!found) {
112: return required ? -1 : cur;
113: }
114: return cur;
115: }
116:
117: protected int find(Object value, boolean required) {
118: return find(((Integer) value).intValue(), required);
119: }
120:
121: protected List getKeyList(int minIndex, int maxIndex) {
122: return IntListList.wrap(_keys.subList(minIndex, maxIndex));
123: }
124:
125: protected int insertKey(int seeking) throws AxionException {
126: int high = _keys.size();
127: int low = 0;
128: int cur = 0;
129: while (low < high) {
130: cur = (high + low) / 2;
131: if (_keys.get(cur) == seeking) {
132: if (isUnique()) {
133: throw new AxionException("Expected "
134: + getIndexedColumn()
135: + " to be unique, found " + seeking
136: + " already.");
137: }
138: break;
139: } else if (_keys.get(cur) > seeking) {
140: high = cur;
141: } else { // if(_keys.getInt(cur) < seeking)
142: if (low == cur) {
143: cur++;
144: }
145: low = cur;
146: }
147: }
148: _keys.add(cur, seeking);
149: return cur;
150: }
151:
152: protected int insertKey(Object value) throws AxionException {
153: int seeking = ((Integer) value).intValue();
154: return insertKey(seeking);
155: }
156:
157: protected int removeKey(int seeking) throws AxionException {
158: int index = find(seeking, true);
159: if (-1 != index) {
160: _keys.removeElementAt(index);
161: }
162: return index;
163: }
164:
165: protected int removeKey(Object value) throws AxionException {
166: int seeking = ((Integer) value).intValue();
167: return removeKey(seeking);
168: }
169:
170: protected void removeKeyAt(int index) throws AxionException {
171: _keys.removeElementAt(index);
172: }
173:
174: private static final IndexLoader LOADER = new IntArrayIndexLoader();
175:
176: private IntList _keys = null;
177: }
|