001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: BasicCursor.java,v 1.8.2.3 2008/01/07 15:14:18 cwl Exp $
007: */
008:
009: package com.sleepycat.persist;
010:
011: import java.util.Iterator;
012:
013: import com.sleepycat.je.DatabaseEntry;
014: import com.sleepycat.je.DatabaseException;
015: import com.sleepycat.je.LockMode;
016: import com.sleepycat.je.OperationStatus;
017: import com.sleepycat.util.keyrange.RangeCursor;
018:
019: /**
020: * Implements EntityCursor and uses a ValueAdapter so that it can enumerate
021: * either keys or entities.
022: *
023: * @author Mark Hayes
024: */
025: class BasicCursor<V> implements EntityCursor<V> {
026:
027: RangeCursor cursor;
028: ValueAdapter<V> adapter;
029: DatabaseEntry key;
030: DatabaseEntry pkey;
031: DatabaseEntry data;
032:
033: BasicCursor(RangeCursor cursor, ValueAdapter<V> adapter) {
034: this .cursor = cursor;
035: this .adapter = adapter;
036: key = adapter.initKey();
037: pkey = adapter.initPKey();
038: data = adapter.initData();
039: }
040:
041: public V first() throws DatabaseException {
042:
043: return first(null);
044: }
045:
046: public V first(LockMode lockMode) throws DatabaseException {
047:
048: return returnValue(cursor.getFirst(key, pkey, data, lockMode));
049: }
050:
051: public V last() throws DatabaseException {
052:
053: return last(null);
054: }
055:
056: public V last(LockMode lockMode) throws DatabaseException {
057:
058: return returnValue(cursor.getLast(key, pkey, data, lockMode));
059: }
060:
061: public V next() throws DatabaseException {
062:
063: return next(null);
064: }
065:
066: public V next(LockMode lockMode) throws DatabaseException {
067:
068: return returnValue(cursor.getNext(key, pkey, data, lockMode));
069: }
070:
071: public V nextDup() throws DatabaseException {
072:
073: return nextDup(null);
074: }
075:
076: public V nextDup(LockMode lockMode) throws DatabaseException {
077:
078: checkInitialized();
079: return returnValue(cursor.getNextDup(key, pkey, data, lockMode));
080: }
081:
082: public V nextNoDup() throws DatabaseException {
083:
084: return nextNoDup(null);
085: }
086:
087: public V nextNoDup(LockMode lockMode) throws DatabaseException {
088:
089: return returnValue(cursor.getNextNoDup(key, pkey, data,
090: lockMode));
091: }
092:
093: public V prev() throws DatabaseException {
094:
095: return prev(null);
096: }
097:
098: public V prev(LockMode lockMode) throws DatabaseException {
099:
100: return returnValue(cursor.getPrev(key, pkey, data, lockMode));
101: }
102:
103: public V prevDup() throws DatabaseException {
104:
105: return prevDup(null);
106: }
107:
108: public V prevDup(LockMode lockMode) throws DatabaseException {
109:
110: checkInitialized();
111: return returnValue(cursor.getPrevDup(key, pkey, data, lockMode));
112: }
113:
114: public V prevNoDup() throws DatabaseException {
115:
116: return prevNoDup(null);
117: }
118:
119: public V prevNoDup(LockMode lockMode) throws DatabaseException {
120:
121: return returnValue(cursor.getPrevNoDup(key, pkey, data,
122: lockMode));
123: }
124:
125: public V current() throws DatabaseException {
126:
127: return current(null);
128: }
129:
130: public V current(LockMode lockMode) throws DatabaseException {
131:
132: checkInitialized();
133: return returnValue(cursor.getCurrent(key, pkey, data, lockMode));
134: }
135:
136: public int count() throws DatabaseException {
137:
138: checkInitialized();
139: return cursor.count();
140: }
141:
142: public Iterator<V> iterator() {
143: return iterator(null);
144: }
145:
146: public Iterator<V> iterator(LockMode lockMode) {
147: return new BasicIterator(this , lockMode);
148: }
149:
150: public boolean update(V entity) throws DatabaseException {
151:
152: checkInitialized();
153: adapter.valueToData(entity, data);
154: return cursor.putCurrent(data) == OperationStatus.SUCCESS;
155: }
156:
157: public boolean delete() throws DatabaseException {
158:
159: checkInitialized();
160: return cursor.delete() == OperationStatus.SUCCESS;
161: }
162:
163: public EntityCursor<V> dup() throws DatabaseException {
164:
165: return new BasicCursor<V>(cursor.dup(true), adapter);
166: }
167:
168: public void close() throws DatabaseException {
169:
170: cursor.close();
171: }
172:
173: void checkInitialized() throws IllegalStateException {
174:
175: if (!cursor.isInitialized()) {
176: throw new IllegalStateException(
177: "Cursor is not initialized at a valid position");
178: }
179: }
180:
181: V returnValue(OperationStatus status) {
182: V value;
183: if (status == OperationStatus.SUCCESS) {
184: value = adapter.entryToValue(key, pkey, data);
185: } else {
186: value = null;
187: }
188: /* Clear entries to save memory. */
189: adapter.clearEntries(key, pkey, data);
190: return value;
191: }
192: }
|