001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredKeySet.java,v 1.31.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import java.util.Set;
012:
013: import com.sleepycat.bind.EntryBinding;
014: import com.sleepycat.je.Database;
015: import com.sleepycat.je.DatabaseEntry;
016: import com.sleepycat.je.DatabaseException;
017: import com.sleepycat.je.OperationStatus;
018:
019: /**
020: * The Set returned by Map.keySet() and which can also be constructed directly
021: * if a Map is not needed.
022: * Since this collection is a set it only contains one element for each key,
023: * even when duplicates are allowed. Key set iterators are therefore
024: * particularly useful for enumerating the unique keys of a store or index that
025: * allows duplicates.
026: *
027: * @author Mark Hayes
028: */
029: public class StoredKeySet extends StoredCollection implements Set {
030:
031: /**
032: * Creates a key set view of a {@link Database}.
033: *
034: * @param database is the Database underlying the new collection.
035: *
036: * @param keyBinding is the binding used to translate between key buffers
037: * and key objects.
038: *
039: * @param writeAllowed is true to create a read-write collection or false
040: * to create a read-only collection.
041: *
042: * @throws IllegalArgumentException if formats are not consistently
043: * defined or a parameter is invalid.
044: *
045: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
046: * thrown.
047: */
048: public StoredKeySet(Database database, EntryBinding keyBinding,
049: boolean writeAllowed) {
050:
051: super (new DataView(database, keyBinding, null, null,
052: writeAllowed, null));
053: }
054:
055: StoredKeySet(DataView keySetView) {
056:
057: super (keySetView);
058: }
059:
060: /**
061: * Adds the specified key to this set if it is not already present
062: * (optional operation).
063: * When a key is added the value in the underlying data store will be
064: * empty.
065: * This method conforms to the {@link Set#add} interface.
066: *
067: * @throws UnsupportedOperationException if the collection is indexed, or
068: * if the collection is read-only.
069: *
070: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
071: * thrown.
072: */
073: public boolean add(Object key) {
074:
075: DataCursor cursor = null;
076: boolean doAutoCommit = beginAutoCommit();
077: try {
078: cursor = new DataCursor(view, true);
079: OperationStatus status = cursor.putNoOverwrite(key, null,
080: false);
081: closeCursor(cursor);
082: commitAutoCommit(doAutoCommit);
083: return (status == OperationStatus.SUCCESS);
084: } catch (Exception e) {
085: closeCursor(cursor);
086: throw handleException(e, doAutoCommit);
087: }
088: }
089:
090: /**
091: * Removes the specified key from this set if it is present (optional
092: * operation).
093: * If duplicates are allowed, this method removes all duplicates for the
094: * given key.
095: * This method conforms to the {@link Set#remove} interface.
096: *
097: * @throws UnsupportedOperationException if the collection is read-only.
098: *
099: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
100: * thrown.
101: */
102: public boolean remove(Object key) {
103:
104: return removeKey(key, null);
105: }
106:
107: /**
108: * Returns true if this set contains the specified key.
109: * This method conforms to the {@link Set#contains} interface.
110: *
111: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
112: * thrown.
113: */
114: public boolean contains(Object key) {
115:
116: return containsKey(key);
117: }
118:
119: boolean hasValues() {
120:
121: return false;
122: }
123:
124: Object makeIteratorData(BaseIterator iterator,
125: DatabaseEntry keyEntry, DatabaseEntry priKeyEntry,
126: DatabaseEntry valueEntry) {
127:
128: return view.makeKey(keyEntry, priKeyEntry);
129: }
130:
131: boolean iterateDuplicates() {
132:
133: return false;
134: }
135: }
|