From: rk Date: Fri, 22 Feb 2002 18:48:06 +0000 (+0000) Subject: add & use is working with little testsuite. information about hits/misses added. X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=6fb36d4e86dce331db03a398c8a995ee93e348d0;p=mir.git add & use is working with little testsuite. information about hits/misses added. --- diff --git a/source/mir/storage/store/ObjectStore.java b/source/mir/storage/store/ObjectStore.java index 844ad1a6..5c554828 100755 --- a/source/mir/storage/store/ObjectStore.java +++ b/source/mir/storage/store/ObjectStore.java @@ -35,11 +35,11 @@ import mir.misc.Logfile; public class ObjectStore { - private static ObjectStore INSTANCE=new ObjectStore(); - private static HashMap containerMap=new HashMap(); // StoreContainerType/StoreContainer - private static Logfile storeLog; - private static long storeHit=0,storeMiss=0; - private static Class storableObjectInterface=StorableObject.class; + private final static ObjectStore INSTANCE=new ObjectStore(); + private final static HashMap containerMap=new HashMap(); // StoreContainerType/StoreContainer + private final static Class storableObjectInterface=StorableObject.class; + private static Logfile storeLog; + private static long storeHit=0,storeMiss=0; private ObjectStore() { } @@ -58,6 +58,7 @@ public class ObjectStore { StorableObject storeObject=null; StoreContainer stoc = getStoreContainerForSid( sid ); if (stoc!=null) storeObject=stoc.use(sid); + else System.out.println("Warning: container not found for: " + sid.toString()); if (storeObject==null) storeMiss++; else storeHit++; return storeObject; } @@ -90,11 +91,16 @@ public class ObjectStore { */ public String toString() { - StringBuffer sb = new StringBuffer("Mir-ObjectStore v_"); + float hitRatio=0; + long divisor=storeHit+storeMiss; + if (divisor>0) hitRatio=(float)storeHit/(float)divisor; + hitRatio*=100; + + StringBuffer sb = new StringBuffer("Mir-ObjectStore v_"); sb.append(version()).append("\n"); - sb.append("\nObjectStore hits/misses: ").append(storeHit); - sb.append("/").append(storeMiss); - sb.append("\nCurrently ").append(containerMap.size()); + sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit); + sb.append("/").append(storeMiss).append("/").append(hitRatio); + sb.append("%\nCurrently ").append(containerMap.size()); sb.append(" StoreContainer in use - listing information:\n"); // ask container for information @@ -175,5 +181,5 @@ public class ObjectStore { * * @return String */ - private String version() { return "00_daythree";} + private String version() { return "00.d4";} } \ No newline at end of file diff --git a/source/mir/storage/store/StorableObject.java b/source/mir/storage/store/StorableObject.java index 24516f47..1d38a707 100755 --- a/source/mir/storage/store/StorableObject.java +++ b/source/mir/storage/store/StorableObject.java @@ -32,6 +32,6 @@ public interface StorableObject { * * @return Set of StoreIdentifier. */ - abstract Set notifyOnReleaseSet(); + abstract Set getNotifyOnReleaseSet(); } \ No newline at end of file diff --git a/source/mir/storage/store/StoreContainer.java b/source/mir/storage/store/StoreContainer.java index 47135308..397179f8 100755 --- a/source/mir/storage/store/StoreContainer.java +++ b/source/mir/storage/store/StoreContainer.java @@ -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 ( size0) 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(); } diff --git a/source/mir/storage/store/StoreContainerType.java b/source/mir/storage/store/StoreContainerType.java index a2d3aeb1..f1535bc3 100755 --- a/source/mir/storage/store/StoreContainerType.java +++ b/source/mir/storage/store/StoreContainerType.java @@ -20,15 +20,22 @@ import mir.misc.Logfile; public class StoreContainerType { - public final static int STOC_TYPE_UNKNOWN=0; - public final static int STOC_TYPE_ENTITY=1; - public final static int STOC_TYPE_ENTITYLIST=2; + public final static int STOC_TYPE_UNKNOWN=-1; + public final static int STOC_TYPE_ENTITY=0; + public final static int STOC_TYPE_ENTITYLIST=1; + public final static int STOC_TYPE_MAX=STOC_TYPE_ENTITYLIST; - private static HashMap uniqueTypes = new HashMap(); // StoreKey / StoreContainerType + private static HashMap[] uniqueTypes=new HashMap[STOC_TYPE_MAX+1]; private static Logfile storeLog; private Class stocClass=null; private int stocType=STOC_TYPE_UNKNOWN; + static { + uniqueTypes[STOC_TYPE_ENTITY]= new HashMap(); + uniqueTypes[STOC_TYPE_ENTITYLIST]=new HashMap(); + + } + private StoreContainerType() {} private StoreContainerType(Class stocClass, int stocType) { @@ -37,37 +44,22 @@ public class StoreContainerType { } public static StoreContainerType valueOf(Class stoc_class, int stoc_type) { - /** @todo factory, only gives out types once to make them comparable via == - * check if class is StorableObject */ StoreContainerType returnStocType=null; - - // inner class for hashlookup - class StoreKey { - int storeType; Class storeClass; - public boolean equals(Object o) { - if ( o instanceof StoreKey && - ((StoreKey)o).storeType==storeType && - ((StoreKey)o).storeClass==storeClass ) - return true; - return false; - } - } - - StoreKey key = new StoreKey(); - key.storeClass = stoc_class; key.storeType = stoc_type; - if ( uniqueTypes.containsKey(key) ) - returnStocType=(StoreContainerType)uniqueTypes.get(key); - else { - returnStocType=new StoreContainerType(stoc_class,stoc_type); - uniqueTypes.put(key,returnStocType); - } + if (stoc_type>=0 && stoc_type < STOC_TYPE_MAX) { + HashMap current = uniqueTypes[stoc_type]; + if ( current.containsKey(stoc_class) ) + returnStocType=(StoreContainerType)current.get(stoc_class); + else { + returnStocType=new StoreContainerType(stoc_class,stoc_type); + current.put(stoc_class,returnStocType); + } + } return returnStocType; } public String toString() { - StringBuffer sb = new StringBuffer("StoreContainerType: "); - sb.append(this.stocClass.toString()).append("@"); - sb.append(stringForStoreType(stocType)); + StringBuffer sb = new StringBuffer(this.stocClass.toString()); + sb.append("@").append(stringForStoreType(stocType)); return sb.toString(); } diff --git a/source/mir/storage/store/StoreIdentifier.java b/source/mir/storage/store/StoreIdentifier.java index dbaac624..4b7000fc 100755 --- a/source/mir/storage/store/StoreIdentifier.java +++ b/source/mir/storage/store/StoreIdentifier.java @@ -1,9 +1,13 @@ package mir.storage.store; /** - * Title: Class StoreIdentifier - * Description: - * Copyright: Copyright (c) 2002 + * Title: Class StoreIdentifier + * Description: StoreIdentifier has two functions. + * A) StoreIdenfier holds a reference to a @see StorableObject + * or B) StoreIdentifier is used too search for a @see StorableObject + * in the @see StoreContainer that matches its + * + * Copyright: Copyright (c) 2002 * Company: indy * @author rk * @version 1.0 @@ -29,11 +33,18 @@ public class StoreIdentifier { private StoreIdentifier() {} public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) { + this(reference.getClass(), uniqueIdentifier); this.reference=reference; - this.uniqueIdentifier=uniqueIdentifier; - this.stocType = StoreContainerType.valueOf(reference.getClass(), storeType); } + public StoreIdentifier(Class theClass, String uniqueIdentifier) { + this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier); + } + + public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) { + this.uniqueIdentifier=uniqueIdentifier; + this.stocType = StoreContainerType.valueOf(theClass, storeType); + } /** * Method: ivalidate * Description: @@ -44,7 +55,7 @@ public class StoreIdentifier { // avoid deadlock due to propagation. if (!invalidating) { invalidating=true; - Set set = reference.notifyOnReleaseSet(); + Set set = reference.getNotifyOnReleaseSet(); /** @todo here we should propagate the invalidation all members of Set * @see StoreContainer. The set may contain objects of different type.*/ release(); @@ -57,7 +68,7 @@ public class StoreIdentifier { this.stocType=null; } - public Object use() { + public StorableObject use() { timesUsed++; return reference; } @@ -69,11 +80,10 @@ public class StoreIdentifier { * */ public boolean equals(Object sid) { - if ( sid instanceof StoreIdentifier) { - if ( ((StoreIdentifier)sid).getStoreContainerType().equals(stocType) && - ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) ) + if ( !(sid instanceof StoreIdentifier) ) return false; + if ( ((StoreIdentifier)sid).getStoreContainerType()==stocType && + ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) ) return true; - } return false; } @@ -82,8 +92,10 @@ public class StoreIdentifier { public boolean hasReference() { return (reference==null) ? false:true; } public String toString() { - return reference.getClass()+"@storetype"+stocType.toString()+"." - +uniqueIdentifier+" ("+timesUsed+") times used )"; + StringBuffer id = new StringBuffer(uniqueIdentifier); + id.append("@storetype: ").append(stocType.toString()); + if (reference != null) id.append(" ("+timesUsed).append(") times used."); + return id.toString(); } diff --git a/source/mir/storage/store/test/EntityC1.java b/source/mir/storage/store/test/EntityC1.java index 635ca31c..4f5d89d7 100755 --- a/source/mir/storage/store/test/EntityC1.java +++ b/source/mir/storage/store/test/EntityC1.java @@ -24,7 +24,7 @@ public class EntityC1 implements StorableObject { return new StoreIdentifier(this, StoreContainerType.STOC_TYPE_ENTITY,id); } - public Set notifyOnReleaseSet() { + public Set getNotifyOnReleaseSet() { return null; } } \ No newline at end of file diff --git a/source/mir/storage/store/test/EntityC2.java b/source/mir/storage/store/test/EntityC2.java new file mode 100755 index 00000000..7cbf03e4 --- /dev/null +++ b/source/mir/storage/store/test/EntityC2.java @@ -0,0 +1,30 @@ +package mir.storage.store.test; + +/** + * Title: + * Description: + * Copyright: Copyright (c) 2002 + * Company: + * @author + * @version 1.0 + */ + +import java.util.*; +import mir.storage.store.*; + +public class EntityC2 implements StorableObject { + + String id; + + public EntityC2(String id) { + this.id=id; + } + + public StoreIdentifier getStoreIdentifier() { + return new StoreIdentifier(this, StoreContainerType.STOC_TYPE_ENTITY,id); + } + + public Set getNotifyOnReleaseSet() { + return null; + } +} \ No newline at end of file diff --git a/source/mir/storage/store/test/EntityC3.java b/source/mir/storage/store/test/EntityC3.java new file mode 100755 index 00000000..3c140cb6 --- /dev/null +++ b/source/mir/storage/store/test/EntityC3.java @@ -0,0 +1,30 @@ +package mir.storage.store.test; + +/** + * Title: + * Description: + * Copyright: Copyright (c) 2002 + * Company: + * @author + * @version 1.0 + */ + +import java.util.*; +import mir.storage.store.*; + +public class EntityC3 implements StorableObject { + + String id; + + public EntityC3(String id) { + this.id=id; + } + + public StoreIdentifier getStoreIdentifier() { + return new StoreIdentifier(this, StoreContainerType.STOC_TYPE_ENTITY,id); + } + + public Set getNotifyOnReleaseSet() { + return null; + } +} \ No newline at end of file diff --git a/source/mir/storage/store/test/TestStore.java b/source/mir/storage/store/test/TestStore.java index e8903b40..29c15e5e 100755 --- a/source/mir/storage/store/test/TestStore.java +++ b/source/mir/storage/store/test/TestStore.java @@ -29,11 +29,39 @@ public class TestStore { } public void startTest() { - //System.out.println(o_store.toString()); + EntityC1 c1 = new EntityC1("1"); o_store.add(c1.getStoreIdentifier()); - EntityC1 c2 = new EntityC1("2"); - o_store.add(c2.getStoreIdentifier()); + EntityC1 c12 = new EntityC1("2"); + o_store.add(c12.getStoreIdentifier()); + o_store.add(c12.getStoreIdentifier()); // should not be added as it's there already + + EntityC2 c2; + for (int i=0; i<20; i++) { + c2 = new EntityC2(""+i); + o_store.add(c2.getStoreIdentifier()); + } // should contain only 10 + + // test cycle: search in store + + StorableObject reference; StoreIdentifier search_sid; + + // search for EntityC1 + search_sid=new StoreIdentifier(EntityC1.class,"1"); + reference=o_store.use(search_sid); + if (reference==null) + System.out.println("--- should have found" + search_sid.toString()); + + search_sid=new StoreIdentifier(EntityC1.class,"A"); + reference=o_store.use(search_sid); + if (reference!=null) + System.out.println("--- should not have found" + search_sid.toString()); + + search_sid=new StoreIdentifier(EntityC3.class,"1"); + reference=o_store.use(search_sid); + if (reference!=null) + System.out.println("--- should not have found" + search_sid.toString()); + System.out.println(o_store.toString()); /** @todo compare values of store and state failed if values are not