From 9413f754e5464f9e3b09290c6d49bbbab1897905 Mon Sep 17 00:00:00 2001 From: idfx Date: Wed, 6 Feb 2002 14:22:39 +0000 Subject: [PATCH] a new db-cache with max-load, not documented yet --- source/mir/servlet/ServletModule.java | 3 +- source/mir/storage/Database.java | 16 ++- source/mir/storage/DatabaseCache.java | 118 ++++++++++++++++++++++ source/mircoders/storage/DatabaseBreaking.java | 40 ++++---- source/mircoders/storage/DatabaseContent.java | 1 + source/mircoders/storage/DatabaseFeature.java | 56 +++++----- source/mircoders/storage/DatabaseMedia.java | 2 +- source/mircoders/storage/DatabaseMediaType.java | 2 +- source/mircoders/storage/DatabaseMediafolder.java | 42 ++++---- source/mircoders/storage/DatabaseMessages.java | 32 +++--- source/mircoders/storage/DatabaseTopics.java | 54 +++++----- 11 files changed, 246 insertions(+), 120 deletions(-) create mode 100755 source/mir/storage/DatabaseCache.java diff --git a/source/mir/servlet/ServletModule.java b/source/mir/servlet/ServletModule.java index 8631ddb7..eab5dc53 100755 --- a/source/mir/servlet/ServletModule.java +++ b/source/mir/servlet/ServletModule.java @@ -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());} } /** diff --git a/source/mir/storage/Database.java b/source/mir/storage/Database.java index f4aa63fd..575d3743 100755 --- a/source/mir/storage/Database.java +++ b/source/mir/storage/Database.java @@ -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 index 00000000..0b4b7382 --- /dev/null +++ b/source/mir/storage/DatabaseCache.java @@ -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 + +} + + diff --git a/source/mircoders/storage/DatabaseBreaking.java b/source/mircoders/storage/DatabaseBreaking.java index cb789d84..e01dd061 100755 --- a/source/mircoders/storage/DatabaseBreaking.java +++ b/source/mircoders/storage/DatabaseBreaking.java @@ -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()); } + } } diff --git a/source/mircoders/storage/DatabaseContent.java b/source/mircoders/storage/DatabaseContent.java index 0478ceae..2e3a4dcf 100755 --- a/source/mircoders/storage/DatabaseContent.java +++ b/source/mircoders/storage/DatabaseContent.java @@ -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"); } diff --git a/source/mircoders/storage/DatabaseFeature.java b/source/mircoders/storage/DatabaseFeature.java index 2ae9d15a..201e9314 100755 --- a/source/mircoders/storage/DatabaseFeature.java +++ b/source/mircoders/storage/DatabaseFeature.java @@ -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); + } } diff --git a/source/mircoders/storage/DatabaseMedia.java b/source/mircoders/storage/DatabaseMedia.java index c16f9471..de43602f 100755 --- a/source/mircoders/storage/DatabaseMedia.java +++ b/source/mircoders/storage/DatabaseMedia.java @@ -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); diff --git a/source/mircoders/storage/DatabaseMediaType.java b/source/mircoders/storage/DatabaseMediaType.java index 2080e24c..71d12737 100755 --- a/source/mircoders/storage/DatabaseMediaType.java +++ b/source/mircoders/storage/DatabaseMediaType.java @@ -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"); diff --git a/source/mircoders/storage/DatabaseMediafolder.java b/source/mircoders/storage/DatabaseMediafolder.java index 2cf9bff9..6f52fc09 100755 --- a/source/mircoders/storage/DatabaseMediafolder.java +++ b/source/mircoders/storage/DatabaseMediafolder.java @@ -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); + } } diff --git a/source/mircoders/storage/DatabaseMessages.java b/source/mircoders/storage/DatabaseMessages.java index 2c969021..ad963ad2 100755 --- a/source/mircoders/storage/DatabaseMessages.java +++ b/source/mircoders/storage/DatabaseMessages.java @@ -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"; + } } diff --git a/source/mircoders/storage/DatabaseTopics.java b/source/mircoders/storage/DatabaseTopics.java index 481b8442..c36a9cb1 100755 --- a/source/mircoders/storage/DatabaseTopics.java +++ b/source/mircoders/storage/DatabaseTopics.java @@ -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); + } -- 2.11.0