1 8 9 package com.sleepycat.persist; 10 11 import java.util.Map ; 12 import java.util.SortedMap ; 13 14 import com.sleepycat.bind.EntryBinding; 15 import com.sleepycat.collections.StoredSortedMap; 16 import com.sleepycat.je.Database; 17 import com.sleepycat.je.DatabaseEntry; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.LockMode; 20 import com.sleepycat.je.OperationStatus; 21 import com.sleepycat.je.Transaction; 22 23 30 class KeysIndex<SK,PK> extends BasicIndex<SK,PK> { 31 32 private EntryBinding pkeyBinding; 33 private SortedMap <SK,PK> map; 34 35 KeysIndex(Database db, 36 Class <SK> keyClass, 37 EntryBinding keyBinding, 38 Class <PK> pkeyClass, 39 EntryBinding pkeyBinding) 40 throws DatabaseException { 41 42 super(db, keyClass, keyBinding, 43 new DataValueAdapter<PK>(pkeyClass, pkeyBinding)); 44 this.pkeyBinding = pkeyBinding; 45 } 46 47 51 52 public PK get(SK key) 53 throws DatabaseException { 54 55 return get(null, key, null); 56 } 57 58 public PK get(Transaction txn, SK key, LockMode lockMode) 59 throws DatabaseException { 60 61 DatabaseEntry keyEntry = new DatabaseEntry(); 62 DatabaseEntry pkeyEntry = new DatabaseEntry(); 63 keyBinding.objectToEntry(key, keyEntry); 64 65 OperationStatus status = db.get(txn, keyEntry, pkeyEntry, lockMode); 66 67 if (status == OperationStatus.SUCCESS) { 68 return (PK) pkeyBinding.entryToObject(pkeyEntry); 69 } else { 70 return null; 71 } 72 } 73 74 public Map <SK,PK> map() { 75 return sortedMap(); 76 } 77 78 public synchronized SortedMap <SK,PK> sortedMap() { 79 if (map == null) { 80 map = new StoredSortedMap(db, keyBinding, pkeyBinding, true); 81 } 82 return map; 83 } 84 } 85 | Popular Tags |