Debian Codegraph @

jdbm1/src/main/java/jdbm/htree/HTree.java

raw
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 [email protected].
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:[email protected]">Alex Boisvert</a>
61 */
62 public class HTree
63 {
64
65 /**
66 * Root hash directory.
67 */
68 private HashDirectory _root;
69
70
71 /**
72 * Private constructor
73 *
74 * @param root Root hash directory.
75 */
76 private HTree( HashDirectory root ) {
77 _root = root;
78 }
79
80
81 /**
82 * Create a persistent hashtable.
83 *
84 * @param recman Record manager used for persistence.
85 */
86 public static HTree createInstance( RecordManager recman )
87 throws IOException
88 {
89 HashDirectory root;
90 long recid;
91
92 root = new HashDirectory( (byte) 0 );
93 recid = recman.insert( root );
94 root.setPersistenceContext( recman, recid );
95
96 return new HTree( root );
97 }
98
99
100 /**
101 * Load a persistent hashtable
102 *
103 * @param recman RecordManager used to store the persistent hashtable
104 * @param root_recid Record id of the root directory of the HTree
105 */
106 public static HTree load( RecordManager recman, long root_recid )
107 throws IOException
108 {
109 HTree tree;
110 HashDirectory root;
111
112 root = (HashDirectory) recman.fetch( root_recid );
113 root.setPersistenceContext( recman, root_recid );
114 tree = new HTree( root );
115 return tree;
116 }
117
118
119 /**
120 * Associates the specified value with the specified key.
121 *
122 * @param key key with which the specified value is to be assocated.
123 * @param value value to be associated with the specified key.
124 */
125 public synchronized void put(Object key, Object value)
126 throws IOException
127 {
128 _root.put(key, value);
129 }
130
131
132 /**
133 * Returns the value which is associated with the given key. Returns
134 * <code>null</code> if there is not association for this key.
135 *
136 * @param key key whose associated value is to be returned
137 */
138 public synchronized Object get(Object key)
139 throws IOException
140 {
141 return _root.get(key);
142 }
143
144
145 /**
146 * Remove the value which is associated with the given key. If the
147 * key does not exist, this method simply ignores the operation.
148 *
149 * @param key key whose associated value is to be removed
150 */
151 public synchronized void remove(Object key)
152 throws IOException
153 {
154 _root.remove(key);
155 }
156
157
158 /**
159 * Returns an enumeration of the keys contained in this
160 */
161 public synchronized FastIterator keys()
162 throws IOException
163 {
164 return _root.keys();
165 }
166
167
168 /**
169 * Returns an enumeration of the values contained in this
170 */
171 public synchronized FastIterator values()
172 throws IOException
173 {
174 return _root.values();
175 }
176
177
178 /**
179 * Get the record identifier used to load this hashtable.
180 */
181 public long getRecid()
182 {
183 return _root.getRecid();
184 }
185
186 }
187