*** empty log message ***
[mir.git] / source / mir / storage / store / StoreContainer.java
index fcff1f6..acec906 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package mir.storage.store;\r
-\r
-/**\r
- * Title:         StoreContainer\r
- *\r
- * Description:   This is the bucket object for one type of StorableObjects,\r
- *                mainy a linked list of StoreIdenfiers. On use or creation\r
- *                an object stored in StoreIdentifier is put to head of the\r
- *                list. if maximum size of the list is reached, the\r
- *                StoreIdentifier at the end of the list is released.\r
- *\r
- * Copyright:     Copyright (c) 2002\r
- * Company:       indy\r
- * @author        //rk\r
- * @version 1.0\r
- */\r
-\r
-import java.util.LinkedList;\r
-import java.util.ListIterator;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-\r
-import mir.misc.StringUtil;\r
-import mir.log.LoggerWrapper;\r
-\r
-public class StoreContainer {\r
-  private final static int DEFAULT_SIZE = 10;\r
-  private static int uniqueCounter = 10000;\r
-\r
-  private LinkedList container;\r
-  private StoreContainerType stocType;\r
-  private int maxSize = DEFAULT_SIZE, uniqueId;\r
-  private int addCount = 0, removeCount = 0, storeOutCount;\r
-  private int hitCount = 0, missCount = 0;\r
-  private static ObjectStore o_store = ObjectStore.getInstance();\r
-\r
-  protected LoggerWrapper logger = new LoggerWrapper("Database.ObjectStore");\r
-\r
-  // avoid construction without parameters\r
-  private StoreContainer() {};\r
-\r
-\r
-  public StoreContainer(StoreContainerType stoc_type) {\r
-    this.uniqueId = ++uniqueCounter;\r
-    this.stocType = stoc_type;\r
-    this.container = new LinkedList();\r
-    int defaultSize = stoc_type.getDefaultSize();\r
-    String confProperty = stoc_type.getConfPrefix() + ".DefaultSize";\r
-    String confedSize = o_store.getConfProperty(confProperty);\r
-    if (confedSize != null) {\r
-      this.maxSize = StringUtil.parseInt(confedSize, defaultSize);\r
-    }\r
-  }\r
-\r
-  public StoreContainer(StoreContainerType stoc_type, int maxSize) {\r
-    this();\r
-    this.maxSize = maxSize;\r
-  }\r
-\r
-  public synchronized StorableObject use(StoreIdentifier sid) {\r
-    int hit = container.indexOf(sid);\r
-    if (hit >= 0) {\r
-      StoreIdentifier hitSid = (StoreIdentifier) container.get(hit);\r
-      if (hitSid != null) {\r
-        hitCount++;\r
-        return hitSid.use();\r
-      }\r
-    }\r
-    missCount++;\r
-    return null;\r
-  }\r
-\r
-  public boolean has(StoreIdentifier sid) {\r
-    return container.contains(sid);\r
-  }\r
-\r
-  public void add(StoreIdentifier sid) {\r
-    if (sid != null && sid.hasReference()) {\r
-      if (has(sid)) {\r
-        moveToHead(sid);\r
-        logger.error("OBJECTStore: tried to add sid " + sid.toString() + " that was already in store.");\r
-      }\r
-      else {\r
-        container.addFirst(sid);\r
-        shrinkIfNecessary();\r
-        addCount++;\r
-      }\r
-    }\r
-  }\r
-\r
-  /**\r
-   *  Method:       invalidate(StorableObject sto)\r
-   *  Description:  finds @see StorableObject, propagates invalidation to\r
-   *                @see StoreIdentifier and removes StoreIdentifier from\r
-   *                list.\r
-   */\r
-  public synchronized void invalidate(StoreIdentifier search_sid) {\r
-    if (search_sid != null) {\r
-      int hit = container.indexOf(search_sid);\r
-      if (hit >= 0) {\r
-        StoreIdentifier sid = (StoreIdentifier) container.get(hit);\r
-        container.remove(sid);\r
-        sid.invalidate();\r
-        removeCount++;\r
-      }\r
-    }\r
-  }\r
-\r
-  public synchronized void invalidate() {\r
-    StoreIdentifier sid;\r
-    while (container.size() > 0) {\r
-      sid = (StoreIdentifier) container.getLast();\r
-      container.removeLast();\r
-      sid.invalidate();\r
-    }\r
-  }\r
-\r
-  /**\r
-   *  Method:       setSize\r
-   *  Description:  readjusts StoreContainer size to value.\r
-   *\r
-   */\r
-  public void setSize(int size) {\r
-    if (size < 0)\r
-      return;\r
-    shrinkToSize(size);\r
-    this.maxSize = size;\r
-  }\r
-\r
-  private void shrinkIfNecessary() {\r
-    shrinkToSize(maxSize);\r
-  }\r
-\r
-  private void shrinkToSize(int size) {\r
-    if (size < container.size()) {\r
-      // shrink\r
-      while (size < container.size()) {\r
-        StoreIdentifier sid = (StoreIdentifier) container.getLast();\r
-        container.remove(sid);\r
-        sid.release();\r
-        storeOutCount++;\r
-      }\r
-    }\r
-  }\r
-\r
-  private synchronized void moveToHead(StoreIdentifier sid) {\r
-    if (sid != null) {\r
-      container.remove(sid);\r
-      container.addFirst(sid);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *  Method:       toString()\r
-   *  Description:  gives out statistical Information, viewable via\r
-   *                @see ServletStoreInfo.\r
-   *\r
-   *  @return       String\r
-   */\r
-  public String toString() {\r
-    return toHtml(null);\r
-  }\r
-\r
-  public String toHtml(HttpServletRequest req) {\r
-    boolean showingContent = false;\r
-    float hitRatio = 0;\r
-    long divisor = hitCount + missCount;\r
-    if (divisor > 0)\r
-      hitRatio = (float) hitCount / (float) divisor;\r
-    hitRatio *= 100;\r
-\r
-    StringBuffer sb = new StringBuffer("StoreContainer id: ");\r
-    sb.append(uniqueId).append(" for ");\r
-    sb.append(stocType.toString());\r
-    if (req != null) {\r
-      String show = req.getParameter("stoc_show");\r
-      if (show != null && show.equals("" + uniqueId)) {\r
-        // show all entries in container\r
-        sb.append(" [<b>showing</b>]");\r
-        showingContent = true;\r
-      }\r
-      else\r
-        sb.append(" [<a href=\"?stoc_show=" + uniqueId + "\">show</a>]");\r
-    }\r
-    sb.append("\n  [current/maximum size: ");\r
-    sb.append(container.size()).append("/").append(maxSize);\r
-    sb.append("]\n  [added/stored out/removed: ").append(addCount).append("/");\r
-    sb.append(storeOutCount).append("/").append(removeCount).append(\r
-        "]\n  [hit/miss/ratio: ");\r
-    sb.append(hitCount).append("/").append(missCount).append("/");\r
-    sb.append(hitRatio).append("%]\n");\r
-\r
-    if (showingContent) {\r
-      sb.append("  <b>Container contains following references:</b>\n  ");\r
-      ListIterator it = container.listIterator();\r
-      while (it.hasNext()) {\r
-        StoreIdentifier sid = (StoreIdentifier) it.next();\r
-        sb.append(sid.toString()).append("\n  ");\r
-      }\r
-      sb.append("<b>End of List</b>\n\n");\r
-\r
-    }\r
-\r
-    return sb.toString();\r
-  }\r
-\r
+/*
+ * Copyright (C) 2001-2006 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mir.storage.store;
+
+/**
+ * Title:         StoreContainer
+ *
+ * Description:   This is the bucket object for one type of StorableObjects,
+ *                mainy a linked list of StoreIdenfiers. On use or creation
+ *                an object stored in StoreIdentifier is put to head of the
+ *                list. if maximum size of the list is reached, the
+ *                StoreIdentifier at the end of the list is released.
+ *
+ * Copyright:     Copyright (c) 2002
+ * Company:       indy
+ * @author        //rk
+ * @version 1.0
+ */
+
+import mir.log.LoggerWrapper;
+import mir.misc.StringUtil;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+public class StoreContainer {
+  private final static int DEFAULT_SIZE = 10;
+  private static int uniqueCounter = 10000;
+
+  private LinkedList container;
+  private StoreContainerType stocType;
+  private int maxSize = DEFAULT_SIZE, uniqueId;
+  private int addCount = 0, removeCount = 0, storeOutCount;
+  private int hitCount = 0, missCount = 0;
+  private static ObjectStore o_store = ObjectStore.getInstance();
+
+  protected LoggerWrapper logger = new LoggerWrapper("Database.ObjectStore");
+
+  // avoid construction without parameters
+  private StoreContainer() {}
+
+
+  public StoreContainer(StoreContainerType stoc_type) {
+    this.uniqueId = ++uniqueCounter;
+    this.stocType = stoc_type;
+    this.container = new LinkedList();
+    int defaultSize = stoc_type.getDefaultSize();
+    String confProperty = stoc_type.getConfPrefix() + ".DefaultSize";
+    String confedSize = o_store.getConfProperty(confProperty);
+    if (confedSize != null) {
+      this.maxSize = StringUtil.parseInt(confedSize, defaultSize);
+    }
+  }
+
+  public StoreContainer(StoreContainerType stoc_type, int maxSize) {
+    this();
+    this.maxSize = maxSize;
+  }
+
+  public synchronized StorableObject use(StoreIdentifier sid) {
+    int hit = container.indexOf(sid);
+    if (hit >= 0) {
+      StoreIdentifier hitSid = (StoreIdentifier) container.get(hit);
+      if (hitSid != null) {
+        hitCount++;
+        return hitSid.use();
+      }
+    }
+    missCount++;
+    return null;
+  }
+
+  public synchronized boolean has(StoreIdentifier sid) {
+    return container.contains(sid);
+  }
+
+  public synchronized void add(StoreIdentifier sid) {
+    if (sid != null && sid.hasReference()) {
+      if (has(sid)) {
+        moveToHead(sid);
+        logger.debug("OBJECTStore: tried to add sid " + sid.toString() + " that was already in store.");
+      }
+      else {
+        container.addFirst(sid);
+        shrinkIfNecessary();
+        addCount++;
+      }
+    }
+  }
+
+  /**
+   *  Method:       invalidate(StorableObject sto)
+   *  Description:  finds @see StorableObject, propagates invalidation to
+   *                @see StoreIdentifier and removes StoreIdentifier from
+   *                list.
+   */
+  public synchronized void invalidate(StoreIdentifier search_sid) {
+    if (search_sid != null) {
+      int hit = container.indexOf(search_sid);
+      if (hit >= 0) {
+        StoreIdentifier sid = (StoreIdentifier) container.get(hit);
+        container.remove(sid);
+        sid.invalidate();
+        removeCount++;
+      }
+    }
+  }
+
+  public synchronized void invalidate() {
+    StoreIdentifier sid;
+    while (container.size() > 0) {
+      sid = (StoreIdentifier) container.getLast();
+      container.removeLast();
+      sid.invalidate();
+    }
+  }
+
+  /**
+   *  Method:       setSize
+   *  Description:  readjusts StoreContainer size to value.
+   *
+   */
+  public synchronized void setSize(int size) {
+    if (size < 0)
+      return;
+    shrinkToSize(size);
+    this.maxSize = size;
+  }
+
+  private void shrinkIfNecessary() {
+    shrinkToSize(maxSize);
+  }
+
+  private synchronized void shrinkToSize(int size) {
+    if (size < container.size()) {
+      // shrink
+      while (size < container.size()) {
+        StoreIdentifier sid = (StoreIdentifier) container.getLast();
+        container.remove(sid);
+        sid.release();
+        storeOutCount++;
+      }
+    }
+  }
+
+  private synchronized void moveToHead(StoreIdentifier sid) {
+    if (sid != null) {
+      container.remove(sid);
+      container.addFirst(sid);
+    }
+  }
+
+  /**
+   *  Method:       toString()
+   *  Description:  gives out statistical Information, viewable via
+   *                @see ServletStoreInfo
+   *
+   *  @return       String
+   */
+  public String toString() {
+    return toHtml(null);
+  }
+
+  public String toHtml(HttpServletRequest req) {
+    boolean showingContent = false;
+    float hitRatio = 0;
+    long divisor = hitCount + missCount;
+    if (divisor > 0)
+      hitRatio = (float) hitCount / (float) divisor;
+    hitRatio *= 100;
+
+    StringBuffer sb = new StringBuffer("StoreContainer id: ");
+    sb.append(uniqueId).append(" for ");
+    sb.append(stocType.toString());
+    if (req != null) {
+      String show = req.getParameter("stoc_show");
+      if (show != null && show.equals("" + uniqueId)) {
+        // show all entries in container
+        sb.append(" [<b>showing</b>]");
+        showingContent = true;
+      }
+      else
+        sb.append(" [<a href=\"?stoc_show=" + uniqueId + "\">show</a>]");
+    }
+    sb.append("\n  [current/maximum size: ");
+    sb.append(container.size()).append("/").append(maxSize);
+    sb.append("]\n  [added/stored out/removed: ").append(addCount).append("/");
+    sb.append(storeOutCount).append("/").append(removeCount).append(
+        "]\n  [hit/miss/ratio: ");
+    sb.append(hitCount).append("/").append(missCount).append("/");
+    sb.append(hitRatio).append("%]\n");
+
+    if (showingContent) {
+      sb.append("  <b>Container contains following references:</b>\n  ");
+      ListIterator it = container.listIterator();
+      while (it.hasNext()) {
+        StoreIdentifier sid = (StoreIdentifier) it.next();
+        sb.append(sid.toString()).append("\n  ");
+      }
+      sb.append("<b>End of List</b>\n\n");
+
+    }
+
+    return sb.toString();
+  }
+
 }
\ No newline at end of file