1 8 9 package com.sleepycat.collections; 10 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import com.sleepycat.compat.DbCompat; 18 import com.sleepycat.je.CursorConfig; 19 import com.sleepycat.je.DatabaseEntry; 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.JoinConfig; 22 import com.sleepycat.je.OperationStatus; 23 24 45 public abstract class StoredCollection extends StoredContainer 46 implements Collection { 47 48 52 public static final int DEFAULT_ITERATOR_BLOCK_SIZE = 10; 53 54 private int iteratorBlockSize = DEFAULT_ITERATOR_BLOCK_SIZE; 55 56 StoredCollection(DataView view) { 57 58 super(view); 59 } 60 61 66 public int getIteratorBlockSize() { 67 68 return iteratorBlockSize; 69 } 70 71 78 public void setIteratorBlockSize(int blockSize) { 79 80 if (blockSize < 2) { 81 throw new IllegalArgumentException 82 ("blockSize is less than two: " + blockSize); 83 } 84 85 iteratorBlockSize = blockSize; 86 } 87 88 final boolean add(Object key, Object value) { 89 90 DataCursor cursor = null; 91 boolean doAutoCommit = beginAutoCommit(); 92 try { 93 cursor = new DataCursor(view, true); 94 OperationStatus status = 95 cursor.putNoDupData(key, value, null, false); 96 closeCursor(cursor); 97 commitAutoCommit(doAutoCommit); 98 return (status == OperationStatus.SUCCESS); 99 } catch (Exception e) { 100 closeCursor(cursor); 101 throw handleException(e, doAutoCommit); 102 } 103 } 104 105 BlockIterator blockIterator() { 106 return new BlockIterator(this, isWriteAllowed(), iteratorBlockSize); 107 } 108 109 131 public Iterator iterator() { 132 return blockIterator(); 133 } 134 135 149 public StoredIterator storedIterator() { 150 151 return storedIterator(isWriteAllowed()); 152 } 153 154 178 public StoredIterator storedIterator(boolean writeAllowed) { 179 180 try { 181 return new StoredIterator(this, writeAllowed && isWriteAllowed(), 182 null); 183 } catch (Exception e) { 184 throw StoredContainer.convertException(e); 185 } 186 } 187 188 194 public StoredIterator iterator(boolean writeAllowed) { 195 196 return storedIterator(writeAllowed); 197 } 198 199 206 public Object [] toArray() { 207 208 ArrayList list = new ArrayList (); 209 StoredIterator i = storedIterator(); 210 try { 211 while (i.hasNext()) { 212 list.add(i.next()); 213 } 214 } finally { 215 i.close(); 216 } 217 return list.toArray(); 218 } 219 220 229 public Object [] toArray(Object [] a) { 230 231 int j = 0; 232 StoredIterator i = storedIterator(); 233 try { 234 while (j < a.length && i.hasNext()) { 235 a[j++] = i.next(); 236 } 237 if (j < a.length) { 238 a[j] = null; 239 } else if (i.hasNext()) { 240 ArrayList list = new ArrayList (Arrays.asList(a)); 241 while (i.hasNext()) { 242 list.add(i.next()); 243 } 244 a = list.toArray(a); 245 } 246 } finally { 247 i.close(); 248 } 249 return a; 250 } 251 252 260 public boolean containsAll(Collection coll) { 261 Iterator i = storedOrExternalIterator(coll); 262 try { 263 while (i.hasNext()) { 264 if (!contains(i.next())) { 265 return false; 266 } 267 } 268 } finally { 269 StoredIterator.close(i); 270 } 271 return true; 272 } 273 274 288 public boolean addAll(Collection coll) { 289 Iterator i = null; 290 boolean doAutoCommit = beginAutoCommit(); 291 try { 292 i = storedOrExternalIterator(coll); 293 boolean changed = false; 294 while (i.hasNext()) { 295 if (add(i.next())) { 296 changed = true; 297 } 298 } 299 StoredIterator.close(i); 300 commitAutoCommit(doAutoCommit); 301 return changed; 302 } catch (Exception e) { 303 StoredIterator.close(i); 304 throw handleException(e, doAutoCommit); 305 } 306 } 307 308 318 public boolean removeAll(Collection coll) { 319 320 return removeAll(coll, true); 321 } 322 323 333 public boolean retainAll(Collection coll) { 334 335 return removeAll(coll, false); 336 } 337 338 private boolean removeAll(Collection coll, boolean ifExistsInColl) { 339 StoredIterator i = null; 340 boolean doAutoCommit = beginAutoCommit(); 341 try { 342 boolean changed = false; 343 i = storedIterator(); 344 while (i.hasNext()) { 345 if (ifExistsInColl == coll.contains(i.next())) { 346 i.remove(); 347 changed = true; 348 } 349 } 350 i.close(); 351 commitAutoCommit(doAutoCommit); 352 return changed; 353 } catch (Exception e) { 354 if (i != null) { 355 i.close(); 356 } 357 throw handleException(e, doAutoCommit); 358 } 359 } 360 361 370 public boolean equals(Object other) { 371 372 if (other instanceof Collection ) { 373 Collection otherColl = StoredCollection.copyCollection(other); 374 StoredIterator i = storedIterator(); 375 try { 376 while (i.hasNext()) { 377 if (!otherColl.remove(i.next())) { 378 return false; 379 } 380 } 381 return otherColl.isEmpty(); 382 } finally { 383 i.close(); 384 } 385 } else { 386 return false; 387 } 388 } 389 390 394 public int hashCode() { 395 return super.hashCode(); 396 } 397 398 408 public List toList() { 409 410 ArrayList list = new ArrayList (); 411 StoredIterator i = storedIterator(); 412 try { 413 while (i.hasNext()) list.add(i.next()); 414 return list; 415 } finally { 416 i.close(); 417 } 418 } 419 420 429 public String toString() { 430 StringBuffer buf = new StringBuffer (); 431 buf.append("["); 432 StoredIterator i = storedIterator(); 433 try { 434 while (i.hasNext()) { 435 if (buf.length() > 1) buf.append(','); 436 buf.append(i.next().toString()); 437 } 438 buf.append(']'); 439 return buf.toString(); 440 } finally { 441 i.close(); 442 } 443 } 444 445 public int size() { 447 448 boolean countDups = iterateDuplicates(); 449 if (DbCompat.DATABASE_COUNT && countDups && !view.range.hasBound()) { 450 try { 451 return (int) DbCompat.getDatabaseCount(view.db); 452 } catch (Exception e) { 453 throw StoredContainer.convertException(e); 454 } 455 } else { 456 int count = 0; 457 CursorConfig cursorConfig = view.currentTxn.isLockingMode() ? 458 CursorConfig.READ_UNCOMMITTED : null; 459 DataCursor cursor = null; 460 try { 461 cursor = new DataCursor(view, false, cursorConfig); 462 OperationStatus status = cursor.getFirst(false); 463 while (status == OperationStatus.SUCCESS) { 464 if (countDups) { 465 count += cursor.count(); 466 } else { 467 count += 1; 468 } 469 status = cursor.getNextNoDup(false); 470 } 471 } catch (Exception e) { 472 throw StoredContainer.convertException(e); 473 } finally { 474 closeCursor(cursor); 475 } 476 return count; 477 } 478 } 479 480 511 public StoredIterator join(StoredContainer[] indices, Object [] indexKeys, 512 JoinConfig joinConfig) { 513 514 try { 515 DataView[] indexViews = new DataView[indices.length]; 516 for (int i = 0; i < indices.length; i += 1) { 517 indexViews[i] = indices[i].view; 518 } 519 DataCursor cursor = view.join(indexViews, indexKeys, joinConfig); 520 return new StoredIterator(this, false, cursor); 521 } catch (Exception e) { 522 throw StoredContainer.convertException(e); 523 } 524 } 525 526 final Object getFirstOrLast(boolean doGetFirst) { 527 528 DataCursor cursor = null; 529 try { 530 cursor = new DataCursor(view, false); 531 OperationStatus status; 532 if (doGetFirst) { 533 status = cursor.getFirst(false); 534 } else { 535 status = cursor.getLast(false); 536 } 537 return (status == OperationStatus.SUCCESS) ? 538 makeIteratorData(null, cursor) : null; 539 } catch (Exception e) { 540 throw StoredContainer.convertException(e); 541 } finally { 542 closeCursor(cursor); 543 } 544 } 545 546 Object makeIteratorData(BaseIterator iterator, DataCursor cursor) { 547 548 return makeIteratorData(iterator, 549 cursor.getKeyThang(), 550 cursor.getPrimaryKeyThang(), 551 cursor.getValueThang()); 552 } 553 554 abstract Object makeIteratorData(BaseIterator iterator, 555 DatabaseEntry keyEntry, 556 DatabaseEntry priKeyEntry, 557 DatabaseEntry valueEntry); 558 559 abstract boolean hasValues(); 560 561 boolean iterateDuplicates() { 562 563 return true; 564 } 565 566 void checkIterAddAllowed() 567 throws UnsupportedOperationException { 568 569 if (!areDuplicatesAllowed()) { 570 throw new UnsupportedOperationException ("duplicates required"); 571 } 572 } 573 574 int getIndexOffset() { 575 576 return 0; 577 } 578 579 private static Collection copyCollection(Object other) { 580 581 if (other instanceof StoredCollection) { 582 return ((StoredCollection) other).toList(); 583 } else { 584 return new ArrayList ((Collection ) other); 585 } 586 } 587 } 588 | Popular Tags |