1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.io.IOException ; 11 import java.io.ObjectOutputStream ; 12 import java.util.LinkedHashMap ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 import org.ozoneDB.ObjectNotFoundException; 16 import org.ozoneDB.OzoneInternalException; 17 import org.ozoneDB.OzoneObjectException; 18 import org.ozoneDB.OzoneRemoteException; 19 import org.ozoneDB.TransactionException; 20 import org.ozoneDB.core.Env; 21 import org.ozoneDB.core.Lock; 22 import org.ozoneDB.core.ObjectContainer; 23 import org.ozoneDB.core.ObjectID; 24 import org.ozoneDB.core.PermissionError; 25 import org.ozoneDB.core.Transaction; 26 import org.ozoneDB.core.TransactionError; 27 import org.ozoneDB.core.User; 28 import org.ozoneDB.core.storage.Cache; 29 import org.ozoneDB.core.storage.FixedSizeCache; 30 import org.ozoneDB.core.storage.WeakReferenceCache; 31 import org.ozoneDB.util.LogWriter; 32 33 37 public class GammaTransaction extends Transaction { 38 39 private static final int FIXME_MAX_OBJECT_IDS = 250; 41 42 private static final float FIXME_LOADFACTOR_OBJECT_IDS = 0.75F; 43 44 private static final String LOGPOSTFIX = ".txlog"; 45 46 private GammaStore gammaStore; 47 48 private Storage logStorage; 49 50 58 private Cache containerCache = new FixedSizeCache(new Properties (), "") { 59 60 public Object remove(Object key) { 61 GammaContainer result = (GammaContainer) super.remove(key); 62 return result; 68 } 69 70 }; 71 72 79 private LinkedHashMap changedObjectIds; 80 81 public GammaTransaction(GammaStore gammaStore, StorageFactory logStorageFactory, Env env, User owner) { 82 super(env, owner); 83 try { 84 setGammaStore(gammaStore); 85 setLogStorage(logStorageFactory.createStorage(logFilename())); 86 } catch (IOException e) { 87 throw new OzoneInternalException("could not create transaction", e); 88 } 89 changedObjectIds = new LinkedHashMap (FIXME_MAX_OBJECT_IDS, FIXME_LOADFACTOR_OBJECT_IDS, true) { 90 public boolean removeEldestEntry(Map.Entry eldest) { 91 92 boolean result = size() >= FIXME_MAX_OBJECT_IDS - 1; 94 if (result) { 95 ObjectID objectID = (ObjectID) eldest.getValue(); 96 try { 97 getLogStorage().writeLong(objectID.value()); 98 } catch (IOException e) { 99 throw new OzoneInternalException("could not write object id", e); 100 } 101 } 102 return result; 103 } 104 }; 105 } 106 107 String logFilename() { 108 String result = Long.toString(taID().value()) + LOGPOSTFIX; 109 return result; 110 } 111 112 116 protected ObjectContainer acquireContainer(ObjectContainer container, int lockLevel) throws PermissionError, TransactionException, TransactionError, IOException , ObjectNotFoundException, ClassNotFoundException { 117 118 if (stopped == true) { 119 throw new TransactionException("Stopped.", TransactionException.STOPPED); 120 } 121 122 maxLockLevel = lockLevel > maxLockLevel ? lockLevel : maxLockLevel; 123 124 acquireCount++; 125 126 131 blocker = container.id(); 137 138 141 if (container.lock() == null) { 145 throw new IllegalStateException ("Container was wrongly deactivated. Increasing heap memory of the JVM may help."); 146 } 147 148 int prevLevel = container.lock().tryAcquire(this, lockLevel); 151 152 if (prevLevel == Lock.NOT_ACQUIRED) { 153 synchronized (this) { 154 while (prevLevel == Lock.NOT_ACQUIRED) { 155 try { 156 if (env.logWriter.hasTarget(LogWriter.DEBUG2)) { 157 env.logWriter.newEntry(this, toString() + " blocked by lock " + container.lock() + "...", LogWriter.DEBUG2); 158 } 159 160 wait(); 161 162 if (env.logWriter.hasTarget(LogWriter.DEBUG2)) { 163 env.logWriter.newEntry(this, toString() + " checking lock again...", LogWriter.DEBUG2); 164 } 165 } catch (Exception e) { 166 } 168 169 184 185 ObjectContainer newContainer = env.storeManager.containerForID(this, blocker); 188 189 if (container == null) { 190 throw new ObjectNotFoundException("No such object."); 191 } 192 193 container.lock().checkDeadlock(this); 195 196 prevLevel = container.lock().tryAcquire(this, lockLevel); 197 } 198 } 199 } 200 201 if (false) { 202 env.logWriter.newEntry(this, toString() + ".acquireContainer(" + blocker + "): successful.", LogWriter.DEBUG); 203 } 204 205 blocker = null; 207 208 209 if (prevLevel < lockLevel) { 211 if (owner == null) { 212 throw new PermissionError("No such user."); 213 } 214 if (!env.userManager.checkPermission(owner, container, lockLevel)) { 215 throw new PermissionError("User does not have proper access rights."); 216 } 217 218 env.storeManager.updateLockLevel(this, container); 219 } 220 container.touch(); 221 222 GammaContainer gammaContainer = (GammaContainer) container; 223 getContainerCache().put(gammaContainer.getObjectId(), gammaContainer); 224 225 return container; 226 } 227 228 private GammaStore getGammaStore() { 230 return gammaStore; 231 } 232 233 private void setGammaStore(GammaStore gammaStore) { 235 this.gammaStore = gammaStore; 236 } 237 238 Cache getContainerCache() { 239 return containerCache; 240 } 241 242 public ObjectContainer createObject(String className, int access, String name, String sig, Object [] args, ObjectID id) throws Exception , OzoneObjectException { 243 GammaContainer result; 244 245 result = (GammaContainer) super.createObject(className, access, name, sig, args, id); 246 getContainerCache().put(result.getObjectId(), result); 247 return result; 248 } 249 250 257 public Object invokeObject(ObjectID id, String methodName, String sig, Object [] args, int lockLevel) throws Exception , OzoneObjectException { 258 Object retValue; 259 260 retValue = super.invokeObject(id, methodName, sig, args, lockLevel); 261 return retValue; 262 } 263 264 public Object invokeObject(ObjectID id, int methodIndex, Object [] args, int lockLevel) throws Exception , OzoneObjectException { 265 Object retValue; 266 267 retValue = super.invokeObject(id, methodIndex, args, lockLevel); 268 return retValue; 269 } 270 271 public void nameObject(ObjectID id, String name) throws Exception { 272 super.nameObject(id, name); 273 } 274 275 public void deleteObject(ObjectID id) throws ObjectNotFoundException, IOException , ClassNotFoundException , TransactionException, TransactionError, OzoneRemoteException, OzoneInternalException, OzoneObjectException { 276 super.deleteObject(id); 277 } 278 279 Storage getLogStorage() { 280 return logStorage; 281 } 282 283 private void setLogStorage(Storage logStorage) { 284 this.logStorage = logStorage; 285 } 286 287 } 288 | Popular Tags |