01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: KeysIndex.java,v 1.7.2.2 2008/01/07 15:14:18 cwl Exp $
07: */
08:
09: 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: /**
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: */
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, Class<SK> keyClass, EntryBinding keyBinding,
36: Class<PK> pkeyClass, EntryBinding pkeyBinding)
37: throws DatabaseException {
38:
39: super (db, keyClass, keyBinding, new DataValueAdapter<PK>(
40: pkeyClass, pkeyBinding));
41: this .pkeyBinding = pkeyBinding;
42: }
43:
44: /*
45: * Of the EntityIndex methods only get()/map()/sortedMap() are implemented
46: * here. All other methods are implemented by BasicIndex.
47: */
48:
49: public PK get(SK key) throws DatabaseException {
50:
51: return get(null, key, null);
52: }
53:
54: public PK get(Transaction txn, SK key, LockMode lockMode)
55: throws DatabaseException {
56:
57: DatabaseEntry keyEntry = new DatabaseEntry();
58: DatabaseEntry pkeyEntry = new DatabaseEntry();
59: keyBinding.objectToEntry(key, keyEntry);
60:
61: OperationStatus status = db.get(txn, keyEntry, pkeyEntry,
62: lockMode);
63:
64: if (status == OperationStatus.SUCCESS) {
65: return (PK) pkeyBinding.entryToObject(pkeyEntry);
66: } else {
67: return null;
68: }
69: }
70:
71: public Map<SK, PK> map() {
72: return sortedMap();
73: }
74:
75: public synchronized SortedMap<SK, PK> sortedMap() {
76: if (map == null) {
77: map = new StoredSortedMap(db, keyBinding, pkeyBinding, true);
78: }
79: return map;
80: }
81: }
|