From da731d2bb81666b9c697d9099da632e7dfcdc0f7 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 9 Sep 2014 12:13:21 +0200 Subject: Replaced int sysKey Item identifier with UUID, which is now portable. ItemPath objects are now used to identify Items throughout the kernel, replacing ints and Integers. --- .../c2kernel/persistency/TransactionManager.java | 116 ++++++++++----------- 1 file changed, 58 insertions(+), 58 deletions(-) (limited to 'src/main/java/com/c2kernel/persistency/TransactionManager.java') diff --git a/src/main/java/com/c2kernel/persistency/TransactionManager.java b/src/main/java/com/c2kernel/persistency/TransactionManager.java index 7362ae1..b28a440 100644 --- a/src/main/java/com/c2kernel/persistency/TransactionManager.java +++ b/src/main/java/com/c2kernel/persistency/TransactionManager.java @@ -7,18 +7,19 @@ import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.entity.agent.JobList; import com.c2kernel.events.History; +import com.c2kernel.lookup.ItemPath; import com.c2kernel.process.auth.Authenticator; import com.c2kernel.utils.Logger; public class TransactionManager { - HashMap locks; + HashMap locks; HashMap> pendingTransactions; ClusterStorageManager storage; public TransactionManager(Authenticator auth) throws ClusterStorageException { storage = new ClusterStorageManager(auth); - locks = new HashMap(); + locks = new HashMap(); pendingTransactions = new HashMap>(); } @@ -40,16 +41,16 @@ public class TransactionManager { storage.close(); } - public String[] getClusterContents(int sysKey, String path) throws ClusterStorageException { + public String[] getClusterContents(ItemPath itemPath, String path) throws ClusterStorageException { if (path.startsWith("/") && path.length() > 1) path = path.substring(1); - return storage.getClusterContents(new Integer(sysKey), path); + return storage.getClusterContents(itemPath, path); } /** * Public get method. Required a 'locker' object for a transaction key. * Checks the transaction table first to see if the caller has uncommitted changes */ - public C2KLocalObject get(int sysKey, String path, Object locker) + public C2KLocalObject get(ItemPath itemPath, String path, Object locker) throws ClusterStorageException, ObjectNotFoundException { if (path.startsWith("/") && path.length() > 1) path = path.substring(1); @@ -57,56 +58,54 @@ public class TransactionManager { // deal out top level remote maps, if transactions aren't needed if (path.indexOf('/') == -1) { if (path.equals(ClusterStorage.HISTORY) && locker != null) - return new History(sysKey, locker); + return new History(itemPath, locker); if (path.equals(ClusterStorage.JOB) && locker != null) - return new JobList(sysKey, locker); + return new JobList(itemPath, locker); } - Integer sysKeyIntObj = new Integer(sysKey); // check to see if the locker has been modifying this cluster - if (locks.containsKey(sysKeyIntObj) && locks.get(sysKeyIntObj).equals(locker)) { + if (locks.containsKey(itemPath) && locks.get(itemPath).equals(locker)) { ArrayList lockerTransaction = pendingTransactions.get(locker); for (TransactionEntry thisEntry : lockerTransaction) { - if (sysKey == thisEntry.sysKey.intValue() && path.equals(thisEntry.getPath())) { + if (itemPath.equals(thisEntry.itemPath) && path.equals(thisEntry.path)) { if (thisEntry.obj == null) - throw new ClusterStorageException("ClusterStorageManager.get() - Cluster " + path + " has been deleted in " + sysKey + + throw new ClusterStorageException("ClusterStorageManager.get() - Cluster " + path + " has been deleted in " + itemPath + " but not yet committed"); return thisEntry.obj; } } } - return storage.get(sysKeyIntObj, path); + return storage.get(itemPath, path); } /** * Public put method. Manages the transaction table keyed by the object 'locker'. * If this object is null, transaction support is bypassed (so long as no lock exists on that object). */ - public void put(int sysKey, C2KLocalObject obj, Object locker) throws ClusterStorageException { - Integer sysKeyIntObj = new Integer(sysKey); + public void put(ItemPath itemPath, C2KLocalObject obj, Object locker) throws ClusterStorageException { Object tempLocker = null; ArrayList lockerTransaction; String path = ClusterStorage.getPath(obj); synchronized(locks) { // look to see if this object is already locked - if (locks.containsKey(sysKeyIntObj)) { + if (locks.containsKey(itemPath)) { // if it's this locker, get the transaction list - Object thisLocker = locks.get(sysKeyIntObj); + Object thisLocker = locks.get(itemPath); if (thisLocker.equals(locker)) // retrieve the transaction list lockerTransaction = pendingTransactions.get(locker); else // locked by someone else - throw new ClusterStorageException("ClusterStorageManager.get() - Access denied: Object " + sysKeyIntObj + + throw new ClusterStorageException("ClusterStorageManager.get() - Access denied: Object " + itemPath + " has been locked for writing by " + thisLocker); } else { // no locks for this item if (locker == null) { // lock the item until the non-transactional put is complete :/ tempLocker = new Object(); - locks.put(sysKeyIntObj, tempLocker); + locks.put(itemPath, tempLocker); lockerTransaction = null; } else { // initialise the transaction - locks.put(sysKeyIntObj, locker); + locks.put(itemPath, locker); lockerTransaction = new ArrayList(); pendingTransactions.put(locker, lockerTransaction); } @@ -114,13 +113,13 @@ public class TransactionManager { } if (tempLocker != null) { // non-locking put/delete - storage.put(sysKeyIntObj, obj); - locks.remove(sysKeyIntObj); + storage.put(itemPath, obj); + locks.remove(itemPath); return; } // create the new entry in the transaction table - TransactionEntry newEntry = new TransactionEntry(sysKeyIntObj, path, obj); + TransactionEntry newEntry = new TransactionEntry(itemPath, obj); /* equals() in TransactionEntry only compares sysKey and path, so we can use * contains() in ArrayList to looks for preexisting entries for this cluster * and overwrite them. @@ -132,29 +131,28 @@ public class TransactionManager { /** Public delete method. Uses the put method, with null as the object value. */ - public void remove(int sysKey, String path, Object locker) throws ClusterStorageException { - Integer sysKeyIntObj = new Integer(sysKey); + public void remove(ItemPath itemPath, String path, Object locker) throws ClusterStorageException { ArrayList lockerTransaction; Object tempLocker = null; synchronized(locks) { // look to see if this object is already locked - if (locks.containsKey(sysKeyIntObj)) { + if (locks.containsKey(itemPath)) { // if it's this locker, get the transaction list - Object thisLocker = locks.get(sysKeyIntObj); + Object thisLocker = locks.get(itemPath); if (thisLocker.equals(locker)) // retrieve the transaction list lockerTransaction = pendingTransactions.get(locker); else // locked by someone else - throw new ClusterStorageException("ClusterStorageManager.get() - Access denied: Object " + sysKeyIntObj + + throw new ClusterStorageException("ClusterStorageManager.get() - Access denied: Object " + itemPath + " has been locked for writing by " + thisLocker); } else { // either we are the locker, or there is no locker if (locker == null) { // non-locking put/delete tempLocker = new Object(); - locks.put(sysKeyIntObj, tempLocker); + locks.put(itemPath, tempLocker); lockerTransaction = null; } else {// initialise the transaction - locks.put(sysKeyIntObj, locker); + locks.put(itemPath, locker); lockerTransaction = new ArrayList(); pendingTransactions.put(locker, lockerTransaction); } @@ -162,13 +160,13 @@ public class TransactionManager { } if (tempLocker != null) { - storage.remove(sysKeyIntObj, path); - locks.remove(sysKeyIntObj); + storage.remove(itemPath, path); + locks.remove(itemPath); return; } // create the new entry in the transaction table - TransactionEntry newEntry = new TransactionEntry(sysKeyIntObj, path, null); + TransactionEntry newEntry = new TransactionEntry(itemPath, path); /* equals() in TransactionEntry only compares sysKey and path, so we can use * contains() in ArrayList to looks for preexisting entries for this cluster * and overwrite them. @@ -188,13 +186,13 @@ public class TransactionManager { * * @throws ClusterStorageException - when deleting fails */ - public void removeCluster(int sysKey, String path, Object locker) throws ClusterStorageException { + public void removeCluster(ItemPath itemPath, String path, Object locker) throws ClusterStorageException { - String[] children = getClusterContents(sysKey, path); + String[] children = getClusterContents(itemPath, path); for (String element : children) - removeCluster(sysKey, path+(path.length()>0?"/":"")+element, locker); + removeCluster(itemPath, path+(path.length()>0?"/":"")+element, locker); if (children.length==0 && path.indexOf("/") > -1) - remove(sysKey, path, locker); + remove(itemPath, path, locker); } /** @@ -209,10 +207,10 @@ public class TransactionManager { for (TransactionEntry thisEntry : lockerTransactions) { try { if (thisEntry.obj == null) - storage.remove(thisEntry.sysKey, thisEntry.path); + storage.remove(thisEntry.itemPath, thisEntry.path); else - storage.put(thisEntry.sysKey, thisEntry.obj); - locks.remove(thisEntry.sysKey); + storage.put(thisEntry.itemPath, thisEntry.obj); + locks.remove(thisEntry.itemPath); } catch (Exception e) { exceptions.put(thisEntry, e); } @@ -238,22 +236,22 @@ public class TransactionManager { public void abort(Object locker) { synchronized(locks) { if (locks.containsValue(locker)) { - for (Integer thisKey : locks.keySet()) { - if (locks.get(thisKey).equals(locker)) - locks.remove(thisKey); + for (ItemPath thisPath : locks.keySet()) { + if (locks.get(thisPath).equals(locker)) + locks.remove(thisPath); } } pendingTransactions.remove(locker); } } - public void clearCache(int sysKey, String path) { - if (sysKey == -1) + public void clearCache(ItemPath itemPath, String path) { + if (itemPath == null) storage.clearCache(); else if (path == null) - storage.clearCache(new Integer(sysKey)); + storage.clearCache(itemPath); else - storage.clearCache(new Integer(sysKey), path); + storage.clearCache(itemPath, path); } @@ -264,9 +262,9 @@ public class TransactionManager { if (locks.size() == 0) Logger.msg(logLevel, " None"); else - for (Integer thisKey : locks.keySet()) { - Object locker = locks.get(thisKey); - Logger.msg(logLevel, " "+thisKey+" locked by "+locker); + for (ItemPath thisPath : locks.keySet()) { + Object locker = locks.get(thisPath); + Logger.msg(logLevel, " "+thisPath+" locked by "+locker); } Logger.msg(logLevel, "Open transactions:"); @@ -285,17 +283,19 @@ public class TransactionManager { /** Used in the transaction table to store details of a put until commit */ class TransactionEntry { - public Integer sysKey; + public ItemPath itemPath; public String path; public C2KLocalObject obj; - public TransactionEntry(Integer sysKey, String path, C2KLocalObject obj) { - this.sysKey = sysKey; - this.path = path; + public TransactionEntry(ItemPath itemPath, C2KLocalObject obj) { + this.itemPath = itemPath; + this.path = ClusterStorage.getPath(obj); this.obj = obj; } - - public String getPath() { - return ClusterStorage.getPath(obj); + + public TransactionEntry(ItemPath itemPath, String path) { + this.itemPath = itemPath; + this.path = path; + this.obj = null; } @Override @@ -305,7 +305,7 @@ public class TransactionManager { report.append("Delete"); else report.append("Put "+obj.getClass().getName()); - report.append(" at ").append(path).append(" in ").append(sysKey); + report.append(" at ").append(path).append(" in ").append(itemPath); return report.toString(); } @@ -314,7 +314,7 @@ public class TransactionManager { */ @Override public int hashCode() { - return sysKey.hashCode()*getPath().hashCode(); + return itemPath.hashCode()*path.hashCode(); } /** -- cgit v1.2.3