1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: KeysIndex.java,v 1.1 2008/02/07 17:12:26 mark Exp $ 7 */ 8 9package com.sleepycat.persist; 10 11import java.util.Map; 12import java.util.SortedMap; 13 14import com.sleepycat.bind.EntryBinding; 15import com.sleepycat.collections.StoredSortedMap; 16import com.sleepycat.db.Database; 17import com.sleepycat.db.DatabaseEntry; 18import com.sleepycat.db.DatabaseException; 19import com.sleepycat.db.LockMode; 20import com.sleepycat.db.OperationStatus; 21import com.sleepycat.db.Transaction; 22 23/** 24 * The EntityIndex returned by SecondaryIndex.keysIndex(). This index maps 25 * secondary key to primary key. In Berkeley DB internal terms, this is a 26 * secondary database that is opened without associating it with a primary. 27 * 28 * @author Mark Hayes 29 */ 30class 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 /* 48 * Of the EntityIndex methods only get()/map()/sortedMap() are implemented 49 * here. All other methods are implemented by BasicIndex. 50 */ 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 boolean isUpdateAllowed() { 86 return false; 87 } 88} 89