a new db-cache with max-load, not documented yet
authoridfx <idfx>
Wed, 6 Feb 2002 14:22:39 +0000 (14:22 +0000)
committeridfx <idfx>
Wed, 6 Feb 2002 14:22:39 +0000 (14:22 +0000)
source/mir/servlet/ServletModule.java
source/mir/storage/Database.java
source/mir/storage/DatabaseCache.java [new file with mode: 0755]
source/mircoders/storage/DatabaseBreaking.java
source/mircoders/storage/DatabaseContent.java
source/mircoders/storage/DatabaseFeature.java
source/mircoders/storage/DatabaseMedia.java
source/mircoders/storage/DatabaseMediaType.java
source/mircoders/storage/DatabaseMediafolder.java
source/mircoders/storage/DatabaseMessages.java
source/mircoders/storage/DatabaseTopics.java

index 8631ddb..eab5dc5 100755 (executable)
@@ -133,8 +133,9 @@ public abstract class ServletModule {
       // theLog.printDebugInfo("--trying to deliver..."+id);
       list(req,res);
       //deliver(req, res, mainModule.getById(id), templateObjektString);
+    } catch (Exception e) {
+      throw new ServletModuleException(e.toString());
     }
-    catch (Exception e) { throw new ServletModuleException(e.toString());}
   }
 
 /**
index f4aa63f..575d374 100755 (executable)
@@ -37,7 +37,7 @@ public class Database implements StorageObject {
   protected int[]                     metadataTypes;
   protected Class                     theEntityClass;
   protected StorageObject             myselfDatabase;
-  protected HashMap                   cache;
+  protected DatabaseCache             cache;
   protected SimpleList                popupCache=null;
   protected boolean                   hasPopupCache = false;
   protected SimpleHash                hashCache=null;
@@ -253,7 +253,7 @@ public class Database implements StorageObject {
 
     if (id==null||id.equals(""))
       throw new StorageObjectException("id war null");
-    if (cache != null && cache.containsKey(id))
+    if (cache != null && (cache.containsKey(id) > -1))
       return (Entity)cache.get(id);  // wenn cache gesetzt, evtl. kein roundtrip zur Datenbank
 
     Statement stmt=null;Connection con=getPooledCon();
@@ -433,9 +433,13 @@ public class Database implements StorageObject {
           if (rs.next())
             count = rs.getInt(1);
           rs.close();
-        }
-        else
+        } else {
           theLog.printError("Mh. Konnte nicht zaehlen: " + countSql);
+        }
+      }
+      //nothing in the table: return null
+      if(count<=0){
+        return null;
       }
       // hier select
       rs = executeSql(stmt, selectSql.toString());
@@ -516,7 +520,8 @@ public class Database implements StorageObject {
           theResultHash.put(metadataFields.get(i), theResult);
         }
       }
-      if (cache != null && theResultHash.containsKey(thePKeyName) && cache.containsKey((String)theResultHash.get(thePKeyName))) {
+      if (cache != null && theResultHash.containsKey(thePKeyName) &&
+            (cache.containsKey((String)theResultHash.get(thePKeyName)) > -1)) {
         //theLog.printDebugInfo("CACHE: (out) "+ theResultHash.get(thePKeyName)+ " :"+theTable);
         returnEntity = (Entity)cache.get((String)theResultHash.get(thePKeyName));
       }
@@ -1118,6 +1123,7 @@ public class Database implements StorageObject {
   void throwStorageObjectException (String message) throws StorageObjectException {
     _throwStorageObjectException(null, message);
   }
+  
 }
 
 
diff --git a/source/mir/storage/DatabaseCache.java b/source/mir/storage/DatabaseCache.java
new file mode 100755 (executable)
index 0000000..0b4b738
--- /dev/null
@@ -0,0 +1,118 @@
+package mir.storage;
+
+import java.util.*;
+
+public class DatabaseCache {
+  private final ArrayList _cache;
+  private int _counter;
+  private final int _max;
+  
+  public DatabaseCache(int i_max){
+    _counter = 0;
+    _max = i_max;
+    _cache = new ArrayList(_max);
+  }
+  
+  public DatabaseCache(){
+    _counter = -1;
+    _max = 100;
+    _cache = new ArrayList(_max);
+  }
+  
+  public synchronized void put(String key, Object value){
+    if(_counter >=_max){
+      _cache.remove(0);
+      _cache.trimToSize();
+      _counter --;
+      System.out.println("put: remove " + _counter);
+    }
+    _cache.add(new Entry(key,value));
+    _counter ++;
+    System.out.println("put: put " + _counter);
+  }
+  
+  public synchronized void clear(){
+    _cache.clear();
+  }
+      
+  public int containsKey(String key){
+    for(int i = 0; i<_cache.size(); i++){
+      if( ((Entry)_cache.get(i)).getKey().equals(key) )
+        return i;
+    }
+    return -1;
+  }
+  
+  public int containsValue(Object o){
+    for(int i = 0; i<_cache.size(); i++){
+      if(((Entry)_cache.get(i)).getValue().equals(o) )
+        return i;
+    }
+    return -1;
+  }
+  
+  public Object get(String key){
+    for(int i = 0; i<_cache.size(); i++){
+      if( ((Entry)_cache.get(i)).getKey(key).equals(key) )
+          ((Entry)_cache.get(i)).getValue();
+    }
+    return null;
+  }
+  
+  public synchronized boolean remove(String key){
+    int i = containsKey(key);
+    if(i==-1){
+      return false;
+    }
+    _cache.remove(i);
+    _cache.trimToSize();
+    _counter --;
+    return true;
+  }
+  
+  public int size(){
+    return _counter;
+  }
+  
+  private class Entry {
+    private String _key;
+    private Object _value;
+    
+    public Entry(String i_key, Object i_value){
+      _key = i_key;
+      _value = i_value;
+    }
+        
+    public void put(String i_key, Object i_value){
+      _key = i_key;
+      _value = i_value;
+    }
+    
+    public Object getValue(String i_key){
+      if(i_key.equals(_key)){
+        return _value;
+      } else {
+        return null;
+      }
+    }
+    
+    public Object getValue(){
+      return _value;
+    }
+    
+    public String getKey(Object i_o){
+      if(i_o.equals(_value)){
+        return _key;
+      } else {
+        return null;
+      }
+    }
+
+    public String getKey(){
+        return _key;
+    }
+  }//Entry
+
+}
+
+
index cb789d8..e01dd06 100755 (executable)
@@ -19,26 +19,26 @@ import mir.misc.*;
 
 public class DatabaseBreaking extends Database implements StorageObject{
 
-       private static DatabaseBreaking instance;
-
-       public static DatabaseBreaking getInstance() throws StorageObjectException {
-               if (instance == null) {
-                       instance = new DatabaseBreaking();
-                       instance.myselfDatabase = instance;
-               }
-               return instance;
-       }
-
-       private DatabaseBreaking() throws StorageObjectException
-       {
-               super();
-               this.cache = new HashMap();
-               this.theTable="breaking";
-               try {
-                       this.theEntityClass = Class.forName("mircoders.entity.EntityBreaking");
-               }
-               catch (Exception e) { throw new StorageObjectException(e.toString());   }
-       }
+  private static DatabaseBreaking instance;
+
+  public static DatabaseBreaking getInstance() throws StorageObjectException {
+    if (instance == null) {
+      instance = new DatabaseBreaking();
+      instance.myselfDatabase = instance;
+    }
+    return instance;
+  }
+
+  private DatabaseBreaking() throws StorageObjectException
+  {
+    super();
+    this.cache = new DatabaseCache(4);
+    this.theTable="breaking";
+    try {
+      this.theEntityClass = Class.forName("mircoders.entity.EntityBreaking");
+    }
+    catch (Exception e) { throw new StorageObjectException(e.toString()); }
+  }
 
 
 }
index 0478cea..2e3a4dc 100755 (executable)
@@ -43,6 +43,7 @@ public class DatabaseContent extends Database implements StorageObject {
     super();
     this.theTable="content";
     this.theCoreTable="media";
+               
     relationComments = new EntityRelation("id", "to_media", DatabaseComment.getInstance(), EntityRelation.TO_MANY);
     relationFeature = new EntityRelation("id", "to_feature", DatabaseFeature.getInstance(), EntityRelation.TO_ONE);
     try { this.theEntityClass = Class.forName("mircoders.entity.EntityContent"); }
index 2ae9d15..201e931 100755 (executable)
@@ -19,33 +19,33 @@ import mir.misc.*;
 
 public class DatabaseFeature extends Database implements StorageObject{
 
-       private static DatabaseFeature instance;
-
-       public static DatabaseFeature getInstance() throws StorageObjectException {
-                       if (instance == null) {
-               instance = new DatabaseFeature();
-               instance.myselfDatabase = instance;
-                       }
-               return instance;
-       }
-
-       private DatabaseFeature() throws StorageObjectException
-       {
-               super();
-               this.cache = new HashMap();
-               this.hasTimestamp = false;
-               this.theTable="feature";
-
-               try {
-                               this.theEntityClass = Class.forName("mircoders.entity.EntityFeature");
-               }
-               catch (Exception e) {
-                               throw new StorageObjectException(e.toString());
-               }
-       }
-
-       public SimpleList getPopupData() throws StorageObjectException {
-               return getPopupData("title",true);
-       }
+  private static DatabaseFeature instance;
+
+  public static DatabaseFeature getInstance() throws StorageObjectException {
+      if (instance == null) {
+    instance = new DatabaseFeature();
+    instance.myselfDatabase = instance;
+      }
+    return instance;
+  }
+
+  private DatabaseFeature() throws StorageObjectException
+  {
+    super();
+    this.cache = new DatabaseCache(10);
+    this.hasTimestamp = false;
+    this.theTable="feature";
+
+    try {
+        this.theEntityClass = Class.forName("mircoders.entity.EntityFeature");
+    }
+    catch (Exception e) {
+        throw new StorageObjectException(e.toString());
+    }
+  }
+
+  public SimpleList getPopupData() throws StorageObjectException {
+    return getPopupData("title",true);
+  }
 
 }
index c16f947..de43602 100755 (executable)
@@ -33,7 +33,7 @@ public class DatabaseMedia extends Database implements StorageObject{
   private DatabaseMedia() throws StorageObjectException
   {
     super();
-    this.cache = new HashMap();
+    this.cache = new DatabaseCache(100);
     this.hasTimestamp = false;
     this.theTable="media*";
     relationMediaType = new EntityRelation("to_media_type", "id", DatabaseMediaType.getInstance(), EntityRelation.TO_ONE);
index 2080e24..71d1273 100755 (executable)
@@ -34,7 +34,7 @@ public class DatabaseMediaType extends Database implements StorageObject{
   {
     super();
     this.hasTimestamp = false;
-    this.cache = new HashMap();
+    this.cache = new DatabaseCache(20);
     this.theTable="media_type";
     try {
       this.theEntityClass = Class.forName("mir.entity.GenericEntity");
index 2cf9bff..6f52fc0 100755 (executable)
@@ -24,26 +24,26 @@ import mir.misc.*;
 
 public class DatabaseMediafolder extends Database implements StorageObject{
 
-       private static DatabaseMediafolder instance;
-
-       public static DatabaseMediafolder getInstance() throws StorageObjectException {
-               if (instance == null) {
-                       instance = new DatabaseMediafolder();
-                       instance.myselfDatabase = instance;
-               }
-               return instance;
-       }
-
-       private DatabaseMediafolder() throws StorageObjectException
-       {
-               super();
-               this.hasTimestamp = false;
-               this.cache = new HashMap();
-               this.theTable="media_folder";
-       }
-
-       public SimpleList getPopupData() throws StorageObjectException {
-               return getPopupData("name",true);
-       }
+  private static DatabaseMediafolder instance;
+
+  public static DatabaseMediafolder getInstance() throws StorageObjectException {
+    if (instance == null) {
+      instance = new DatabaseMediafolder();
+      instance.myselfDatabase = instance;
+    }
+    return instance;
+  }
+
+  private DatabaseMediafolder() throws StorageObjectException
+  {
+    super();
+    this.hasTimestamp = false;
+    this.cache = new DatabaseCache(20);
+    this.theTable="media_folder";
+  }
+
+  public SimpleList getPopupData() throws StorageObjectException {
+    return getPopupData("name",true);
+  }
 
 }
index 2c96902..ad963ad 100755 (executable)
@@ -24,22 +24,22 @@ import mir.misc.*;
 
 public class DatabaseMessages extends Database implements StorageObject{
 
-       private static DatabaseMessages instance;
-
-       public static DatabaseMessages getInstance() throws StorageObjectException {
-               if (instance == null) {
-                       instance = new DatabaseMessages();
-                       instance.myselfDatabase = instance;
-               }
-               return instance;
-       }
-
-       private DatabaseMessages() throws StorageObjectException
-       {
-               super();
-               this.cache = new HashMap();
-               this.theTable="messages";
-       }
+  private static DatabaseMessages instance;
+
+  public static DatabaseMessages getInstance() throws StorageObjectException {
+    if (instance == null) {
+      instance = new DatabaseMessages();
+      instance.myselfDatabase = instance;
+    }
+    return instance;
+  }
+
+  private DatabaseMessages() throws StorageObjectException
+  {
+    super();
+    this.cache = new DatabaseCache(10);
+    this.theTable="messages";
+  }
 
 
 }
index 481b844..c36a9cb 100755 (executable)
@@ -19,34 +19,34 @@ import mir.misc.*;
 
 public class DatabaseTopics extends Database implements StorageObject{
 
-       private static DatabaseTopics instance;
-
-       public static DatabaseTopics getInstance() throws StorageObjectException {
-                       if (instance == null) {
-               instance = new DatabaseTopics();
-               instance.myselfDatabase = instance;
-                       }
-               return instance;
-       }
-
-       private DatabaseTopics() throws StorageObjectException
-       {
+  private static DatabaseTopics instance;
+
+  public static DatabaseTopics getInstance() throws StorageObjectException {
+      if (instance == null) {
+    instance = new DatabaseTopics();
+    instance.myselfDatabase = instance;
+      }
+    return instance;
+  }
+
+  private DatabaseTopics() throws StorageObjectException
+  {
           super();
-                       this.cache = new HashMap();
-                       this.hasTimestamp = false;
-                       this.theTable="topic";
-                       try {
-               this.theEntityClass = Class.forName("mircoders.entity.EntityTopics");
-                       }
-                       catch (Exception e) {
-               throw new StorageObjectException(e.toString());
-                       }
-
-       }
-
-       public SimpleList getPopupData() throws StorageObjectException {
-               return getPopupData("title",true);
-       }
+      this.cache = new DatabaseCache(20);
+      this.hasTimestamp = false;
+      this.theTable="topic";
+      try {
+    this.theEntityClass = Class.forName("mircoders.entity.EntityTopics");
+      }
+      catch (Exception e) {
+    throw new StorageObjectException(e.toString());
+      }
+
+  }
+
+  public SimpleList getPopupData() throws StorageObjectException {
+    return getPopupData("title",true);
+  }