1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.collections; 10 11import java.util.Set; 12 13import com.sleepycat.bind.EntryBinding; 14import com.sleepycat.db.Database; 15import com.sleepycat.db.DatabaseEntry; 16import com.sleepycat.db.DatabaseException; 17import com.sleepycat.db.OperationStatus; 18 19/** 20 * The Set returned by Map.keySet() and which can also be constructed directly 21 * if a Map is not needed. 22 * Since this collection is a set it only contains one element for each key, 23 * even when duplicates are allowed. Key set iterators are therefore 24 * particularly useful for enumerating the unique keys of a store or index that 25 * allows duplicates. 26 * 27 * @author Mark Hayes 28 */ 29public class StoredKeySet<K> extends StoredCollection<K> implements Set<K> { 30 31 /** 32 * Creates a key set view of a {@link Database}. 33 * 34 * @param database is the Database underlying the new collection. 35 * 36 * @param keyBinding is the binding used to translate between key buffers 37 * and key objects. 38 * 39 * @param writeAllowed is true to create a read-write collection or false 40 * to create a read-only collection. 41 * 42 * @throws IllegalArgumentException if formats are not consistently 43 * defined or a parameter is invalid. 44 * 45 * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is 46 * thrown. 47 */ 48 public StoredKeySet(Database database, 49 EntryBinding<K> keyBinding, 50 boolean writeAllowed) { 51 52 super(new DataView(database, keyBinding, null, null, 53 writeAllowed, null)); 54 } 55 56 StoredKeySet(DataView keySetView) { 57 58 super(keySetView); 59 } 60 61 /** 62 * Adds the specified key to this set if it is not already present 63 * (optional operation). 64 * When a key is added the value in the underlying data store will be 65 * empty. 66 * This method conforms to the {@link Set#add} interface. 67 * 68 * @throws UnsupportedOperationException if the collection is indexed, or 69 * if the collection is read-only. 70 * 71 * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is 72 * thrown. 73 */ 74 public boolean add(K key) { 75 76 DataCursor cursor = null; 77 boolean doAutoCommit = beginAutoCommit(); 78 try { 79 cursor = new DataCursor(view, true); 80 OperationStatus status = cursor.putNoOverwrite(key, null, false); 81 closeCursor(cursor); 82 commitAutoCommit(doAutoCommit); 83 return (status == OperationStatus.SUCCESS); 84 } catch (Exception e) { 85 closeCursor(cursor); 86 throw handleException(e, doAutoCommit); 87 } 88 } 89 90 /** 91 * Removes the specified key from this set if it is present (optional 92 * operation). 93 * If duplicates are allowed, this method removes all duplicates for the 94 * given key. 95 * This method conforms to the {@link Set#remove} interface. 96 * 97 * @throws UnsupportedOperationException if the collection is read-only. 98 * 99 * @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 K makeIteratorData(BaseIterator iterator, 125 DatabaseEntry keyEntry, 126 DatabaseEntry priKeyEntry, 127 DatabaseEntry valueEntry) { 128 129 return (K) view.makeKey(keyEntry, priKeyEntry); 130 } 131 132 boolean iterateDuplicates() { 133 134 return false; 135 } 136} 137