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 javax.servlet.http.*;
22 public class StoreContainer {
24 private final static int DEFAULT_SIZE=10;
25 private static Logfile storeLog;
26 private static int uniqueCounter=10000;
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();
35 // avoid construction without parameters
36 private StoreContainer() {};
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);
50 public StoreContainer(StoreContainerType stoc_type, int maxSize) {
57 public synchronized StorableObject use(StoreIdentifier sid) {
58 int hit = container.indexOf(sid);
60 StoreIdentifier hitSid = (StoreIdentifier)container.get(hit);
70 public boolean has(StoreIdentifier sid) {
71 return container.contains(sid);
74 public void add(StoreIdentifier sid) {
75 if ( sid != null && sid.hasReference() ) {
78 System.err.println("OBJECTStore: tried to add sid " + sid.toString() +
79 " that was already in store.");
82 container.addFirst(sid);
90 * Method: invalidate(StorableObject sto)
91 * Description: finds @see StorableObject, propagates invalidation to
92 * @see StoreIdentifier and removes StoreIdentifier from
95 public synchronized void invalidate(StoreIdentifier search_sid) {
96 if (search_sid!=null) {
97 int hit = container.indexOf(search_sid);
99 StoreIdentifier sid = (StoreIdentifier)container.get(hit);
100 container.remove(sid);
107 public synchronized void invalidate() {
109 while (container.size() > 0) {
110 sid=(StoreIdentifier)container.getLast();
111 container.removeLast();
118 * Description: readjusts StoreContainer size to value.
121 public void setSize(int size) {
127 private void shrinkIfNecessary() { shrinkToSize(maxSize); }
129 private void shrinkToSize(int size) {
130 if ( size < container.size() ) {
132 while (size < container.size() ) {
133 StoreIdentifier sid = (StoreIdentifier)container.getLast();
134 container.remove(sid);
141 private synchronized void moveToHead(StoreIdentifier sid) {
143 container.remove(sid);
144 container.addFirst(sid);
150 * Description: gives out statistical Information, viewable via
151 * @see ServletStoreInfo.
155 public String toString() {
159 public String toHtml(HttpServletRequest req) {
160 boolean showingContent=false;
162 long divisor=hitCount+missCount;
163 if (divisor>0) hitRatio=(float)hitCount/(float)divisor;
166 StringBuffer sb = new StringBuffer("StoreContainer id: ");
167 sb.append(uniqueId).append(" for ");
168 sb.append(stocType.toString());
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>]");
177 sb.append(" [<a href=\"?stoc_show="+uniqueId+"\">show</a>]");
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");
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 ");
193 sb.append("<b>End of List</b>\n\n");
197 return sb.toString();