001: /*
002: * $Id: MemoryTable.java,v 1.9 2005/12/22 09:02:31 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.tables;
042:
043: import java.util.ArrayList;
044: import java.util.NoSuchElementException;
045:
046: import org.apache.commons.collections.primitives.ArrayIntList;
047: import org.apache.commons.collections.primitives.IntCollection;
048: import org.apache.commons.collections.primitives.IntIterator;
049: import org.axiondb.AxionException;
050: import org.axiondb.Index;
051: import org.axiondb.Row;
052: import org.axiondb.RowCollection;
053: import org.axiondb.RowIterator;
054: import org.axiondb.Table;
055: import org.axiondb.engine.rowiterators.BaseRowIterator;
056: import org.axiondb.event.RowInsertedEvent;
057:
058: /**
059: * A memory-resident {@link Table}.
060: *
061: * @version $Revision: 1.9 $ $Date: 2005/12/22 09:02:31 $
062: * @author Chuck Burdick
063: * @author Ahimanikya Satapathy
064: */
065: public class MemoryTable extends BaseTable implements Table {
066:
067: public MemoryTable(String name) {
068: super (name);
069: }
070:
071: public MemoryTable(String name, String type) {
072: super (name);
073: setType(type);
074: }
075:
076: public void applyDeletes(IntCollection rowids)
077: throws AxionException {
078: applyDeletesToIndices(rowids);
079: applyDeletesToRows(rowids);
080: }
081:
082: public void applyInserts(RowCollection rows) throws AxionException {
083: applyInsertsToIndices(rows);
084: applyInsertsToRows(rows.rowIterator());
085: }
086:
087: public void applyUpdates(RowCollection rows) throws AxionException {
088: applyUpdatesToIndices(rows);
089: applyUpdatesToRows(rows.rowIterator());
090: }
091:
092: public final void freeRowId(int id) {
093: if (_freeIdPos >= 0 && id == _freeIds.get(_freeIdPos)) {
094: _freeIdPos--;
095: } else if (_nextFreeId > _rows.size() - 1) {
096: _nextFreeId--;
097: }
098: }
099:
100: public final int getNextRowId() {
101: if (_freeIds.isEmpty() || _freeIdPos >= _freeIds.size() - 1) {
102: return _nextFreeId = (_nextFreeId == -1 ? _rows.size()
103: : _nextFreeId + 1);
104: } else {
105: return _freeIds.get(++_freeIdPos);
106: }
107: }
108:
109: public final Row getRow(int id) {
110: if (id > _rows.size() - 1 || id < 0) {
111: return null;
112: }
113: return (Row) _rows.get(id);
114: }
115:
116: public final int getRowCount() {
117: return _rowCount;
118: }
119:
120: public void populateIndex(Index index) throws AxionException {
121: for (int i = 0, I = _rows.size(); i < I; i++) {
122: Row row = (Row) (_rows.get(i));
123: if (row != null) {
124: index
125: .rowInserted(new RowInsertedEvent(this , null,
126: row));
127: }
128: }
129: }
130:
131: public void truncate() throws AxionException {
132: _rows.clear();
133: _freeIds.clear();
134: _rowCount = 0;
135: truncateIndices();
136: }
137:
138: protected RowIterator getRowIterator() throws AxionException {
139: return new BaseRowIterator() {
140: Row _current = null;
141: int _currentId = -1;
142: int _currentIndex = -1;
143: int _nextId = 0;
144: int _nextIndex = 0;
145:
146: public Row current() {
147: if (!hasCurrent()) {
148: throw new NoSuchElementException("No current row.");
149: }
150: return _current;
151: }
152:
153: public final int currentIndex() {
154: return _currentIndex;
155: }
156:
157: public final boolean hasCurrent() {
158: return (null != _current);
159: }
160:
161: public final boolean hasNext() {
162: return nextIndex() < getRowCount();
163: }
164:
165: public final boolean hasPrevious() {
166: return nextIndex() > 0;
167: }
168:
169: public Row last() {
170: if (isEmpty()) {
171: throw new IllegalStateException("No rows in table.");
172: }
173: _nextIndex = getRowCount();
174: _nextId = _rows.size();
175: previous();
176:
177: _nextIndex++;
178: _nextId++;
179: return current();
180: }
181:
182: public Row next() {
183: if (!hasNext()) {
184: throw new NoSuchElementException("No next row");
185: }
186:
187: do {
188: _currentId = _nextId++;
189: _current = getRow(_currentId);
190: } while (null == _current);
191: _currentIndex = _nextIndex;
192: _nextIndex++;
193: return _current;
194: }
195:
196: public final int nextIndex() {
197: return _nextIndex;
198: }
199:
200: public Row previous() {
201: if (!hasPrevious()) {
202: throw new NoSuchElementException("No previous row");
203: }
204: do {
205: _currentId = (--_nextId);
206: _current = getRow(_currentId);
207: } while (null == _current);
208: _nextIndex--;
209: _currentIndex = _nextIndex;
210: return _current;
211: }
212:
213: public final int previousIndex() {
214: return _nextIndex - 1;
215: }
216:
217: public void remove() throws AxionException {
218: if (-1 == _currentIndex) {
219: throw new IllegalStateException("No current row.");
220: }
221: deleteRow(_current);
222: _nextIndex--;
223: _currentIndex = -1;
224: }
225:
226: public void reset() {
227: _current = null;
228: _nextIndex = 0;
229: _currentId = -1;
230: _currentIndex = -1;
231: _nextId = 0;
232: }
233:
234: public final void set(Row row) throws AxionException {
235: if (-1 == _currentIndex) {
236: throw new IllegalStateException("No current row.");
237: }
238: updateRow(_current, row);
239: }
240:
241: public final int size() throws AxionException {
242: return getRowCount();
243: }
244:
245: public String toString() {
246: return "MemoryTable(" + getName() + ")";
247: }
248: };
249: }
250:
251: private void applyDeleteToRow(int rowid) throws AxionException {
252: if (rowid > _rows.size() - 1) {
253: throw new AxionException("Can't delete non-existent row");
254: }
255: //_freeIds.add(rowid);
256: _rows.set(rowid, null);
257: _rowCount--;
258: }
259:
260: private void applyDeletesToRows(IntCollection rowids)
261: throws AxionException {
262: for (IntIterator iter = rowids.iterator(); iter.hasNext();) {
263: applyDeleteToRow(iter.next());
264: }
265: _freeIds.addAll(rowids);
266: }
267:
268: private void applyInsertsToRows(RowIterator rows)
269: throws AxionException {
270: for (Row row; rows.hasNext();) {
271: row = rows.next();
272: if (!_freeIds.isEmpty() && _freeIdPos > -1) {
273: _rows.set(_freeIds.removeElementAt(0), row);
274: _freeIdPos--;
275: } else {
276: _rows.add(row);
277: }
278: _rowCount++;
279: }
280: _nextFreeId = -1;
281: }
282:
283: private void applyUpdatesToRows(RowIterator rows)
284: throws AxionException {
285: for (Row row; rows.hasNext();) {
286: row = rows.next();
287: if (row.getIdentifier() > _rows.size() - 1) {
288: throw new AxionException(
289: "Can't update non-existent row");
290: }
291: _rows.set(row.getIdentifier(), row);
292: }
293: }
294:
295: private int _freeIdPos = -1;
296: private ArrayIntList _freeIds = new ArrayIntList();
297:
298: private int _nextFreeId = -1;
299: private int _rowCount = 0;
300:
301: private ArrayList _rows = new ArrayList();
302: }
|