1 8 9 package com.sleepycat.collections; 10 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 14 import com.sleepycat.compat.DbCompat; 15 import com.sleepycat.je.CursorConfig; 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.OperationStatus; 18 import com.sleepycat.util.RuntimeExceptionWrapper; 19 20 42 public abstract class StoredContainer implements Cloneable { 43 44 DataView view; 45 46 StoredContainer(DataView view) { 47 48 this.view = view; 49 } 50 51 59 public final boolean isWriteAllowed() { 60 61 return view.writeAllowed; 62 } 63 64 75 public final CursorConfig getCursorConfig() { 76 77 return DbCompat.cloneCursorConfig(view.cursorConfig); 78 } 79 80 96 public final boolean isDirtyReadAllowed() { 97 98 return view.readUncommittedAllowed; 99 } 100 101 106 public final boolean isDirtyRead() { 107 108 return view.cursorConfig.getReadUncommitted(); 109 } 110 111 122 public final boolean isTransactional() { 123 124 return view.transactional; 125 } 126 127 130 final StoredContainer configuredClone(CursorConfig config) { 131 132 try { 133 StoredContainer cont = (StoredContainer) clone(); 134 cont.view = cont.view.configuredView(config); 135 cont.initAfterClone(); 136 return cont; 137 } catch (CloneNotSupportedException willNeverOccur) { return null; } 138 } 139 140 143 void initAfterClone() { 144 } 145 146 156 public final boolean areDuplicatesAllowed() { 157 158 return view.dupsAllowed; 159 } 160 161 172 public final boolean areDuplicatesOrdered() { 173 174 return view.dupsOrdered; 175 } 176 177 188 public final boolean areKeysRenumbered() { 189 190 return view.keysRenumbered; 191 } 192 193 204 public final boolean isOrdered() { 205 206 return view.ordered; 207 } 208 209 217 public final boolean isSecondary() { 218 219 return view.isSecondary(); 220 } 221 222 234 public abstract int size(); 235 236 245 public boolean isEmpty() { 246 247 try { 248 return view.isEmpty(); 249 } catch (Exception e) { 250 throw convertException(e); 251 } 252 } 253 254 264 public void clear() { 265 266 boolean doAutoCommit = beginAutoCommit(); 267 try { 268 view.clear(); 269 commitAutoCommit(doAutoCommit); 270 } catch (Exception e) { 271 throw handleException(e, doAutoCommit); 272 } 273 } 274 275 Object get(Object key) { 276 277 DataCursor cursor = null; 278 try { 279 cursor = new DataCursor(view, false); 280 if (OperationStatus.SUCCESS == 281 cursor.getSearchKey(key, null, false)) { 282 return cursor.getCurrentValue(); 283 } else { 284 return null; 285 } 286 } catch (Exception e) { 287 throw StoredContainer.convertException(e); 288 } finally { 289 closeCursor(cursor); 290 } 291 } 292 293 Object put(final Object key, final Object value) { 294 295 DataCursor cursor = null; 296 boolean doAutoCommit = beginAutoCommit(); 297 try { 298 cursor = new DataCursor(view, true); 299 Object [] oldValue = new Object [1]; 300 cursor.put(key, value, oldValue, false); 301 closeCursor(cursor); 302 commitAutoCommit(doAutoCommit); 303 return oldValue[0]; 304 } catch (Exception e) { 305 closeCursor(cursor); 306 throw handleException(e, doAutoCommit); 307 } 308 } 309 310 final boolean removeKey(final Object key, final Object [] oldVal) { 311 312 DataCursor cursor = null; 313 boolean doAutoCommit = beginAutoCommit(); 314 try { 315 cursor = new DataCursor(view, true); 316 boolean found = false; 317 OperationStatus status = cursor.getSearchKey(key, null, true); 318 while (status == OperationStatus.SUCCESS) { 319 cursor.delete(); 320 found = true; 321 if (oldVal != null && oldVal[0] == null) { 322 oldVal[0] = cursor.getCurrentValue(); 323 } 324 status = areDuplicatesAllowed() ? 325 cursor.getNextDup(true): OperationStatus.NOTFOUND; 326 } 327 closeCursor(cursor); 328 commitAutoCommit(doAutoCommit); 329 return found; 330 } catch (Exception e) { 331 closeCursor(cursor); 332 throw handleException(e, doAutoCommit); 333 } 334 } 335 336 boolean containsKey(Object key) { 337 338 DataCursor cursor = null; 339 try { 340 cursor = new DataCursor(view, false); 341 return OperationStatus.SUCCESS == 342 cursor.getSearchKey(key, null, false); 343 } catch (Exception e) { 344 throw StoredContainer.convertException(e); 345 } finally { 346 closeCursor(cursor); 347 } 348 } 349 350 final boolean removeValue(Object value) { 351 352 DataCursor cursor = null; 353 boolean doAutoCommit = beginAutoCommit(); 354 try { 355 cursor = new DataCursor(view, true); 356 OperationStatus status = cursor.findValue(value, true); 357 if (status == OperationStatus.SUCCESS) { 358 cursor.delete(); 359 } 360 closeCursor(cursor); 361 commitAutoCommit(doAutoCommit); 362 return (status == OperationStatus.SUCCESS); 363 } catch (Exception e) { 364 closeCursor(cursor); 365 throw handleException(e, doAutoCommit); 366 } 367 } 368 369 boolean containsValue(Object value) { 370 371 DataCursor cursor = null; 372 try { 373 cursor = new DataCursor(view, false); 374 OperationStatus status = cursor.findValue(value, true); 375 return (status == OperationStatus.SUCCESS); 376 } catch (Exception e) { 377 throw StoredContainer.convertException(e); 378 } finally { 379 closeCursor(cursor); 380 } 381 } 382 383 388 final Iterator storedOrExternalIterator(Collection coll) { 389 390 if (coll instanceof StoredCollection) { 391 return ((StoredCollection) coll).storedIterator(); 392 } else { 393 return coll.iterator(); 394 } 395 } 396 397 final void closeCursor(DataCursor cursor) { 398 399 if (cursor != null) { 400 try { 401 cursor.close(); 402 } catch (Exception e) { 403 throw StoredContainer.convertException(e); 404 } 405 } 406 } 407 408 final boolean beginAutoCommit() { 409 410 if (view.transactional) { 411 CurrentTransaction currentTxn = view.getCurrentTxn(); 412 try { 413 if (currentTxn.isAutoCommitAllowed()) { 414 currentTxn.beginTransaction(null); 415 return true; 416 } 417 } catch (DatabaseException e) { 418 throw new RuntimeExceptionWrapper(e); 419 } 420 } 421 return false; 422 } 423 424 final void commitAutoCommit(boolean doAutoCommit) 425 throws DatabaseException { 426 427 if (doAutoCommit) view.getCurrentTxn().commitTransaction(); 428 } 429 430 final RuntimeException handleException(Exception e, boolean doAutoCommit) { 431 432 if (doAutoCommit) { 433 try { 434 view.getCurrentTxn().abortTransaction(); 435 } catch (DatabaseException ignored) { 436 437 } 438 } 439 return StoredContainer.convertException(e); 440 } 441 442 static RuntimeException convertException(Exception e) { 443 444 if (e instanceof RuntimeException ) { 445 return (RuntimeException ) e; 446 } else { 447 return new RuntimeExceptionWrapper(e); 448 } 449 } 450 } 451 | Popular Tags |