You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2007/03/18 18:33:16 UTC
svn commit: r519644 - in /db/derby/code/trunk/java/engine/org/apache/derby:
diag/StatementCache.java iapi/services/cache/CacheManager.java
impl/services/cache/CachedItem.java impl/services/cache/Clock.java
Author: kahatlen
Date: Sun Mar 18 10:33:15 2007
New Revision: 519644
URL: http://svn.apache.org/viewvc?view=rev&rev=519644
Log:
DERBY-2114: Let Clock embed a HashMap rather than inherit from Hashtable
Patch contributed by Dyre Tjeldvoll.
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/diag/StatementCache.java
db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/CacheManager.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/CachedItem.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/Clock.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/diag/StatementCache.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/diag/StatementCache.java?view=diff&rev=519644&r1=519643&r2=519644
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/diag/StatementCache.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/diag/StatementCache.java Sun Mar 18 10:33:15 2007
@@ -39,11 +39,12 @@
import java.sql.Timestamp;
import org.apache.derby.impl.sql.conn.CachedStatement;
-import org.apache.derby.impl.services.cache.CachedItem;
import java.util.Vector;
-import java.util.Enumeration;
+
+import java.util.Iterator;
+import java.util.Collection;
/**
StatementCache is a virtual table that shows the contents of the SQL statement cache.
@@ -95,24 +96,19 @@
public StatementCache() throws SQLException {
org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext lcc =
- (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext) ConnectionUtil.getCurrentLCC();
+ (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext)
+ ConnectionUtil.getCurrentLCC();
if (lcc.statementCache != null) {
-
- java.util.Hashtable stmtCache = (java.util.Hashtable) lcc.statementCache;
- data = new Vector(stmtCache.size());
- for (Enumeration e = stmtCache.elements(); e.hasMoreElements(); ) {
-
-
- CachedItem ci = (CachedItem) e.nextElement();
- CachedStatement cs = (CachedStatement) ci.getEntry();
-
- GenericPreparedStatement ps = (GenericPreparedStatement) cs.getPreparedStatement();
-
+ final Collection values = lcc.statementCache.values();
+ data = new Vector(values.size());
+ for (Iterator i = values.iterator(); i.hasNext(); ) {
+ final CachedStatement cs = (CachedStatement) i.next();
+ final GenericPreparedStatement ps =
+ (GenericPreparedStatement) cs.getPreparedStatement();
data.addElement(ps);
}
}
-
}
public boolean next() {
Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/CacheManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/CacheManager.java?view=diff&rev=519644&r1=519643&r2=519644
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/CacheManager.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/cache/CacheManager.java Sun Mar 18 10:33:15 2007
@@ -27,6 +27,8 @@
import org.apache.derby.iapi.util.Matchable;
import org.apache.derby.iapi.util.Operator;
+import java.util.Collection;
+
public interface CacheManager {
/**
@@ -303,4 +305,13 @@
* @param operator
*/
public void scan( Matchable filter, Operator operator);
+
+ /**
+ * Return a Collection of the Cacheables currently in the
+ * cache. The Collection should be a copy so that external
+ * synchronization isn't required.
+ *
+ * @return a Collection of all the elements in the cache
+ */
+ public Collection values();
}
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/CachedItem.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/CachedItem.java?view=diff&rev=519644&r1=519643&r2=519644
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/CachedItem.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/CachedItem.java Sun Mar 18 10:33:15 2007
@@ -65,7 +65,7 @@
@see org.apache.derby.impl.services.cache
@see Cacheable
*/
-public final class CachedItem {
+final class CachedItem {
/*
** Fields
*/
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/Clock.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/Clock.java?view=diff&rev=519644&r1=519643&r2=519644
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/Clock.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/cache/Clock.java Sun Mar 18 10:33:15 2007
@@ -41,7 +41,9 @@
import org.apache.derby.iapi.reference.SQLState;
import java.util.ArrayList;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Collection;
import java.util.Properties;
@@ -97,12 +99,13 @@
*/
-final class Clock extends Hashtable implements CacheManager, Serviceable {
+final class Clock implements CacheManager, Serviceable {
/*
** Fields
*/
public final CacheStat stat;
+ private final HashMap cache_;
private DaemonService cleaner; // the background worker thread who is going to
// do pre-flush for this cache.
private final ArrayList holders;
@@ -146,16 +149,12 @@
it will not grow. If the cache is full, an exception will be thrown
*/
- Clock(CacheableFactory holderFactory,
- String name,
- int initialSize,
- long maximumSize,
- boolean useByteCount)
- {
- super(initialSize, (float) 0.95);
+ Clock(CacheableFactory holderFactory, String name,
+ int initialSize, long maximumSize, boolean useByteCount) {
+ cache_ = new HashMap(initialSize, (float) 0.95);
this.maximumSize = maximumSize;
this.holderFactory = holderFactory;
- this.useByteCount = useByteCount;
+ this.useByteCount = useByteCount;
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(ClockFactory.CacheTrace)) {
@@ -224,7 +223,7 @@
if (!active)
return null;
- item = (CachedItem) get(key);
+ item = (CachedItem) cache_.get(key);
if (item != null) {
item.keepAfterSearch();
@@ -262,7 +261,7 @@
SanityManager.ASSERT(item != null, "found null item");
synchronized (this) {
- CachedItem inCacheItem = (CachedItem) get(key);
+ CachedItem inCacheItem = (CachedItem) cache_.get(key);
if (inCacheItem != null) {
// some-one beat us to adding an item into the cache,
@@ -273,14 +272,16 @@
item.keepAfterSearch();
} else {
// yes, we really are the ones to add it
- put(key, item);
+ cache_.put(key, item);
add = true;
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON("memoryLeakTrace")) {
- if (size() > ((11 * maximumSize) / 10))
- System.out.println("memoryLeakTrace:Cache:" + name + " " + size());
+ if (cache_.size() > ((11 * maximumSize) / 10))
+ System.out.println
+ ("memoryLeakTrace:Cache:" + name +
+ " " + cache_.size());
}
}
}
@@ -341,7 +342,7 @@
if (!active)
return null;
- item = (CachedItem) get(key);
+ item = (CachedItem) cache_.get(key);
if (item == null) {
stat.findCachedMiss++;
@@ -390,7 +391,7 @@
if( keys[i] == null)
return;
- item = (CachedItem) get(keys[i]);
+ item = (CachedItem) cache_.get(keys[i]);
if( null != item)
item.setUsed( true);
}
@@ -423,21 +424,23 @@
if (!active)
return null;
- if (get(key) != null) {
+ if (cache_.get(key) != null) {
item.unkeepForCreate();
throw StandardException.newException(SQLState.OBJECT_EXISTS_IN_CACHE, this.name, key);
}
- put(key, item);
+ cache_.put(key, item);
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON("memoryLeakTrace")) {
- if (size() > ((11 * maximumSize) / 10))
- System.out.println("memoryLeakTrace:Cache:" + name + " " + size());
+ if (cache_.size() > ((11 * maximumSize) / 10))
+ System.out.println
+ ("memoryLeakTrace:Cache:" + name + " " +
+ cache_.size());
}
}
}
@@ -481,7 +484,7 @@
synchronized (this) {
- item = (CachedItem) get(entry.getIdentity());
+ item = (CachedItem) cache_.get(entry.getIdentity());
if (SanityManager.DEBUG) {
SanityManager.ASSERT(item != null, "item null");
@@ -493,7 +496,7 @@
if (removeItem) {
- remove(entry.getIdentity());
+ cache_.remove(entry.getIdentity());
// we keep the item here to stop another thread trying to evict it
// while we are destroying it.
@@ -529,7 +532,7 @@
if (removeItem) {
- remove(item.getEntry().getIdentity());
+ cache_.remove(item.getEntry().getIdentity());
// we keep the item here to stop another thread trying to evict it
// while we are destroying it.
@@ -568,7 +571,7 @@
- item = (CachedItem) get(entry.getIdentity());
+ item = (CachedItem) cache_.get(entry.getIdentity());
if (SanityManager.DEBUG) {
SanityManager.ASSERT(item != null);
@@ -582,7 +585,7 @@
removeNow = item.unkeep();
if (removeNow) {
- remove(entry.getIdentity());
+ cache_.remove(entry.getIdentity());
item.keepForClean();
}
}
@@ -801,7 +804,7 @@
boolean notifyWaiters;
synchronized (this) {
- Object removed = remove(key);
+ Object removed = cache_.remove(key);
if (SanityManager.DEBUG) {
SanityManager.ASSERT(removed == item);
}
@@ -810,7 +813,7 @@
// put the actual key into the hash table, not the one that was passed in
// for the find or create. This is because the caller may re-use the key
// for another cache operation, which would corrupt our hashtable
- put(entry.getIdentity(), item);
+ cache_.put(entry.getIdentity(), item);
if( useByteCount)
currentByteCount += ((SizedCacheable) entry).getSize() - origEntrySize;
item.setValidState(true);
@@ -1292,7 +1295,7 @@
if( useByteCount)
shrink = ((SizedCacheable) item.getEntry()).getSize();
- remove(item.getEntry().getIdentity());
+ cache_.remove(item.getEntry().getIdentity());
item.setValidState(false);
validItemCount--;
item.getEntry().clearIdentity();
@@ -1845,4 +1848,28 @@
clockHand = validItems + 1;
} // end of trimToSize
+
+ /**
+ * Tell if a key exists in the cache.
+ * @param k the key to test for
+ * @return true if k is a key in the cache
+ */
+ public synchronized boolean containsKey(Object k) {
+ return cache_.containsKey(k);
+ }
+
+ /**
+ * Return a Collection of the Cacheables currently in the
+ * cache. The Collection is a snapshot (copy) so external
+ * synchronization isn't required. Part of the CacheManager
+ * interface.
+ * @return a Collection of the cache elements.
+ */
+ public synchronized Collection values() {
+ ArrayList al = new ArrayList();
+ for (Iterator i = cache_.values().iterator(); i.hasNext();){
+ al.add(((CachedItem)i.next()).getEntry());
+ }
+ return al;
+ }
}