1 8 9 package com.sleepycat.collections; 10 11 import java.util.Collection ; 12 import java.util.Collections ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import com.sleepycat.bind.EntityBinding; 18 import com.sleepycat.bind.EntryBinding; 19 import com.sleepycat.je.Database; 20 import com.sleepycat.util.keyrange.KeyRangeException; 21 22 36 public class StoredMap extends StoredContainer implements Map { 37 38 private StoredKeySet keySet; 39 private StoredEntrySet entrySet; 40 private StoredValueSet valueSet; 41 42 62 public StoredMap(Database database, EntryBinding keyBinding, 63 EntryBinding valueBinding, boolean writeAllowed) { 64 65 super(new DataView(database, keyBinding, valueBinding, null, 66 writeAllowed, null)); 67 initView(); 68 } 69 70 91 public StoredMap(Database database, EntryBinding keyBinding, 92 EntryBinding valueBinding, 93 PrimaryKeyAssigner keyAssigner) { 94 95 super(new DataView(database, keyBinding, valueBinding, null, 96 true, keyAssigner)); 97 initView(); 98 } 99 100 120 public StoredMap(Database database, EntryBinding keyBinding, 121 EntityBinding valueEntityBinding, boolean writeAllowed) { 122 123 super(new DataView(database, keyBinding, null, valueEntityBinding, 124 writeAllowed, null)); 125 initView(); 126 } 127 128 149 public StoredMap(Database database, EntryBinding keyBinding, 150 EntityBinding valueEntityBinding, 151 PrimaryKeyAssigner keyAssigner) { 152 153 super(new DataView(database, keyBinding, null, valueEntityBinding, 154 true, keyAssigner)); 155 initView(); 156 } 157 158 StoredMap(DataView view) { 159 160 super(view); 161 initView(); 162 } 163 164 167 void initAfterClone() { 168 initView(); 169 } 170 171 179 private void initView() { 180 181 182 if (isOrdered()) { 183 entrySet = new StoredSortedEntrySet(view); 184 } else { 185 entrySet = new StoredEntrySet(view); 186 } 187 188 189 DataView newView = view.keySetView(); 190 if (isOrdered()) { 191 keySet = new StoredSortedKeySet(newView); 192 } else { 193 keySet = new StoredKeySet(newView); 194 } 195 196 197 newView = view.valueSetView(); 198 if (isOrdered() && newView.canDeriveKeyFromValue()) { 199 valueSet = new StoredSortedValueSet(newView); 200 } else { 201 valueSet = new StoredValueSet(newView); 202 } 203 } 204 205 216 public Object get(Object key) { 217 218 return super.get(key); 219 } 220 221 246 public Object put(Object key, Object value) { 247 248 return super.put(key, value); 249 } 250 251 274 public Object append(Object value) { 275 276 boolean doAutoCommit = beginAutoCommit(); 277 try { 278 Object [] key = new Object [1]; 279 view.append(value, key, null); 280 commitAutoCommit(doAutoCommit); 281 return key[0]; 282 } catch (Exception e) { 283 throw handleException(e, doAutoCommit); 284 } 285 } 286 287 298 public Object remove(Object key) { 299 300 Object [] oldVal = new Object [1]; 301 removeKey(key, oldVal); 302 return oldVal[0]; 303 } 304 305 312 public boolean containsKey(Object key) { 313 314 return super.containsKey(key); 315 } 316 317 326 public boolean containsValue(Object value) { 327 328 return super.containsValue(value); 329 } 330 331 344 public void putAll(Map map) { 345 346 boolean doAutoCommit = beginAutoCommit(); 347 Iterator i = null; 348 try { 349 Collection coll = map.entrySet(); 350 i = storedOrExternalIterator(coll); 351 while (i.hasNext()) { 352 Map.Entry entry = (Map.Entry ) i.next(); 353 put(entry.getKey(), entry.getValue()); 354 } 355 StoredIterator.close(i); 356 commitAutoCommit(doAutoCommit); 357 } catch (Exception e) { 358 StoredIterator.close(i); 359 throw handleException(e, doAutoCommit); 360 } 361 } 362 363 381 public Set keySet() { 382 383 return keySet; 384 } 385 386 404 public Set entrySet() { 405 406 return entrySet; 407 } 408 409 429 public Collection values() { 430 431 return valueSet; 432 } 433 434 452 public Collection duplicates(Object key) { 453 454 try { 455 DataView newView = view.valueSetView(key); 456 return new StoredValueSet(newView); 457 } catch (KeyRangeException e) { 458 return Collections.EMPTY_SET; 459 } catch (Exception e) { 460 throw StoredContainer.convertException(e); 461 } 462 } 463 464 484 public Map duplicatesMap(Object secondaryKey, 485 EntryBinding primaryKeyBinding) { 486 try { 487 DataView newView = 488 view.duplicatesView(secondaryKey, primaryKeyBinding); 489 if (isOrdered()) { 490 return new StoredSortedMap(newView); 491 } else { 492 return new StoredMap(newView); 493 } 494 } catch (Exception e) { 495 throw StoredContainer.convertException(e); 496 } 497 } 498 499 508 public boolean equals(Object other) { 509 510 if (other instanceof Map ) { 511 return entrySet().equals(((Map ) other).entrySet()); 512 } else { 513 return false; 514 } 515 } 516 517 521 public int hashCode() { 522 return super.hashCode(); 523 } 524 525 public int size() { 527 return values().size(); 528 } 529 530 540 public String toString() { 541 542 return entrySet().toString(); 543 } 544 } 545 546 | Popular Tags |