001 /**
002 * JDBM LICENSE v1.00
003 *
004 * Redistribution and use of this software and associated documentation
005 * ("Software"), with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 * 1. Redistributions of source code must retain copyright
009 * statements and notices. Redistributions must also contain a
010 * copy of this document.
011 *
012 * 2. Redistributions in binary form must reproduce the
013 * above copyright notice, this list of conditions and the
014 * following disclaimer in the documentation and/or other
015 * materials provided with the distribution.
016 *
017 * 3. The name "JDBM" must not be used to endorse or promote
018 * products derived from this Software without prior written
019 * permission of Cees de Groot. For written permission,
020 * please contact cg@cdegroot.com.
021 *
022 * 4. Products derived from this Software may not be called "JDBM"
023 * nor may "JDBM" appear in their names without prior written
024 * permission of Cees de Groot.
025 *
026 * 5. Due credit should be given to the JDBM Project
027 * (http://jdbm.sourceforge.net/).
028 *
029 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
033 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040 * OF THE POSSIBILITY OF SUCH DAMAGE.
041 *
042 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
043 * Contributions are (C) Copyright 2000 by their associated contributors.
044 *
045 */
046
047 package jdbm.htree;
048
049 import jdbm.RecordManager;
050 import jdbm.helper.FastIterator;
051 import java.io.IOException;
052
053 /**
054 * Persistent hashtable implementation for PageManager.
055 * Implemented as an H*Tree structure.
056 *
057 * WARNING! If this instance is used in a transactional context, it
058 * *must* be discarded after a rollback.
059 *
060 * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
061 * @version $Id: HTree.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $
062 */
063 public class HTree
064 {
065
066 /**
067 * Root hash directory.
068 */
069 private HashDirectory _root;
070
071
072 /**
073 * Private constructor
074 *
075 * @param root Root hash directory.
076 */
077 private HTree( HashDirectory root ) {
078 _root = root;
079 }
080
081
082 /**
083 * Create a persistent hashtable.
084 *
085 * @param recman Record manager used for persistence.
086 */
087 public static HTree createInstance( RecordManager recman )
088 throws IOException
089 {
090 HashDirectory root;
091 long recid;
092
093 root = new HashDirectory( (byte) 0 );
094 recid = recman.insert( root );
095 root.setPersistenceContext( recman, recid );
096
097 return new HTree( root );
098 }
099
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