From 4c016bce45303774296700f1863f0aa5f1c6c201 Mon Sep 17 00:00:00 2001 From: rk Date: Tue, 19 Feb 2002 20:12:05 +0000 Subject: [PATCH] release / invalidate --- source/mir/storage/store/ObjectStore.java | 31 +++++++++++++++----- source/mir/storage/store/StorableObject.java | 8 ++++++ source/mir/storage/store/StoreContainer.java | 41 +++++++++++++++++++++++---- source/mir/storage/store/StoreIdentifier.java | 23 ++++++++++++--- 4 files changed, 86 insertions(+), 17 deletions(-) diff --git a/source/mir/storage/store/ObjectStore.java b/source/mir/storage/store/ObjectStore.java index 52f52bf7..86f2c4a7 100755 --- a/source/mir/storage/store/ObjectStore.java +++ b/source/mir/storage/store/ObjectStore.java @@ -46,11 +46,6 @@ public class ObjectStore { public static ObjectStore getInstance() { return INSTANCE; } - public boolean has(StoreIdentifier sid) { - StoreContainer stoc = getStoreContainerForSid( sid ); - return ( stoc != null && stoc.has(sid) ) ? true:false; - } - /** * Method: use * Description: The ObjectStore tries to find the @see StoreIdentifier sid @@ -112,6 +107,23 @@ public class ObjectStore { return sb.toString(); } + /** + * Method: invalidate(StorableObject sto) + * Description: ObjectStore is notified of change of a @see StorableObject + * sto and invalidates all relevant cache entries. + */ + + public void invalidate(StorableObject sto) { + // propagate invalidation to StoreContainer + if (sto!=null) { + StoreIdentifier sid = sto.getStoreIdentifier(); + if (sto!=null) { + StoreContainer stoc = getStoreContainerForSid(sid); + stoc.invalidate(sto); + } + } + } + // internal methods for StoreContainer managment /** @@ -139,7 +151,6 @@ public class ObjectStore { * @return true if yes, otherwise no. */ private final static boolean implementsStorableObject(Class aClass) { - boolean yesno=false; if (aClass!=null) { Class[] interfaces = aClass.getInterfaces(); if (interfaces.length>0) { @@ -148,10 +159,16 @@ public class ObjectStore { } } } + return false; + } - return yesno; + + private boolean has(StoreIdentifier sid) { + StoreContainer stoc = getStoreContainerForSid( sid ); + return ( stoc != null && stoc.has(sid) ) ? true:false; } + /** * Method: version() * Description: returns ObjectStore version as String diff --git a/source/mir/storage/store/StorableObject.java b/source/mir/storage/store/StorableObject.java index de6f4a5c..24516f47 100755 --- a/source/mir/storage/store/StorableObject.java +++ b/source/mir/storage/store/StorableObject.java @@ -24,6 +24,14 @@ public interface StorableObject { * */ abstract StoreIdentifier getStoreIdentifier(); + + /** + * Method: notifyOnReleaseSet() + * Description: Contains a Set of @see StoreIdentifier which are invalidated + * on update/insert/delete of this StorableObject. + * + * @return Set of StoreIdentifier. + */ abstract Set notifyOnReleaseSet(); } \ No newline at end of file diff --git a/source/mir/storage/store/StoreContainer.java b/source/mir/storage/store/StoreContainer.java index 2d700af1..897cf78b 100755 --- a/source/mir/storage/store/StoreContainer.java +++ b/source/mir/storage/store/StoreContainer.java @@ -20,16 +20,19 @@ import mir.misc.Logfile; public class StoreContainer { - private final static int DEFAULT_SIZE=10; + private final static int DEFAULT_SIZE=10; + private static Logfile storeLog; + private static int uniqueCounter=10000; private LinkedList container; private StoreContainerType stocType; private int maxSize=DEFAULT_SIZE; - private static Logfile storeLog; + private int uniqueId; private StoreContainer() {}; public StoreContainer(StoreContainerType stoc_type) { + this.uniqueId=++uniqueCounter; this.stocType=stoc_type; this.container=new LinkedList(); } @@ -39,8 +42,6 @@ public class StoreContainer { this.maxSize=maxSize; } - /** @todo methods: release+propagation */ - public StorableObject use(StoreIdentifier sid) { // find sid in LinkedList or die // move sid to head of linked list @@ -58,12 +59,38 @@ public class StoreContainer { } /** + * Method: invalidate(StorableObject sto) + * Description: finds @see StorableObject, propagates invalidation to + * @see StoreIdentifier and removes StoreIdentifier from + * list. + */ + public void invalidate(StorableObject sto) { + if (sto!=null) { + StoreIdentifier sid = sto.getStoreIdentifier(); + if (sid!=null) { + if ( container.contains(sid) ) { + sid.invalidate(); + container.remove(sid); + } + } + } + } + + /** * Method: setSize * Description: readjusts StoreContainer size to value. * */ public void setSize(int size) { - /** @todo check size, if size too big, shrink */ + if (size <0) return; + if ( size container.size() ) { + // shrink + while (size > container.size() ) { + StoreIdentifier sid = (StoreIdentifier)container.getLast(); + sid.release(); + container.remove(sid); + } + } this.maxSize=size; } @@ -75,10 +102,12 @@ public class StoreContainer { * @return String */ public String toString() { - StringBuffer sb = new StringBuffer("StoreContainer for "); + StringBuffer sb = new StringBuffer("StoreContainer id: "); + sb.append(uniqueId).append(" for "); sb.append(stocType.toString()).append("\nCurrent size: "); sb.append(container.size()).append("\nMaximum size:"); sb.append(maxSize).append("\n"); + /** @todo list members ? */ return sb.toString(); } diff --git a/source/mir/storage/store/StoreIdentifier.java b/source/mir/storage/store/StoreIdentifier.java index 5ecf3408..f10179b3 100755 --- a/source/mir/storage/store/StoreIdentifier.java +++ b/source/mir/storage/store/StoreIdentifier.java @@ -13,7 +13,8 @@ import mir.misc.Logfile; public class StoreIdentifier { - /** @todo move main stuff to storecontainertype */ + /** @todo check if invalidating already to avoid deadlocks + * what about concurrency? */ private static Logfile storeLog; @@ -21,6 +22,7 @@ public class StoreIdentifier { private StorableObject reference=null; private String uniqueIdentifier=null; // id for Entity & sql for EntityList private long timesUsed; + private boolean invalidating=false; /** @todo initialize logfile */ @@ -32,11 +34,24 @@ public class StoreIdentifier { this.stocType = StoreContainerType.valueOf(reference.getClass(), storeType); } + /** + * Method: ivalidate + * Description: + * + * @return + */ public void invalidate() { - Set set = reference.notifyOnReleaseSet(); - /** @todo here we should propagate the invalidation all members of Set - * @see StoreContainer */ + // avoid deadlock due to propagation. + if (!invalidating) { + invalidating=true; + Set set = reference.notifyOnReleaseSet(); + /** @todo here we should propagate the invalidation all members of Set + * @see StoreContainer. The set may contain objects of different type.*/ + release(); + } + } + public void release() { this.reference=null; this.uniqueIdentifier=null; this.stocType=null; -- 2.11.0