1 8 9 package com.sleepycat.persist; 10 11 import java.util.Iterator ; 12 13 import com.sleepycat.je.DatabaseEntry; 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.LockMode; 16 import com.sleepycat.je.OperationStatus; 17 import com.sleepycat.util.keyrange.RangeCursor; 18 19 25 class BasicCursor<V> implements EntityCursor<V> { 26 27 RangeCursor cursor; 28 ValueAdapter<V> adapter; 29 DatabaseEntry key; 30 DatabaseEntry pkey; 31 DatabaseEntry data; 32 33 BasicCursor(RangeCursor cursor, ValueAdapter<V> adapter) { 34 this.cursor = cursor; 35 this.adapter = adapter; 36 key = adapter.initKey(); 37 pkey = adapter.initPKey(); 38 data = adapter.initData(); 39 } 40 41 public V first() 42 throws DatabaseException { 43 44 return first(null); 45 } 46 47 public V first(LockMode lockMode) 48 throws DatabaseException { 49 50 return returnValue(cursor.getFirst(key, pkey, data, lockMode)); 51 } 52 53 public V last() 54 throws DatabaseException { 55 56 return last(null); 57 } 58 59 public V last(LockMode lockMode) 60 throws DatabaseException { 61 62 return returnValue(cursor.getLast(key, pkey, data, lockMode)); 63 } 64 65 public V next() 66 throws DatabaseException { 67 68 return next(null); 69 } 70 71 public V next(LockMode lockMode) 72 throws DatabaseException { 73 74 return returnValue(cursor.getNext(key, pkey, data, lockMode)); 75 } 76 77 public V nextDup() 78 throws DatabaseException { 79 80 return nextDup(null); 81 } 82 83 public V nextDup(LockMode lockMode) 84 throws DatabaseException { 85 86 checkInitialized(); 87 return returnValue(cursor.getNextDup(key, pkey, data, lockMode)); 88 } 89 90 public V nextNoDup() 91 throws DatabaseException { 92 93 return nextNoDup(null); 94 } 95 96 public V nextNoDup(LockMode lockMode) 97 throws DatabaseException { 98 99 return returnValue(cursor.getNextNoDup(key, pkey, data, lockMode)); 100 } 101 102 public V prev() 103 throws DatabaseException { 104 105 return prev(null); 106 } 107 108 public V prev(LockMode lockMode) 109 throws DatabaseException { 110 111 return returnValue(cursor.getPrev(key, pkey, data, lockMode)); 112 } 113 114 public V prevDup() 115 throws DatabaseException { 116 117 return prevDup(null); 118 } 119 120 public V prevDup(LockMode lockMode) 121 throws DatabaseException { 122 123 checkInitialized(); 124 return returnValue(cursor.getPrevDup(key, pkey, data, lockMode)); 125 } 126 127 public V prevNoDup() 128 throws DatabaseException { 129 130 return prevNoDup(null); 131 } 132 133 public V prevNoDup(LockMode lockMode) 134 throws DatabaseException { 135 136 return returnValue(cursor.getPrevNoDup(key, pkey, data, lockMode)); 137 } 138 139 public V current() 140 throws DatabaseException { 141 142 return current(null); 143 } 144 145 public V current(LockMode lockMode) 146 throws DatabaseException { 147 148 checkInitialized(); 149 return returnValue(cursor.getCurrent(key, pkey, data, lockMode)); 150 } 151 152 public int count() 153 throws DatabaseException { 154 155 checkInitialized(); 156 return cursor.count(); 157 } 158 159 public Iterator <V> iterator() { 160 return iterator(null); 161 } 162 163 public Iterator <V> iterator(LockMode lockMode) { 164 return new BasicIterator(this, lockMode); 165 } 166 167 public boolean update(V entity) 168 throws DatabaseException { 169 170 checkInitialized(); 171 adapter.valueToData(entity, data); 172 return cursor.putCurrent(data) == OperationStatus.SUCCESS; 173 } 174 175 public boolean delete() 176 throws DatabaseException { 177 178 checkInitialized(); 179 return cursor.delete() == OperationStatus.SUCCESS; 180 } 181 182 public EntityCursor<V> dup() 183 throws DatabaseException { 184 185 return new BasicCursor<V>(cursor.dup(true), adapter); 186 } 187 188 public void close() 189 throws DatabaseException { 190 191 cursor.close(); 192 } 193 194 void checkInitialized() 195 throws IllegalStateException { 196 197 if (!cursor.isInitialized()) { 198 throw new IllegalStateException 199 ("Cursor is not initialized at a valid position"); 200 } 201 } 202 203 V returnValue(OperationStatus status) { 204 V value; 205 if (status == OperationStatus.SUCCESS) { 206 value = adapter.entryToValue(key, pkey, data); 207 } else { 208 value = null; 209 } 210 211 adapter.clearEntries(key, pkey, data); 212 return value; 213 } 214 } 215 | Popular Tags |