e5c79c51a7c4fc04d9dd43b9131d261a87e0e369
[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 javax.servlet.http.*;
20 import mir.misc.*;
21
22 public class StoreContainer {
23
24         private final static int    DEFAULT_SIZE=10;
25         private static Logfile      storeLog;
26         private static int          uniqueCounter=10000;
27
28         private LinkedList          container;
29         private StoreContainerType  stocType;
30         private int                 maxSize=DEFAULT_SIZE, uniqueId;
31   private int                 addCount=0,removeCount=0,storeOutCount;
32   private int                 hitCount=0,missCount=0;
33   private static ObjectStore  o_store = ObjectStore.getInstance();
34
35         // avoid construction without parameters
36   private StoreContainer() {};
37
38         public StoreContainer(StoreContainerType stoc_type) {
39                 this.uniqueId=++uniqueCounter;
40                 this.stocType=stoc_type;
41                 this.container=new LinkedList();
42     int defaultSize = stoc_type.getDefaultSize();
43     String confProperty = stoc_type.getConfPrefix()+".DefaultSize";
44     String confedSize = o_store.getConfProperty(confProperty);
45     if ( confedSize!=null ) {
46       this.maxSize = StringUtil.parseInt (confedSize, defaultSize);
47     }
48         }
49
50         public StoreContainer(StoreContainerType stoc_type, int maxSize) {
51                 this();
52                 this.maxSize=maxSize;
53         }
54
55
56
57         public synchronized StorableObject use(StoreIdentifier sid) {
58     int hit = container.indexOf(sid);
59     if (hit>=0) {
60       StoreIdentifier hitSid = (StoreIdentifier)container.get(hit);
61                   if ( hitSid!=null ) {
62          hitCount++;
63          return hitSid.use();
64                   }
65     }
66     missCount++;
67     return null;
68         }
69
70         public boolean has(StoreIdentifier sid) {
71                 return container.contains(sid);
72         }
73
74         public void add(StoreIdentifier sid) {
75                 if ( sid != null && sid.hasReference() ) {
76                         if ( has(sid) ) {
77                           moveToHead(sid);
78                           System.err.println("OBJECTStore: tried to add sid " + sid.toString() +
79                             " that was already in store.");
80       }
81                         else {
82                                 container.addFirst(sid);
83         shrinkIfNecessary();
84         addCount++;
85                         }
86                 }
87         }
88
89         /**
90          *  Method:       invalidate(StorableObject sto)
91          *  Description:  finds @see StorableObject, propagates invalidation to
92          *                @see StoreIdentifier and removes StoreIdentifier from
93          *                list.
94          */
95         public synchronized void invalidate(StoreIdentifier search_sid) {
96     if (search_sid!=null) {
97       int hit = container.indexOf(search_sid);
98       if (hit >0 ) {
99         StoreIdentifier sid = (StoreIdentifier)container.get(hit);
100         container.remove(sid);
101         sid.invalidate();
102         removeCount++;
103       }
104     }
105         }
106
107   public synchronized void invalidate() {
108     StoreIdentifier sid;
109     while (container.size() > 0) {
110       sid=(StoreIdentifier)container.getLast();
111       container.removeLast();
112       sid.invalidate();
113     }
114   }
115
116         /**
117          *  Method:       setSize
118          *  Description:  readjusts StoreContainer size to value.
119          *
120          */
121         public void setSize(int size) {
122                 if (size <0) return;
123     shrinkToSize(size);
124                 this.maxSize=size;
125         }
126
127         private void shrinkIfNecessary() { shrinkToSize(maxSize); }
128
129   private void shrinkToSize(int size) {
130     if ( size < container.size() ) {
131                         // shrink
132                         while (size < container.size() ) {
133                                 StoreIdentifier sid = (StoreIdentifier)container.getLast();
134                                 container.remove(sid);
135         sid.release();
136         storeOutCount++;
137                         }
138                 }
139   }
140
141   private synchronized void moveToHead(StoreIdentifier sid) {
142     if ( sid!=null ) {
143       container.remove(sid);
144       container.addFirst(sid);
145     }
146   }
147
148         /**
149          *  Method:       toString()
150          *  Description:  gives out statistical Information, viewable via
151          *                @see ServletStoreInfo.
152          *
153          *  @return       String
154          */
155         public String toString() {
156     return toHtml(null);
157         }
158
159   public String toHtml(HttpServletRequest req) {
160     boolean showingContent=false;
161     float hitRatio=0;
162     long divisor=hitCount+missCount;
163     if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
164     hitRatio*=100;
165
166                 StringBuffer sb = new StringBuffer("StoreContainer id: ");
167                 sb.append(uniqueId).append(" for ");
168                 sb.append(stocType.toString());
169     if ( req!=null ) {
170       String show = req.getParameter("stoc_show");
171       if ( show!=null && show.equals(""+uniqueId) ) {
172         // show all entries in container
173         sb.append(" [<b>showing</b>]");
174         showingContent=true;
175       }
176       else
177         sb.append(" [<a href=\"?stoc_show="+uniqueId+"\">show</a>]");
178     }
179     sb.append("\n  [current/maximum size: ");
180                 sb.append(container.size()).append("/").append(maxSize);
181                 sb.append("]\n  [added/stored out/removed: ").append(addCount).append("/");
182     sb.append(storeOutCount).append("/").append(removeCount).append("]\n  [hit/miss/ratio: ");
183     sb.append(hitCount).append("/").append(missCount).append("/");
184     sb.append(hitRatio).append("%]\n");
185
186     if (showingContent) {
187       sb.append("  <b>Container contains following references:</b>\n  ");
188       ListIterator it = container.listIterator();
189       while ( it.hasNext() ) {
190         StoreIdentifier sid = (StoreIdentifier)it.next();
191         sb.append(sid.toString()).append("\n  ");
192       }
193       sb.append("<b>End of List</b>\n\n");
194
195     }
196
197                 return sb.toString();
198         }
199
200 }