001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: StoredValueSet.java,v 1.40.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.EntityBinding;
014: import com.sleepycat.bind.EntryBinding;
015: import com.sleepycat.je.Database;
016: import com.sleepycat.je.DatabaseEntry;
017: import com.sleepycat.je.DatabaseException;
018: import com.sleepycat.je.OperationStatus;
019:
020: /**
021: * The Set returned by Map.values() and Map.duplicates(), and which can also be
022: * constructed directly if a Map is not needed.
023: * Although this collection is a set it may contain duplicate values. Only if
024: * an entity value binding is used are all elements guaranteed to be unique.
025: *
026: * @author Mark Hayes
027: */
028: public class StoredValueSet extends StoredCollection implements Set {
029:
030: /*
031: * This class is also used internally for the set returned by duplicates().
032: */
033:
034: /**
035: * Creates a value set view of a {@link Database}.
036: *
037: * @param database is the Database underlying the new collection.
038: *
039: * @param valueBinding is the binding used to translate between value
040: * buffers and value objects.
041: *
042: * @param writeAllowed is true to create a read-write collection or false
043: * to create a read-only collection.
044: *
045: * @throws IllegalArgumentException if formats are not consistently
046: * defined or a parameter is invalid.
047: *
048: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
049: * thrown.
050: */
051: public StoredValueSet(Database database, EntryBinding valueBinding,
052: boolean writeAllowed) {
053:
054: super (new DataView(database, null, valueBinding, null,
055: writeAllowed, null));
056: }
057:
058: /**
059: * Creates a value set entity view of a {@link Database}.
060: *
061: * @param database is the Database underlying the new collection.
062: *
063: * @param valueEntityBinding is the binding used to translate between
064: * key/value buffers and entity value objects.
065: *
066: * @param writeAllowed is true to create a read-write collection or false
067: * to create a read-only collection.
068: *
069: * @throws IllegalArgumentException if formats are not consistently
070: * defined or a parameter is invalid.
071: *
072: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
073: * thrown.
074: */
075: public StoredValueSet(Database database,
076: EntityBinding valueEntityBinding, boolean writeAllowed) {
077:
078: super (new DataView(database, null, null, valueEntityBinding,
079: writeAllowed, null));
080: }
081:
082: StoredValueSet(DataView valueSetView) {
083:
084: super (valueSetView);
085: }
086:
087: /**
088: * Adds the specified entity to this set if it is not already present
089: * (optional operation).
090: * This method conforms to the {@link Set#add} interface.
091: *
092: * @param entity is the entity to be added.
093: *
094: * @return true if the entity was added, that is the key-value pair
095: * represented by the entity was not previously present in the collection.
096: *
097: * @throws UnsupportedOperationException if the collection is read-only,
098: * if the collection is indexed, or if an entity binding is not used.
099: *
100: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
101: * thrown.
102: */
103: public boolean add(Object entity) {
104:
105: if (view.isSecondary()) {
106: throw new UnsupportedOperationException(
107: "add() not allowed with index");
108: } else if (view.range.isSingleKey()) {
109: /* entity is actually just a value in this case */
110: if (!view.dupsAllowed) {
111: throw new UnsupportedOperationException(
112: "duplicates required");
113: }
114: DataCursor cursor = null;
115: boolean doAutoCommit = beginAutoCommit();
116: try {
117: cursor = new DataCursor(view, true);
118: cursor.useRangeKey();
119: OperationStatus status = cursor.putNoDupData(null,
120: entity, null, true);
121: closeCursor(cursor);
122: commitAutoCommit(doAutoCommit);
123: return (status == OperationStatus.SUCCESS);
124: } catch (Exception e) {
125: closeCursor(cursor);
126: throw handleException(e, doAutoCommit);
127: }
128: } else if (view.entityBinding == null) {
129: throw new UnsupportedOperationException(
130: "add() requires entity binding");
131: } else {
132: return add(null, entity);
133: }
134: }
135:
136: /**
137: * Returns true if this set contains the specified element.
138: * This method conforms to the {@link java.util.Set#contains}
139: * interface.
140: *
141: * @param value the value to check.
142: *
143: * @return whether the set contains the given value.
144: */
145: public boolean contains(Object value) {
146:
147: return containsValue(value);
148: }
149:
150: /**
151: * Removes the specified value from this set if it is present (optional
152: * operation).
153: * If an entity binding is used, the key-value pair represented by the
154: * given entity is removed. If an entity binding is used, the first
155: * occurrence of a key-value pair with the given value is removed.
156: * This method conforms to the {@link Set#remove} interface.
157: *
158: * @throws UnsupportedOperationException if the collection is read-only.
159: *
160: * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is
161: * thrown.
162: */
163: public boolean remove(Object value) {
164:
165: return removeValue(value);
166: }
167:
168: Object makeIteratorData(BaseIterator iterator,
169: DatabaseEntry keyEntry, DatabaseEntry priKeyEntry,
170: DatabaseEntry valueEntry) {
171:
172: return view.makeValue(priKeyEntry, valueEntry);
173: }
174:
175: boolean hasValues() {
176:
177: return true;
178: }
179: }
|