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 synchronized 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 synchronized void invalidate(StoreIdentifier search_sid) {
86 if (search_sid!=null) {
87 int hit = container.indexOf(search_sid);
89 StoreIdentifier sid = (StoreIdentifier)container.get(hit);
90 container.remove(sid);
97 public synchronized void invalidate() {
99 while (container.size() > 0) {
100 sid=(StoreIdentifier)container.getLast();
101 container.removeLast();
108 * Description: readjusts StoreContainer size to value.
111 public void setSize(int size) {
117 private void shrinkIfNecessary() { shrinkToSize(maxSize); }
119 private void shrinkToSize(int size) {
120 if ( size < container.size() ) {
122 while (size < container.size() ) {
123 StoreIdentifier sid = (StoreIdentifier)container.getLast();
124 container.remove(sid);
131 private synchronized void moveToHead(StoreIdentifier sid) {
133 container.remove(sid);
134 container.addFirst(sid);
140 * Description: gives out statistical Information, viewable via
141 * @see ServletStoreInfo.
145 public String toString() {
147 long divisor=hitCount+missCount;
148 if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
151 StringBuffer sb = new StringBuffer("StoreContainer id: ");
152 sb.append(uniqueId).append(" for ");
153 sb.append(stocType.toString()).append("\n [current/maximum size: ");
154 sb.append(container.size()).append("/").append(maxSize);
155 sb.append("]\n [added/stored out/removed: ").append(addCount).append("/");
156 sb.append(storeOutCount).append("/").append(removeCount).append("]\n [hit/miss/ratio: ");
157 sb.append(hitCount).append("/").append(missCount).append("/");
158 sb.append(hitRatio).append("%]\n");
160 /** @todo list members ? */
161 return sb.toString();