1 package mir.storage.store;
4 * Title: StoreContainer
6 * Description: This is the bucket object for one type of StorableObjects,
7 * mainy a linked list of StoreIdenfiers. On use or creation
8 * an object stored in StoreIdentifier is put to head of the
9 * list. if maximum size of the list is reached, the
10 * StoreIdentifier at the end of the list is released.
12 * Copyright: Copyright (c) 2002
19 import mir.misc.Logfile;
21 public class StoreContainer {
23 private final static int DEFAULT_SIZE=10;
24 private static Logfile storeLog;
25 private static int uniqueCounter=10000;
27 private LinkedList container;
28 private StoreContainerType stocType;
29 private int maxSize=DEFAULT_SIZE;
31 private int addCount=0,removeCount=0,storeOutCount;
32 private int hitCount=0,missCount=0;
34 private StoreContainer() {};
36 public StoreContainer(StoreContainerType stoc_type) {
37 this.uniqueId=++uniqueCounter;
38 this.stocType=stoc_type;
39 this.container=new LinkedList();
42 public StoreContainer(StoreContainerType stoc_type, int maxSize) {
47 public StorableObject use(StoreIdentifier sid) {
48 int hit = container.indexOf(sid);
50 StoreIdentifier hitSid = (StoreIdentifier)container.get(hit);
60 public boolean has(StoreIdentifier sid) {
61 return container.contains(sid);
64 public void add(StoreIdentifier sid) {
65 if ( sid != null && sid.hasReference() ) {
68 System.err.println("OBJECTStore: tried to add sid " + sid.toString() +
69 " that was already in store.");
72 container.addFirst(sid);
80 * Method: invalidate(StorableObject sto)
81 * Description: finds @see StorableObject, propagates invalidation to
82 * @see StoreIdentifier and removes StoreIdentifier from
85 public void invalidate(StorableObject sto) {
87 StoreIdentifier sid = sto.getStoreIdentifier();
89 if ( container.contains(sid) ) {
91 container.remove(sid);
100 * Description: readjusts StoreContainer size to value.
103 public void setSize(int size) {
109 private void shrinkIfNecessary() { shrinkToSize(maxSize); }
111 private void shrinkToSize(int size) {
112 if ( size < container.size() ) {
114 while (size < container.size() ) {
115 StoreIdentifier sid = (StoreIdentifier)container.getLast();
116 container.remove(sid);
123 private synchronized void moveToHead(StoreIdentifier sid) {
125 container.remove(sid);
126 container.addFirst(sid);
132 * Description: gives out statistical Information, viewable via
133 * @see ServletStoreInfo.
137 public String toString() {
139 long divisor=hitCount+missCount;
140 if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
143 StringBuffer sb = new StringBuffer("StoreContainer id: ");
144 sb.append(uniqueId).append(" for ");
145 sb.append(stocType.toString()).append("\n [current/maximum size: ");
146 sb.append(container.size()).append("/").append(maxSize);
147 sb.append("]\n [added/stored out/removed: ").append(addCount).append("/");
148 sb.append(storeOutCount).append("/").append(removeCount).append("]\n [hit/miss/ratio: ");
149 sb.append(hitCount).append("/").append(missCount).append("/");
150 sb.append(hitRatio).append("%]\n");
152 /** @todo list members ? */
153 return sb.toString();