KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > KeysIndex


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: KeysIndex.java,v 1.7 2006/10/30 21:14:30 bostic Exp $
7  */

8
9 package com.sleepycat.persist;
10
11 import java.util.Map JavaDoc;
12 import java.util.SortedMap JavaDoc;
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 JavaDoc<SK,PK> map;
34
35     KeysIndex(Database db,
36               Class JavaDoc<SK> keyClass,
37               EntryBinding keyBinding,
38               Class JavaDoc<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 JavaDoc<SK,PK> map() {
75         return sortedMap();
76     }
77
78     public synchronized SortedMap JavaDoc<SK,PK> sortedMap() {
79         if (map == null) {
80             map = new StoredSortedMap(db, keyBinding, pkeyBinding, true);
81         }
82         return map;
83     }
84 }
85
Popular Tags