add & use is working with little testsuite. information about hits/misses added.
[mir.git] / source / mir / storage / store / StoreContainer.java
index 4713530..397179f 100755 (executable)
@@ -28,6 +28,8 @@ public class StoreContainer {
        private StoreContainerType  stocType;
        private int                 maxSize=DEFAULT_SIZE;
        private int                 uniqueId;
+  private int                 addCount=0,removeCount=0,storeOutCount;
+  private int                 hitCount=0,missCount=0;
 
        private StoreContainer() {};
 
@@ -43,25 +45,35 @@ public class StoreContainer {
        }
 
        public StorableObject use(StoreIdentifier sid) {
-               // find sid in LinkedList or die
-               // move sid to head of linked list
-               // return reference on object
-               return null;
+    int hit = container.indexOf(sid);
+    if (hit>0) {
+      StoreIdentifier hitSid = (StoreIdentifier)container.get(hit);
+                 if ( hitSid!=null ) {
+         hitCount++;
+         return hitSid.use();
+                 }
+    }
+    missCount++;
+    return null;
        }
 
        public boolean has(StoreIdentifier sid) {
-               return true; // yes yes
+               return container.contains(sid);
        }
 
        public void add(StoreIdentifier sid) {
                if ( sid != null && sid.hasReference() ) {
-                       //if ( has(sid) )
-                       //      moveToHead(sid);
-                       //else
+                       if ( has(sid) ) {
+                         moveToHead(sid);
+                         System.err.println("OBJECTStore: tried to add sid " + sid.toString() +
+                            " that was already in store.");
+      }
+                       else {
                                container.addFirst(sid);
+        shrinkIfNecessary();
+        addCount++;
+                       }
                }
-               // add to head of linkedlist, if size is exceded throw away tail until
-               // size ok.
        }
 
        /**
@@ -77,6 +89,7 @@ public class StoreContainer {
                                if ( container.contains(sid) ) {
                                        sid.invalidate();
                                        container.remove(sid);
+          removeCount++;
                                }
                        }
                }
@@ -93,21 +106,27 @@ public class StoreContainer {
                this.maxSize=size;
        }
 
-       private void shrink() {
-               shrinkToSize(maxSize);
-       }
+       private void shrinkIfNecessary() { shrinkToSize(maxSize); }
 
   private void shrinkToSize(int size) {
-    if ( size<maxSize && size < container.size() ) {
+    if ( size < container.size() ) {
                        // shrink
                        while (size < container.size() ) {
                                StoreIdentifier sid = (StoreIdentifier)container.getLast();
-                               sid.release();
                                container.remove(sid);
+        sid.release();
+        storeOutCount++;
                        }
                }
   }
 
+  private synchronized void moveToHead(StoreIdentifier sid) {
+    if ( sid!=null ) {
+      container.remove(sid);
+      container.addFirst(sid);
+    }
+  }
+
        /**
         *  Method:       toString()
         *  Description:  gives out statistical Information, viewable via
@@ -116,12 +135,21 @@ public class StoreContainer {
         *  @return       String
         */
        public String toString() {
+    float hitRatio=0;
+    long divisor=hitCount+missCount;
+    if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
+    hitRatio*=100;
+
                StringBuffer sb = new StringBuffer("StoreContainer id: ");
                sb.append(uniqueId).append(" for ");
-               sb.append(stocType.toString()).append(" // Current/Max size: ");
-               sb.append(container.size()).append(" / ");
-               sb.append(maxSize).append("\n");
-               /** @todo list members ? */
+               sb.append(stocType.toString()).append("\n  [current/maximum size: ");
+               sb.append(container.size()).append("/").append(maxSize);
+               sb.append("]\n  [added/stored out/removed: ").append(addCount).append("/");
+    sb.append(storeOutCount).append("/").append(removeCount).append("]\n  [hit/miss/ratio: ");
+    sb.append(hitCount).append("/").append(missCount).append("/");
+    sb.append(hitRatio).append("%]\n");
+
+    /** @todo list members ? */
                return sb.toString();
        }