release / invalidate
authorrk <rk>
Tue, 19 Feb 2002 20:12:05 +0000 (20:12 +0000)
committerrk <rk>
Tue, 19 Feb 2002 20:12:05 +0000 (20:12 +0000)
source/mir/storage/store/ObjectStore.java
source/mir/storage/store/StorableObject.java
source/mir/storage/store/StoreContainer.java
source/mir/storage/store/StoreIdentifier.java

index 52f52bf..86f2c4a 100755 (executable)
@@ -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
index de6f4a5..24516f4 100755 (executable)
@@ -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
index 2d700af..897cf78 100755 (executable)
@@ -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<maxSize && 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();
   }
 
index 5ecf340..f10179b 100755 (executable)
@@ -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;