KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > memoryimpl > SinglevaluedIndexImpl


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.memoryimpl;
20
21 import java.util.*;
22 import java.io.*;
23
24 import org.netbeans.mdr.persistence.*;
25 import org.netbeans.mdr.util.*;
26
27 /** Default memory implementation of {@link
28  * org.netbeans.mdr.persistence.SinglevaluedIndex} using
29  * {@link java.util.Hashtable}.
30  * @author Pavel Buzek
31  * @version
32  */

33 public class SinglevaluedIndexImpl extends Object JavaDoc implements SinglevaluedIndex, Streamable {
34
35     /* -------------------------------------------------------------------- */
36     /* -- Private attributes ---------------------------------------------- */
37     /* -------------------------------------------------------------------- */
38
39     private String JavaDoc name;
40     private Storage.EntryType keyType;
41     private Storage.EntryType valueType;
42     protected Map table;
43     protected StorageImpl storage;
44     
45     protected TransactionLog transLog = new TransactionLog (this);
46
47     /* -------------------------------------------------------------------- */
48     /* -- Constructors ---------------------------------------------------- */
49     /* -------------------------------------------------------------------- */
50
51     /** Constructor used when restoring the streamable index from a stream. */
52     public SinglevaluedIndexImpl () {
53     }
54     
55     /** Creates a new single-valued index. */
56     public SinglevaluedIndexImpl(String JavaDoc name, StorageImpl storage, Storage.EntryType keyType, Storage.EntryType valueType) {
57         this.name = name;
58         this.keyType = keyType;
59         this.valueType = valueType;
60         table = new HashMap();
61         this.storage = storage;
62     }
63
64     /* -------------------------------------------------------------------- */
65     /* -- Implementation of org.netbeans.mdr.persistence.Index ------------ */
66     /* -------------------------------------------------------------------- */
67     
68     public String JavaDoc getName() throws StorageException {
69         return this.name;
70     }
71
72     public Storage.EntryType getValueType() throws StorageException {
73         return this.valueType;
74     }
75
76     public Storage.EntryType getKeyType() throws StorageException {
77         return this.keyType;
78     }
79     
80     /** Returns a set view of the keys contained in this index.
81      * Returned set is read only and may not be modified.
82      * @return keys contained in this index
83      * @throws StorageException
84      */

85     public synchronized java.util.Set JavaDoc keySet() throws StorageException {
86         return table.keySet();
87     }
88     
89     /** Adds the specified value to values associated in this index with the
90      * specified key. If the index puts limit on number of values associated
91      * with one key and adding value would break this limit, it throws
92      * StorageBadRequestException.
93      * @param key
94      * @param value
95      * @throws StorageException
96      */

97     public synchronized void add(Object JavaDoc key,Object JavaDoc value) throws StorageException {
98         Object JavaDoc original = table.put (key, value);
99         if (original != null) {
100             table.put(key, original);
101             throw new StorageBadRequestException (
102                 "Cannot add more than one item to key in single-valued index.");
103         }
104         transLog.logAdd (key);
105     }
106     
107     /** Removes all values assosiated in the index with specified key.
108      * @return true if this index changed as a result of this call
109      * @param key
110      * @throws StorageException
111      */

112     public synchronized boolean remove(Object JavaDoc key) throws StorageException {
113         Object JavaDoc value = table.remove(key);
114         if (value != null) {
115             transLog.logRemove (key, value);
116             return true;
117         } else {
118             return false;
119         }
120     }
121
122     /* -------------------------------------------------------------------- */
123     /* -- Implementation of org.netbeans.mdr.persistence.SinglevaluedIndex */
124     /* -------------------------------------------------------------------- */
125     
126     /** Associates the specified value with the specified key in this index.
127      * @return true if there was an item in this index that was associated with the key
128      * prior to this call
129      * @param key
130      * @param value
131      * @throws StorageException
132      */

133     public synchronized boolean put(Object JavaDoc key,Object JavaDoc value) throws StorageException {
134         Object JavaDoc old = table.put(key, value);
135         if (old == null) {
136             transLog.logAdd (key);
137             return false;
138         } else {
139             transLog.logReplace (key, old);
140             return true;
141         }
142     }
143     
144     /** Replaces the original value associated with the specified key in this index
145      * with new value. If no value was associated with this key prior to this call
146      * StorageBadRequestException is thrown.
147      * @param key
148      * @param value
149      * @throws StorageException
150      * @throws StorageBadRequestException if the index has no entry with the given
151      * key
152      */

153     public synchronized void replace(Object JavaDoc key, Object JavaDoc value) throws StorageException {
154         Object JavaDoc original = table.put(key, value);
155         if (original == null) {
156             table.remove(key);
157             throw new StorageBadRequestException ("Cannot replace item that does not exist in the index.");
158         }
159         transLog.logReplace (key, original);
160     }
161     
162     /** Returns the value to which this index maps the specified key.
163      * StorageBadRequestException is thrown if there is no value for the key.
164      * @return value associated with specified key
165      * @param key
166      * @throws StorageException
167      * @throws StorageBadRequestException
168      */

169     public synchronized Object JavaDoc get(Object JavaDoc key) throws StorageException {
170         Object JavaDoc value = table.get(key);
171         if (value == null) {
172             throw new StorageBadRequestException ("Item not found: " + key);
173         }
174         return value;
175     }
176     
177     /** Like get, but if the index contains keys, this returns the object
178      * corresponding to the key
179      * @return
180      * @param key
181      * @throws StorageException
182      */

183     public synchronized Object JavaDoc getObject (Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
184         if (keyType == Storage.EntryType.MOFID) {
185             synchronized (repos) {
186                 return repos.get(get(key));
187             }
188         } else {
189             return get(key);
190         }
191     }
192
193     /** Returns the value to which this index maps the specified key
194      * or null if there is no value for this key.
195      * @return value associated with specified key or null
196      * @param key
197      * @throws StorageException
198      */

199     public synchronized Object JavaDoc getIfExists(Object JavaDoc key) throws StorageException {
200         return table.get (key);
201     }
202
203     /** Like getIfExists, but if the index contains keys, this returns the object
204      * corresponding to the key
205      * @return
206      * @param key
207      * @throws StorageException
208      */

209     public synchronized Object JavaDoc getObjectIfExists (Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
210         Object JavaDoc val = getIfExists(key);
211         if (val == null) {
212             return null;
213         } else {
214             if (keyType == Storage.EntryType.MOFID) {
215                 synchronized (repos) {
216                     return repos.get(val);
217                 }
218             } else {
219                 return val;
220             }
221         }
222     }
223     
224     /** Returns a collection view of the values contained in this index.
225      * Returned collection is read only and may not be modified.
226      * If this index has no items, empty Collection is returned.
227      * @return
228      * @throws StorageException
229      */

230     public synchronized java.util.Collection JavaDoc values() throws StorageException {
231         return table.values();
232     }
233
234     /* -------------------------------------------------------------------- */
235     /* -- Implementation of org.netbeans.mdr.persistence.Streamable ------- */
236     /* -------------------------------------------------------------------- */
237
238     /** This method will be used to move changed object from storage cache
239      * to the persistent part of storage. It writes the object`s state
240      * (set of attributes) in the stream as an array of bytes, for example
241      * in textual representation.
242      * @param outputStream OutputStream that holds value of a Streamable object
243      */

244     public void write(java.io.OutputStream JavaDoc out) throws StorageException {
245         try {
246             IOUtils.writeString(out, name);
247             out.write(keyType.encode());
248             out.write(valueType.encode());
249             Utils.write(out, table, storage);
250         } catch (java.io.IOException JavaDoc e) {
251             throw new StorageIOException(e);
252         }
253     }
254     /** Restore state of the Storable object from the stream.
255      * @param inputStream InputStream that represents an internal representation of fields of a Streamable object
256      * in which it was written by {@link write } method
257      */

258     public void read(java.io.InputStream JavaDoc is) throws StorageException {
259         try {
260             name = IOUtils.readString(is);
261             keyType = Storage.EntryType.decodeEntryType((byte) is.read());
262             valueType = Storage.EntryType.decodeEntryType((byte) is.read());
263             table = (Map) Utils.read(is, storage);
264         } catch (java.io.IOException JavaDoc e) {
265             throw new StorageIOException(e);
266         }
267     }
268
269     /**
270      * Returns key-value pairs, where the key contains the queried prefix.
271      */

272     public synchronized Collection queryByKeyPrefix (Object JavaDoc prefix, SinglevaluedIndex primaryIndex) throws StorageException {
273         if (keyType != Storage.EntryType.STRING) {
274             throw new UnsupportedOperationException JavaDoc ("Key type must be EntryType.STRING");
275         }
276         if (!(prefix instanceof String JavaDoc)) {
277             throw new StorageBadRequestException ("String object parameter expected.");
278         }
279         
280         List result = new LinkedList ();
281         Iterator iter = table.keySet().iterator ();
282         while (iter.hasNext ()) {
283             String JavaDoc key = (String JavaDoc) iter.next ();
284             if (key.startsWith ((String JavaDoc) prefix)) {
285                 result.add (new MapEntryImpl (key, getObject (key, primaryIndex)));
286             }
287         }
288         return result;
289     }
290     
291     /* -------------------------------------------------------------------- */
292     /* -- Transaction support --------------------------------------------- */
293     /* -------------------------------------------------------------------- */
294     
295     protected synchronized void rollBackChanges () throws StorageException {
296         transLog.rollBack ();
297         transLog.clear ();
298     }
299     
300     protected synchronized void commitChanges () throws StorageException {
301         transLog.clear ();
302     }
303     
304     /* -------------------------------------------------------------------- */
305     /* -- Methods not specified by any interface -------------------------- */
306     /* -------------------------------------------------------------------- */
307
308     /** Does nothing. */
309     public void changed (Object JavaDoc key) {
310     }
311 }
312
Popular Tags