00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.collections;
00011
00012 import java.util.Set;
00013
00014 import com.sleepycat.bind.EntityBinding;
00015 import com.sleepycat.bind.EntryBinding;
00016 import com.sleepycat.db.Database;
00017 import com.sleepycat.db.DatabaseException;
00018 import com.sleepycat.db.OperationStatus;
00019
00039 public class StoredValueSet extends StoredCollection implements Set {
00040
00041
00042
00043
00044
00045 private boolean isSingleKey;
00046
00064 public StoredValueSet(Database database,
00065 EntryBinding valueBinding,
00066 boolean writeAllowed) {
00067
00068 super(new DataView(database, null, valueBinding, null,
00069 writeAllowed, null));
00070 }
00071
00089 public StoredValueSet(Database database,
00090 EntityBinding valueEntityBinding,
00091 boolean writeAllowed) {
00092
00093 super(new DataView(database, null, null, valueEntityBinding,
00094 writeAllowed, null));
00095 }
00096
00097 StoredValueSet(DataView valueSetView) {
00098
00099 super(valueSetView);
00100 }
00101
00102 StoredValueSet(DataView valueSetView, boolean isSingleKey) {
00103
00104 super(valueSetView);
00105 this.isSingleKey = isSingleKey;
00106 }
00107
00124 public boolean add(Object entity) {
00125
00126 if (view.isSecondary()) {
00127 throw new UnsupportedOperationException(
00128 "add() not allowed with index");
00129 } else if (isSingleKey) {
00130
00131 if (!view.dupsAllowed) {
00132 throw new UnsupportedOperationException("duplicates required");
00133 }
00134 DataCursor cursor = null;
00135 boolean doAutoCommit = beginAutoCommit();
00136 try {
00137 cursor = new DataCursor(view, true);
00138 cursor.useRangeKey();
00139 OperationStatus status =
00140 cursor.putNoDupData(null, entity, null, true);
00141 closeCursor(cursor);
00142 commitAutoCommit(doAutoCommit);
00143 return (status == OperationStatus.SUCCESS);
00144 } catch (Exception e) {
00145 closeCursor(cursor);
00146 throw handleException(e, doAutoCommit);
00147 }
00148 } else if (view.entityBinding == null) {
00149 throw new UnsupportedOperationException(
00150 "add() requires entity binding");
00151 } else {
00152 return add(null, entity);
00153 }
00154 }
00155
00165 public boolean contains(Object value) {
00166
00167 return containsValue(value);
00168 }
00169
00183 public boolean remove(Object value) {
00184
00185 return removeValue(value);
00186 }
00187
00188
00189 public int size() {
00190
00191 if (!isSingleKey) {
00192 return super.size();
00193 }
00194 DataCursor cursor = null;
00195 try {
00196 cursor = new DataCursor(view, false);
00197 OperationStatus status = cursor.getFirst(false);
00198 if (status == OperationStatus.SUCCESS) {
00199 return cursor.count();
00200 } else {
00201 return 0;
00202 }
00203 } catch (Exception e) {
00204 throw StoredContainer.convertException(e);
00205 } finally {
00206 closeCursor(cursor);
00207 }
00208 }
00209
00210 Object makeIteratorData(StoredIterator iterator, DataCursor cursor)
00211 throws DatabaseException {
00212
00213 return cursor.getCurrentValue();
00214 }
00215
00216 boolean hasValues() {
00217
00218 return true;
00219 }
00220 }