1 19 20 package org.netbeans.mdr.storagemodel.transientimpl; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Properties ; 25 import java.util.Stack ; 26 import org.netbeans.mdr.persistence.*; 27 import org.netbeans.mdr.storagemodel.MdrStorage; 28 import org.netbeans.mdr.storagemodel.TransientStorableObject; 29 30 34 public class TransientStorage implements Storage { 35 36 public static final String STORAGE_ID = "GC"; 38 private MdrStorage mdrStorage; 39 private long sequenceNumber; 40 private String name; 41 private Stack indexTxLog; 42 private Stack attrTxLog; 43 44 private HashMap indexes; 45 46 47 public TransientStorage (String name) { 48 this.name = name; 49 this.sequenceNumber = 1; 50 this.indexes = new HashMap (); 51 this.indexTxLog = new Stack (); 52 this.attrTxLog = new Stack (); 53 } 54 55 public String getName() { 56 return this.name; 57 } 58 59 public String getStorageId () { 60 return STORAGE_ID; 61 } 62 63 public synchronized long getSerialNumber () { 64 return this.sequenceNumber++; 65 } 66 67 70 public MOFID readMOFID (java.io.InputStream inputStream) throws StorageException { 71 throw new UnsupportedOperationException (); 72 } 73 74 77 public void writeMOFID (java.io.OutputStream outputStream, MOFID modId) throws StorageException { 78 throw new UnsupportedOperationException (); 79 } 80 81 public MOFID resolveMOFID (String sMofId) { 82 if (sMofId == null) 83 return null; 84 if (!sMofId.startsWith (STORAGE_ID)) 85 return null; 86 try { 87 long serialNumber = Long.parseLong (sMofId.substring(STORAGE_ID.length()+1),16); 88 return new MOFID (serialNumber, STORAGE_ID); 89 }catch (NumberFormatException nfe) { 90 return null; 91 } 92 } 93 94 97 public synchronized SinglevaluedIndex getPrimaryIndex() throws StorageException { 98 SinglevaluedIndex primaryIndex = (SinglevaluedIndex) this.indexes.get (TransientObjectResolverIndex.NAME); 99 if ( primaryIndex == null) { 100 primaryIndex = new TransientObjectResolverIndex (); 101 this.indexes.put (TransientObjectResolverIndex.NAME, primaryIndex); 102 this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, primaryIndex)); 103 } 104 return primaryIndex; 105 } 106 107 108 public boolean exists() throws StorageException { 109 return true; 110 } 111 112 115 public synchronized boolean delete() throws StorageException { 116 for (Iterator it = this.indexes.keySet ().iterator (); it.hasNext(); ) { 117 this.dropIndex ((String ) it.next()); 118 } 119 return true; 120 } 121 122 131 public void create (boolean replace, ObjectResolver resolver) throws StorageException { 132 this.mdrStorage = (MdrStorage) resolver; 133 } 134 135 139 public void open(boolean createOnNoExist, ObjectResolver resolver) throws StorageException { 140 throw new UnsupportedOperationException (); 141 } 142 143 144 public void close() throws StorageException { 145 this.commitChanges (); 146 this.delete (); 147 } 148 149 155 public synchronized SinglevaluedIndex createSinglevaluedIndex(String name, EntryType keyType, EntryType valueType) throws StorageException { 156 if (this.indexes.get (name) != null) 157 throw new StorageBadRequestException ("Index already exists."); 158 TransientSinglevaluedIndex tsi = new TransientSinglevaluedIndex (this.mdrStorage, name, keyType, valueType); 159 this.indexes.put (name, tsi); 160 this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tsi)); 161 return tsi; 162 } 163 170 public synchronized MultivaluedOrderedIndex createMultivaluedOrderedIndex(String name, EntryType keyType, EntryType valueType, boolean unique) throws StorageException { 171 if (this.indexes.get (name) != null) 172 throw new StorageBadRequestException ("Index already exists."); 173 MultivaluedOrderedIndex tmoi = new TransientMultivaluedOrderedIndex (this.mdrStorage, name, keyType, valueType, unique); 174 this.indexes.put (name, tmoi); 175 this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tmoi)); 176 return tmoi; 177 } 178 186 public synchronized MultivaluedIndex createMultivaluedIndex(String name, EntryType keyType, EntryType valueType, boolean unique) throws StorageException { 187 if (this.indexes.get (name) != null) 188 throw new StorageBadRequestException ("Index already exists."); 189 MultivaluedIndex tmi = new TransientMultivaluedIndex (this.mdrStorage, name, keyType, valueType, unique); 190 this.indexes.put (name, tmi); 191 this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tmi)); 192 return tmi; 193 } 194 195 199 public synchronized Index getIndex(String name) throws StorageException { 200 return (Index) this.indexes.get (name); 201 } 202 206 public synchronized SinglevaluedIndex getSinglevaluedIndex(String name) throws StorageException { 207 return (SinglevaluedIndex) this.getIndex (name); 208 } 209 213 public synchronized MultivaluedIndex getMultivaluedIndex(String name) throws StorageException { 214 return (MultivaluedIndex) this.getIndex (name); 215 } 216 220 public synchronized MultivaluedOrderedIndex getMultivaluedOrderedIndex(String name) throws StorageException { 221 return (MultivaluedOrderedIndex) this.getIndex (name); 222 } 223 226 public synchronized void dropIndex(String name) throws StorageException { 227 Object index = this.indexes.remove (name); 228 this.indexTxLog.push ( new CompensatingTransaction.DropIndexCTx (name, index)); 229 } 230 231 public void objectStateWillChange (Object key) throws StorageException { 232 } 234 235 240 public void objectStateChanged (Object key) throws StorageException { 241 this.attrTxLog.push (key); 242 } 243 244 248 public void commitChanges() throws StorageException { 249 this.indexTxLog.clear (); 250 Iterator it = null; 251 synchronized (this) { 252 it = ((HashMap )this.indexes.clone ()).values ().iterator (); 253 } 254 while (it.hasNext ()) { 255 ((TransactionalIndex)it.next()).commit (); 256 } 257 while (!this.attrTxLog.empty ()) { 258 MOFID id = (MOFID) this.attrTxLog.pop (); 259 SinglevaluedIndex pi = this.getPrimaryIndex (); 260 TransientStorableObject tso = (TransientStorableObject) pi.getIfExists(id); 261 if (tso != null) 262 tso.commit (); 263 } 264 } 265 266 273 public void rollBackChanges () throws StorageException { 274 while (!this.indexTxLog.empty ()) { 275 CompensatingTransaction ctx = (CompensatingTransaction) this.indexTxLog.pop (); 276 ctx.perform (this.indexes); 277 } 278 Iterator it = null; 279 synchronized (this) { 280 it = ((HashMap )this.indexes.clone ()).values ().iterator (); 281 } 282 while (it.hasNext ()) { 283 ((TransactionalIndex)it.next ()).rollBack (); 284 } 285 while (!this.attrTxLog.empty ()) { 286 MOFID id = (MOFID) this.attrTxLog.pop (); 287 SinglevaluedIndex pi = this.getPrimaryIndex (); 288 TransientStorableObject tso = (TransientStorableObject) pi.getIfExists(id); 289 if (tso != null) 290 tso.rollBack (); 291 } 292 } 293 294 297 public void shutDown() throws StorageException { 298 } 299 300 301 302 } 303 | Popular Tags |