📄 storedcollection.java
字号:
try { i = storedOrExternalIterator(coll); boolean changed = false; while (i.hasNext()) { if (add(i.next())) { changed = true; } } StoredIterator.close(i); commitAutoCommit(doAutoCommit); return changed; } catch (Exception e) { StoredIterator.close(i); throw handleException(e, doAutoCommit); } } /** * Removes all this collection's elements that are also contained in the * specified collection (optional operation). * This method conforms to the {@link Collection#removeAll} interface. * * @throws UnsupportedOperationException if the collection is read-only. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean removeAll(Collection coll) { return removeAll(coll, true); } /** * Retains only the elements in this collection that are contained in the * specified collection (optional operation). * This method conforms to the {@link Collection#removeAll} interface. * * @throws UnsupportedOperationException if the collection is read-only. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean retainAll(Collection coll) { return removeAll(coll, false); } private boolean removeAll(Collection coll, boolean ifExistsInColl) { StoredIterator i = null; boolean doAutoCommit = beginAutoCommit(); try { boolean changed = false; i = storedIterator(); while (i.hasNext()) { if (ifExistsInColl == coll.contains(i.next())) { i.remove(); changed = true; } } i.close(); commitAutoCommit(doAutoCommit); return changed; } catch (Exception e) { if (i != null) { i.close(); } throw handleException(e, doAutoCommit); } } /** * Compares the specified object with this collection for equality. * A value comparison is performed by this method and the stored values * are compared rather than calling the equals() method of each element. * This method conforms to the {@link Collection#equals} interface. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public boolean equals(Object other) { if (other instanceof Collection) { Collection otherColl = StoredCollection.copyCollection(other); StoredIterator i = storedIterator(); try { while (i.hasNext()) { if (!otherColl.remove(i.next())) { return false; } } return otherColl.isEmpty(); } finally { i.close(); } } else { return false; } } /* * Add this in to keep FindBugs from whining at us about implementing * equals(), but not hashCode(). */ public int hashCode() { return super.hashCode(); } /** * Returns a copy of this collection as an ArrayList. This is the same as * {@link #toArray()} but returns a collection instead of an array. * * @return an {@link ArrayList} containing a copy of all elements in this * collection. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public List toList() { ArrayList list = new ArrayList(); StoredIterator i = storedIterator(); try { while (i.hasNext()) list.add(i.next()); return list; } finally { i.close(); } } /** * Converts the collection to a string representation for debugging. * WARNING: The returned string may be very large. * * @return the string representation. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public String toString() { StringBuffer buf = new StringBuffer(); buf.append("["); StoredIterator i = storedIterator(); try { while (i.hasNext()) { if (buf.length() > 1) buf.append(','); buf.append(i.next().toString()); } buf.append(']'); return buf.toString(); } finally { i.close(); } } // Inherit javadoc public int size() { int count = 0; boolean countDups = iterateDuplicates(); CursorConfig cursorConfig = view.currentTxn.isLockingMode() ? CursorConfig.READ_UNCOMMITTED : null; DataCursor cursor = null; try { cursor = new DataCursor(view, false, cursorConfig); OperationStatus status = cursor.getFirst(false); while (status == OperationStatus.SUCCESS) { if (countDups) { count += cursor.count(); } else { count += 1; } status = cursor.getNextNoDup(false); } } catch (Exception e) { throw StoredContainer.convertException(e); } finally { closeCursor(cursor); } return count; } /** * Returns an iterator representing an equality join of the indices and * index key values specified. * This method does not exist in the standard {@link Collection} interface. * * <p><strong>Warning:</strong> The iterator returned must be explicitly * closed using {@link StoredIterator#close()} or {@link * StoredIterator#close(java.util.Iterator)} to release the underlying * database cursor resources.</p> * * <p>The returned iterator supports only the two methods: hasNext() and * next(). All other methods will throw UnsupportedOperationException.</p> * * @param indices is an array of indices with elements corresponding to * those in the indexKeys array. * * @param indexKeys is an array of index key values identifying the * elements to be selected. * * @param joinConfig is the join configuration, or null to use the * default configuration. * * @return an iterator over the elements in this collection that match * all specified index key values. * * @throws IllegalArgumentException if this collection is indexed or if a * given index does not have the same store as this collection. * * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is * thrown. */ public StoredIterator join(StoredContainer[] indices, Object[] indexKeys, JoinConfig joinConfig) { try { DataView[] indexViews = new DataView[indices.length]; for (int i = 0; i < indices.length; i += 1) { indexViews[i] = indices[i].view; } DataCursor cursor = view.join(indexViews, indexKeys, joinConfig); return new StoredIterator(this, false, cursor); } catch (Exception e) { throw StoredContainer.convertException(e); } } final Object getFirstOrLast(boolean doGetFirst) { DataCursor cursor = null; try { cursor = new DataCursor(view, false); OperationStatus status; if (doGetFirst) { status = cursor.getFirst(false); } else { status = cursor.getLast(false); } return (status == OperationStatus.SUCCESS) ? makeIteratorData(null, cursor) : null; } catch (Exception e) { throw StoredContainer.convertException(e); } finally { closeCursor(cursor); } } Object makeIteratorData(BaseIterator iterator, DataCursor cursor) { return makeIteratorData(iterator, cursor.getKeyThang(), cursor.getPrimaryKeyThang(), cursor.getValueThang()); } abstract Object makeIteratorData(BaseIterator iterator, DatabaseEntry keyEntry, DatabaseEntry priKeyEntry, DatabaseEntry valueEntry); abstract boolean hasValues(); boolean iterateDuplicates() { return true; } void checkIterAddAllowed() throws UnsupportedOperationException { if (!areDuplicatesAllowed()) { throw new UnsupportedOperationException("duplicates required"); } } int getIndexOffset() { return 0; } private static Collection copyCollection(Object other) { if (other instanceof StoredCollection) { return ((StoredCollection) other).toList(); } else { return new ArrayList((Collection) other); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -