1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact cg@cdegroot.com.
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Contributions are (C) Copyright 2000 by their associated contributors.
44 *
45 */
46
47 package jdbm.htree;
48
49 import jdbm.RecordManager;
50 import jdbm.helper.FastIterator;
51 import java.io.IOException;
52
53 /**
54 * Persistent hashtable implementation for PageManager.
55 * Implemented as an H*Tree structure.
56 *
57 * WARNING! If this instance is used in a transactional context, it
58 * *must* be discarded after a rollback.
59 *
60 * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
61 * @version $Id: HTree.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $
62 */
63 public class HTree
64 {
65
66 /**
67 * Root hash directory.
68 */
69 private HashDirectory _root;
70
71
72 /**
73 * Private constructor
74 *
75 * @param root Root hash directory.
76 */
77 private HTree( HashDirectory root ) {
78 _root = root;
79 }
80
81
82 /**
83 * Create a persistent hashtable.
84 *
85 * @param recman Record manager used for persistence.
86 */
87 public static HTree createInstance( RecordManager recman )
88 throws IOException
89 {
90 HashDirectory root;
91 long recid;
92
93 root = new HashDirectory( (byte) 0 );
94 recid = recman.insert( root );
95 root.setPersistenceContext( recman, recid );
96
97 return new HTree( root );
98 }
99
100
101 /**
102 * Load a persistent hashtable
103 *
104 * @param recman RecordManager used to store the persistent hashtable
105 * @param root_recid Record id of the root directory of the HTree
106 */
107 public static HTree load( RecordManager recman, long root_recid )
108 throws IOException
109 {
110 HTree tree;
111 HashDirectory root;
112
113 root = (HashDirectory) recman.fetch( root_recid );
114 root.setPersistenceContext( recman, root_recid );
115 tree = new HTree( root );
116 return tree;
117 }
118
119
120 /**
121 * Associates the specified value with the specified key.
122 *
123 * @param key key with which the specified value is to be assocated.
124 * @param value value to be associated with the specified key.
125 */
126 public synchronized void put(Object key, Object value)
127 throws IOException
128 {
129 _root.put(key, value);
130 }
131
132
133 /**
134 * Returns the value which is associated with the given key. Returns
135 * <code>null</code> if there is not association for this key.
136 *
137 * @param key key whose associated value is to be returned
138 */
139 public synchronized Object get(Object key)
140 throws IOException
141 {
142 return _root.get(key);
143 }
144
145
146 /**
147 * Remove the value which is associated with the given key. If the
148 * key does not exist, this method simply ignores the operation.
149 *
150 * @param key key whose associated value is to be removed
151 */
152 public synchronized void remove(Object key)
153 throws IOException
154 {
155 _root.remove(key);
156 }
157
158
159 /**
160 * Returns an enumeration of the keys contained in this
161 */
162 public synchronized FastIterator keys()
163 throws IOException
164 {
165 return _root.keys();
166 }
167
168
169 /**
170 * Returns an enumeration of the values contained in this
171 */
172 public synchronized FastIterator values()
173 throws IOException
174 {
175 return _root.values();
176 }
177
178
179 /**
180 * Get the record identifier used to load this hashtable.
181 */
182 public long getRecid()
183 {
184 return _root.getRecid();
185 }
186
187 }
188