KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > SinglevaluedBtree


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20
21 import org.netbeans.mdr.persistence.*;
22 import org.netbeans.mdr.util.MapEntryImpl;
23 import java.util.*;
24 import java.text.*;
25
26 /**
27  * Btree implementation of SinglevaluedIndex
28  *
29  * @author Dana Bergen
30  * @version 1.0
31  */

32 public class SinglevaluedBtree extends Btree implements SinglevaluedIndex {
33
34     public SinglevaluedBtree(String JavaDoc name, Storage.EntryType keyType,
35                      Storage.EntryType dataType,
36                  BtreePageSource pageSource)
37                  throws StorageException {
38     super(name, keyType, dataType, pageSource);
39     }
40
41     protected void init() throws StorageException {
42         uniqueKeys = true;
43     uniqueValues = false;
44     super.init();
45     }
46
47     /**
48      * No-argument constructor for reconstructing via read(). Only used with
49      * Btree's whose pageSource is a BtreeMDRSource.
50      */

51     public SinglevaluedBtree() {
52     }
53
54     /*
55      *
56      * SinglevaluedIndex interface methods
57      *
58      */

59
60     /**
61      * Return a single object associated with the specified key.
62      *
63      * @param key key for lookup
64      *
65      * @return the data stored in the index for that key, or null if
66      * not found
67      *
68      * @exception StorageException If there was a problem reading
69      * pages
70      */

71     public Object JavaDoc getIfExists(Object JavaDoc key)
72         throws StorageException {
73     return getIfExistsInternal(key, null);
74     }
75
76     /** Like getIfExists, but if the index contains keys, this returns the object
77      * corresponding to the key
78      * @return
79      * @param key
80      * @throws StorageException
81      */

82     public Object JavaDoc getObjectIfExists(Object JavaDoc key, SinglevaluedIndex repos)
83         throws StorageException {
84     return getIfExistsInternal(key, repos);
85     }
86
87     /* get item or object */
88     private Object JavaDoc getIfExistsInternal(Object JavaDoc key, SinglevaluedIndex repos)
89         throws StorageException {
90
91         beginRead();
92     try {
93         byte[] keyBuffer;
94         byte[] dataBuffer;
95         Object JavaDoc result;
96
97         if ((keyBuffer = keyInfo.toBuffer(key)) == null) {
98         throw new StorageBadRequestException(
99           MessageFormat.format(
100           "Invalid key type for this index: {0} received, {1} expected",
101                 new Object JavaDoc[] {
102                 key.getClass().getName(),
103                 keyInfo.typeName()} ));
104         }
105
106         BtreePage root = pageSource.getPage(rootPageId, this);
107         dataBuffer = root.get(keyBuffer);
108         if (dataBuffer != null) {
109             if (repos == null) {
110             result = dataInfo.fromBuffer(dataBuffer);
111         }
112         else {
113             result = dataInfo.objectFromBuffer(dataBuffer, repos);
114         }
115         } else {
116         result = null;
117         }
118         pageSource.unpinPage(root);
119         return result;
120     } finally {
121         endRead();
122     }
123     }
124
125     /**
126      * Return a single object associated with the specified key.
127      *
128      * @param key key for lookup
129      *
130      * @return the data stored in the index for that key
131      *
132      * @exception StorageException If no matching item was found, or
133      * there was a problem reading pages
134      */

135     public Object JavaDoc get(Object JavaDoc key) throws StorageException {
136         beginRead();
137     try {
138     
139         Object JavaDoc result = getIfExists(key);
140         if (result == null) {
141         throw new StorageBadRequestException (
142             MessageFormat.format("Key {0} not found in index",
143             new Object JavaDoc[] {key}));
144         }
145         return result;
146     } finally {
147         endRead();
148     }
149     }
150
151     /** Like get, but if the index contains keys, this returns the object
152      * corresponding to the key
153      * @return
154      * @param key
155      * @throws StorageException
156      */

157     public Object JavaDoc getObject (Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
158         beginRead();
159     try {
160     
161         Object JavaDoc result = getObjectIfExists(key, repos);
162         if (result == null) {
163         throw new StorageBadRequestException (
164             MessageFormat.format("Key {0} not found in index",
165             new Object JavaDoc[] {key}));
166         }
167         return result;
168     } finally {
169         endRead();
170     }
171     }
172
173     /**
174      * Add a new entry to the index.
175      *
176      * @param key key to insert
177      * @param data data associated with key
178      *
179      * @return true if an existing entry was replaced
180      *
181      * @exception StorageException
182      * If a problem was encountered accessing storage
183      */

184     public boolean put(Object JavaDoc key, Object JavaDoc data) throws StorageException {
185         beginWrite();
186     try {
187         replaced = false;
188         btreePut(key, data, REPLACE_IF_EXISTS, 0);
189         return replaced;
190     } finally {
191         endWrite();
192     }
193     }
194     
195     /**
196      * Replace an index entry.
197      *
198      * @param key key whose entry is to be replaced
199      * @param data new data to be associated with key
200      *
201      * @exception StorageBadRequestException
202      * If key or data are incorrect type, or if no entry
203      * exists matching key.
204      * @exception StorageException
205      * If a problem was encountered accessing storage
206      */

207     public void replace(Object JavaDoc key, Object JavaDoc data) throws StorageException {
208         beginWrite();
209     try {
210         failed = false;
211         btreePut(key, data, REPLACE, 0);
212         if (failed) {
213         throw new StorageBadRequestException (
214             MessageFormat.format("Key {0} not found in index",
215             new Object JavaDoc[] {key}));
216         }
217     } finally {
218         endWrite();
219     }
220     }
221
222     /**
223      * Return a collection view of this btree's values.
224      *
225      * @return A collection
226      * @exception StorageException
227      */

228     public Collection values() throws StorageException {
229         return new BtreeCollection(this);
230     }
231     
232     /**
233      * Returns {@link BtreeEntryImpl} key-value pairs, where the key contains the queried prefix.
234      */

235     public synchronized Collection queryByKeyPrefix (Object JavaDoc prefix, SinglevaluedIndex primaryIndex) throws StorageException {
236         if (keyType != Storage.EntryType.STRING) {
237             throw new UnsupportedOperationException JavaDoc ("Key type must be EntryType.STRING");
238         }
239         
240         List result = new LinkedList ();
241         byte [] prefixBytes = keyInfo.toBuffer (prefix);
242         SearchResult location = getLocation (prefixBytes);
243         if (location.entryNum == location.page.numEntries())
244             BtreePage.getNext (null, location);
245         
246         while ((location.entryNum < location.page.numEntries()) &&
247             isPrefix (prefixBytes, location.page.getKey (location.entryNum))) {
248             byte [] key = location.page.getKey (location.entryNum);
249             byte [] data = location.page.getData (location.entryNum);
250             Object JavaDoc entry = new MapEntryImpl (
251                 keyInfo.objectFromBuffer (key, primaryIndex),
252                 dataInfo.objectFromBuffer (data, primaryIndex)
253             );
254             result.add (entry);
255             BtreePage.getNext (null, location);
256         } // while
257
return result;
258     }
259     
260     static boolean isPrefix (byte [] prefix, byte [] key) {
261         if (prefix.length > key.length)
262             return false;
263         for (int x = 0; x < prefix.length; x++) {
264             if (prefix [x] != key [x])
265                 return false;
266         }
267         return true;
268     }
269     
270 }
271
Popular Tags