1 19 package org.netbeans.mdr.persistence.memoryimpl; 20 21 import java.util.*; 22 import java.io.*; 23 24 import org.netbeans.mdr.persistence.*; 25 import org.netbeans.mdr.util.*; 26 27 33 public class SinglevaluedIndexImpl extends Object implements SinglevaluedIndex, Streamable { 34 35 36 37 38 39 private String name; 40 private Storage.EntryType keyType; 41 private Storage.EntryType valueType; 42 protected Map table; 43 protected StorageImpl storage; 44 45 protected TransactionLog transLog = new TransactionLog (this); 46 47 48 49 50 51 52 public SinglevaluedIndexImpl () { 53 } 54 55 56 public SinglevaluedIndexImpl(String name, StorageImpl storage, Storage.EntryType keyType, Storage.EntryType valueType) { 57 this.name = name; 58 this.keyType = keyType; 59 this.valueType = valueType; 60 table = new HashMap(); 61 this.storage = storage; 62 } 63 64 65 66 67 68 public String getName() throws StorageException { 69 return this.name; 70 } 71 72 public Storage.EntryType getValueType() throws StorageException { 73 return this.valueType; 74 } 75 76 public Storage.EntryType getKeyType() throws StorageException { 77 return this.keyType; 78 } 79 80 85 public synchronized java.util.Set keySet() throws StorageException { 86 return table.keySet(); 87 } 88 89 97 public synchronized void add(Object key,Object value) throws StorageException { 98 Object original = table.put (key, value); 99 if (original != null) { 100 table.put(key, original); 101 throw new StorageBadRequestException ( 102 "Cannot add more than one item to key in single-valued index."); 103 } 104 transLog.logAdd (key); 105 } 106 107 112 public synchronized boolean remove(Object key) throws StorageException { 113 Object value = table.remove(key); 114 if (value != null) { 115 transLog.logRemove (key, value); 116 return true; 117 } else { 118 return false; 119 } 120 } 121 122 123 124 125 126 133 public synchronized boolean put(Object key,Object value) throws StorageException { 134 Object old = table.put(key, value); 135 if (old == null) { 136 transLog.logAdd (key); 137 return false; 138 } else { 139 transLog.logReplace (key, old); 140 return true; 141 } 142 } 143 144 153 public synchronized void replace(Object key, Object value) throws StorageException { 154 Object original = table.put(key, value); 155 if (original == null) { 156 table.remove(key); 157 throw new StorageBadRequestException ("Cannot replace item that does not exist in the index."); 158 } 159 transLog.logReplace (key, original); 160 } 161 162 169 public synchronized Object get(Object key) throws StorageException { 170 Object value = table.get(key); 171 if (value == null) { 172 throw new StorageBadRequestException ("Item not found: " + key); 173 } 174 return value; 175 } 176 177 183 public synchronized Object getObject (Object key, SinglevaluedIndex repos) throws StorageException { 184 if (keyType == Storage.EntryType.MOFID) { 185 synchronized (repos) { 186 return repos.get(get(key)); 187 } 188 } else { 189 return get(key); 190 } 191 } 192 193 199 public synchronized Object getIfExists(Object key) throws StorageException { 200 return table.get (key); 201 } 202 203 209 public synchronized Object getObjectIfExists (Object key, SinglevaluedIndex repos) throws StorageException { 210 Object val = getIfExists(key); 211 if (val == null) { 212 return null; 213 } else { 214 if (keyType == Storage.EntryType.MOFID) { 215 synchronized (repos) { 216 return repos.get(val); 217 } 218 } else { 219 return val; 220 } 221 } 222 } 223 224 230 public synchronized java.util.Collection values() throws StorageException { 231 return table.values(); 232 } 233 234 235 236 237 238 244 public void write(java.io.OutputStream out) throws StorageException { 245 try { 246 IOUtils.writeString(out, name); 247 out.write(keyType.encode()); 248 out.write(valueType.encode()); 249 Utils.write(out, table, storage); 250 } catch (java.io.IOException e) { 251 throw new StorageIOException(e); 252 } 253 } 254 258 public void read(java.io.InputStream is) throws StorageException { 259 try { 260 name = IOUtils.readString(is); 261 keyType = Storage.EntryType.decodeEntryType((byte) is.read()); 262 valueType = Storage.EntryType.decodeEntryType((byte) is.read()); 263 table = (Map) Utils.read(is, storage); 264 } catch (java.io.IOException e) { 265 throw new StorageIOException(e); 266 } 267 } 268 269 272 public synchronized Collection queryByKeyPrefix (Object prefix, SinglevaluedIndex primaryIndex) throws StorageException { 273 if (keyType != Storage.EntryType.STRING) { 274 throw new UnsupportedOperationException ("Key type must be EntryType.STRING"); 275 } 276 if (!(prefix instanceof String )) { 277 throw new StorageBadRequestException ("String object parameter expected."); 278 } 279 280 List result = new LinkedList (); 281 Iterator iter = table.keySet().iterator (); 282 while (iter.hasNext ()) { 283 String key = (String ) iter.next (); 284 if (key.startsWith ((String ) prefix)) { 285 result.add (new MapEntryImpl (key, getObject (key, primaryIndex))); 286 } 287 } 288 return result; 289 } 290 291 292 293 294 295 protected synchronized void rollBackChanges () throws StorageException { 296 transLog.rollBack (); 297 transLog.clear (); 298 } 299 300 protected synchronized void commitChanges () throws StorageException { 301 transLog.clear (); 302 } 303 304 305 306 307 308 309 public void changed (Object key) { 310 } 311 } 312 | Popular Tags |