KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class PrimaryIndexImpl extends SinglevaluedIndexImpl {
28
29     /* Streams used to serialize data */
30     private ByteArrayOutputStream baoStrm = new ByteArrayOutputStream ();
31     private DataOutputStream daoStrm = new DataOutputStream (baoStrm);
32
33     public PrimaryIndexImpl(StorageImpl storage) {
34         super(StorageImpl.PRIMARY_INDEX_NAME, storage, Storage.EntryType.MOFID, Storage.EntryType.STREAMABLE);
35     }
36     
37     /** Removes all values assosiated in the index with specified key.
38      * @return true if this index changed as a result of this call
39      * @param key
40      * @throws StorageException
41      */

42     public synchronized boolean remove(Object JavaDoc key) throws StorageException {
43         Object JavaDoc original = table.remove(key);
44         if (original != null) {
45             Object JavaDoc valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original);
46             transLog.logRemove(key, valueLog);
47             return true;
48         } else
49             return false;
50     }
51     
52     /** Associates the specified value with the specified key in this index.
53      * @return true if there was an item in this index that was associated with the key
54      * prior to this call
55      * @param key
56      * @param value
57      * @throws StorageException
58      */

59     public synchronized boolean put(Object JavaDoc key,Object JavaDoc value) throws StorageException {
60         Object JavaDoc original = table.put(key, value);
61         if (original == null) {
62             transLog.logAdd(key);
63         } else {
64             Object JavaDoc valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original);
65             transLog.logReplace(key, valueLog);
66         }
67         return original != null;
68     }
69     
70     /** Replaces the original value associated with the specified key in this index
71      * with new value. If no value was associated with this key prior to this call
72      * StorageBadRequestException is thrown.
73      * @param key
74      * @param value
75      * @throws StorageException
76      * @throws StorageBadRequestException if the index has no entry with the given
77      * key
78      */

79     public synchronized void replace(Object JavaDoc key,Object JavaDoc value) throws StorageException, StorageBadRequestException {
80         Object JavaDoc original = table.put (key, value);
81         if (original == null) {
82             table.remove(key);
83             throw new StorageBadRequestException ("Cannot replace item that does not exist in the index.");
84         }
85         Object JavaDoc valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original);
86         transLog.logReplace(key, valueLog);
87     }
88     
89     public synchronized void willChange (Object JavaDoc key) throws StorageException {
90         if (!transLog.isLogged(key)) {
91             Object JavaDoc value = table.get (key);
92             transLog.logValue(key, createValueLog ((Streamable) value));
93         }
94         transLog.setDirty(key);
95     }
96     
97     public void changed (Object JavaDoc key) {
98         // do nothing
99
}
100     
101     private PrimaryValueLog createValueLog (Streamable value) throws StorageException {
102         baoStrm.reset();
103         try {
104             ((Streamable) value).write(daoStrm);
105         } catch (RuntimeException JavaDoc e) {
106             // ignore
107
}
108         return new PrimaryValueLog (baoStrm.toByteArray(), value);
109     }
110
111     /* -------------------------------------------------------------------- */
112     /* -- Implementation of org.netbeans.mdr.persistence.Streamable ------- */
113     /* -------------------------------------------------------------------- */
114
115     /** This method will be used to move changed object from storage cache
116      * to the persistent part of storage. It writes the object`s state
117      * (set of attributes) in the stream as an array of bytes, for example
118      * in textual representation.
119      * @param outputStream OutputStream that holds value of a Streamable object
120      */

121     public void write(java.io.OutputStream JavaDoc out) throws StorageException {
122         try {
123             IOUtils.writeInt(out, table.size());
124             for (Iterator it = table.entrySet().iterator(); it.hasNext();) {
125                 Map.Entry entry = (Map.Entry) it.next();
126                 storage.writeMOFID(out, (MOFID) entry.getKey());
127                 Streamable value = (Streamable) entry.getValue();
128                 IOUtils.writeString(out, value.getClass().getName());
129                 value.write(out);
130             }
131         } catch (java.io.IOException JavaDoc e) {
132             throw new StorageIOException(e);
133         }
134     }
135     /** Restore state of the Storable object from the stream.
136      * @param inputStream InputStream that represents an internal representation of fields of a Streamable object
137      * in which it was written by {@link write } method
138      */

139     public void read(java.io.InputStream JavaDoc is) throws StorageException {
140         try {
141             int size = IOUtils.readInt(is);
142             table = new HashMap(size * 4 / 3);
143             for (int i = 0; i < size; i++) {
144                 MOFID key = storage.readMOFID(is);
145                 Streamable value = (Streamable) Class.forName(IOUtils.readString(is)).newInstance();
146                 if (value instanceof StorageClient) {
147                     ((StorageClient) value).setStorage(storage);
148                 }
149                 value.read(is);
150                 table.put(key, value);
151             }
152         } catch (java.io.IOException JavaDoc e) {
153             throw new StorageIOException(e);
154         } catch (Exception JavaDoc e) {
155             throw (StorageException) Logger.getDefault().annotate(new StoragePersistentDataException(), e);
156         }
157     }
158
159     // inner class ..............................................................
160

161     private class PrimaryValueLog implements TransactionLog.ValueLog {
162
163         private byte [] bytes;
164         private Streamable obj;
165
166         PrimaryValueLog (byte [] bytes, Streamable obj) {
167             this.bytes = bytes;
168             this.obj = obj;
169         }
170
171         public Object JavaDoc resolveOriginalValue () throws StorageException {
172             ByteArrayInputStream baiStrm = new ByteArrayInputStream (bytes);
173             DataInputStream daiStrm = new DataInputStream (baiStrm);
174             if (obj instanceof StorageClient) {
175                 ((StorageClient) obj).setStorage(storage);
176             }
177             obj.read(daiStrm);
178             return obj;
179         }
180     }
181 }
182
Popular Tags