1 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 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 42 public synchronized boolean remove(Object key) throws StorageException { 43 Object original = table.remove(key); 44 if (original != null) { 45 Object valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original); 46 transLog.logRemove(key, valueLog); 47 return true; 48 } else 49 return false; 50 } 51 52 59 public synchronized boolean put(Object key,Object value) throws StorageException { 60 Object original = table.put(key, value); 61 if (original == null) { 62 transLog.logAdd(key); 63 } else { 64 Object valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original); 65 transLog.logReplace(key, valueLog); 66 } 67 return original != null; 68 } 69 70 79 public synchronized void replace(Object key,Object value) throws StorageException, StorageBadRequestException { 80 Object 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 valueLog = transLog.isLogged(key) ? null : createValueLog((Streamable) original); 86 transLog.logReplace(key, valueLog); 87 } 88 89 public synchronized void willChange (Object key) throws StorageException { 90 if (!transLog.isLogged(key)) { 91 Object value = table.get (key); 92 transLog.logValue(key, createValueLog ((Streamable) value)); 93 } 94 transLog.setDirty(key); 95 } 96 97 public void changed (Object key) { 98 } 100 101 private PrimaryValueLog createValueLog (Streamable value) throws StorageException { 102 baoStrm.reset(); 103 try { 104 ((Streamable) value).write(daoStrm); 105 } catch (RuntimeException e) { 106 } 108 return new PrimaryValueLog (baoStrm.toByteArray(), value); 109 } 110 111 112 113 114 115 121 public void write(java.io.OutputStream 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 e) { 132 throw new StorageIOException(e); 133 } 134 } 135 139 public void read(java.io.InputStream 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 e) { 153 throw new StorageIOException(e); 154 } catch (Exception e) { 155 throw (StorageException) Logger.getDefault().annotate(new StoragePersistentDataException(), e); 156 } 157 } 158 159 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 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 |