397179f8dfa63f105c4cf62847ae6c88c18e0bdc
[mir.git] / source / mir / storage / store / StoreContainer.java
1 package mir.storage.store;
2
3 /**
4  * Title:         StoreContainer
5  *
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.
11  *
12  * Copyright:     Copyright (c) 2002
13  * Company:       indy
14  * @author        //rk
15  * @version 1.0
16  */
17
18 import java.util.*;
19 import mir.misc.Logfile;
20
21 public class StoreContainer {
22
23         private final static int    DEFAULT_SIZE=10;
24         private static Logfile      storeLog;
25         private static int          uniqueCounter=10000;
26
27         private LinkedList          container;
28         private StoreContainerType  stocType;
29         private int                 maxSize=DEFAULT_SIZE;
30         private int                 uniqueId;
31   private int                 addCount=0,removeCount=0,storeOutCount;
32   private int                 hitCount=0,missCount=0;
33
34         private StoreContainer() {};
35
36         public StoreContainer(StoreContainerType stoc_type) {
37                 this.uniqueId=++uniqueCounter;
38                 this.stocType=stoc_type;
39                 this.container=new LinkedList();
40         }
41
42         public StoreContainer(StoreContainerType stoc_type, int maxSize) {
43                 this();
44                 this.maxSize=maxSize;
45         }
46
47         public StorableObject use(StoreIdentifier sid) {
48     int hit = container.indexOf(sid);
49     if (hit>0) {
50       StoreIdentifier hitSid = (StoreIdentifier)container.get(hit);
51                   if ( hitSid!=null ) {
52          hitCount++;
53          return hitSid.use();
54                   }
55     }
56     missCount++;
57     return null;
58         }
59
60         public boolean has(StoreIdentifier sid) {
61                 return container.contains(sid);
62         }
63
64         public void add(StoreIdentifier sid) {
65                 if ( sid != null && sid.hasReference() ) {
66                         if ( has(sid) ) {
67                           moveToHead(sid);
68                           System.err.println("OBJECTStore: tried to add sid " + sid.toString() +
69                             " that was already in store.");
70       }
71                         else {
72                                 container.addFirst(sid);
73         shrinkIfNecessary();
74         addCount++;
75                         }
76                 }
77         }
78
79         /**
80          *  Method:       invalidate(StorableObject sto)
81          *  Description:  finds @see StorableObject, propagates invalidation to
82          *                @see StoreIdentifier and removes StoreIdentifier from
83          *                list.
84          */
85         public void invalidate(StorableObject sto) {
86                 if (sto!=null) {
87                         StoreIdentifier sid = sto.getStoreIdentifier();
88                         if (sid!=null) {
89                                 if ( container.contains(sid) ) {
90                                         sid.invalidate();
91                                         container.remove(sid);
92           removeCount++;
93                                 }
94                         }
95                 }
96         }
97
98         /**
99          *  Method:       setSize
100          *  Description:  readjusts StoreContainer size to value.
101          *
102          */
103         public void setSize(int size) {
104                 if (size <0) return;
105     shrinkToSize(size);
106                 this.maxSize=size;
107         }
108
109         private void shrinkIfNecessary() { shrinkToSize(maxSize); }
110
111   private void shrinkToSize(int size) {
112     if ( size < container.size() ) {
113                         // shrink
114                         while (size < container.size() ) {
115                                 StoreIdentifier sid = (StoreIdentifier)container.getLast();
116                                 container.remove(sid);
117         sid.release();
118         storeOutCount++;
119                         }
120                 }
121   }
122
123   private synchronized void moveToHead(StoreIdentifier sid) {
124     if ( sid!=null ) {
125       container.remove(sid);
126       container.addFirst(sid);
127     }
128   }
129
130         /**
131          *  Method:       toString()
132          *  Description:  gives out statistical Information, viewable via
133          *                @see ServletStoreInfo.
134          *
135          *  @return       String
136          */
137         public String toString() {
138     float hitRatio=0;
139     long divisor=hitCount+missCount;
140     if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
141     hitRatio*=100;
142
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");
151
152     /** @todo list members ? */
153                 return sb.toString();
154         }
155
156 }