jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
Subject svn commit: r418263 - in /jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state: ItemState.java ItemStateReferenceCache.java MLRUItemStateCache.java NodeState.java PropertyState.java
Date Fri, 30 Jun 2006 13:26:16 GMT
Author: tripod
Date: Fri Jun 30 06:26:16 2006
New Revision: 418263

URL: http://svn.apache.org/viewvc?rev=418263&view=rev
Log:
JCR-471: Create resource sensitive cache for item states

Added:
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java?rev=418263&r1=418262&r2=418263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
Fri Jun 30 06:26:16 2006
@@ -449,6 +449,13 @@
         }
     }
 
+    /**
+     * Returns the approximate memory consuption of this state.
+     *
+     * @return the approximate memory consuption of this state.
+     */
+    public abstract long getMemoryConsumption();
+    
     //----------------------------------------------------< ItemStateListener >
     /**
      * {@inheritDoc}

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java?rev=418263&r1=418262&r2=418263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemStateReferenceCache.java
Fri Jun 30 06:26:16 2006
@@ -67,7 +67,7 @@
      * cache.
      */
     public ItemStateReferenceCache() {
-        this(new LRUItemStateCache());
+        this(new MLRUItemStateCache());
     }
 
     /**
@@ -99,7 +99,7 @@
         cache.retrieve(id);
 
         // retrieve from primary cache
-        return (ItemState) refs.get(id);
+        return refs.get(id);
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java?rev=418263&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
(added)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
Fri Jun 30 06:26:16 2006
@@ -0,0 +1,199 @@
+/*
+ * $URL$
+ * $Id$
+ *
+ * Copyright 1997-2006 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * This software is the confidential and proprietary information of
+ * Day Management AG, ("Confidential Information"). You shall not
+ * disclose such Confidential Information and shall use it only in
+ * accordance with the terms of the license agreement you entered into
+ * with Day.
+ */
+package org.apache.jackrabbit.core.state;
+
+import org.apache.commons.collections.map.LRUMap;
+import org.apache.commons.collections.map.LinkedMap;
+import org.apache.jackrabbit.core.ItemId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+import java.util.Collections;
+import java.util.Collection;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+/**
+ * An <code>ItemStateCache</code> implementation that internally uses a
+ * {@link LRUMap} to maintain a cache of <code>ItemState</code> objects. the
+ * cache uses a rough estimate of the memory consuption of the cache item
+ * states for calculating the maximum number of entries.
+ */
+public class MLRUItemStateCache implements ItemStateCache {
+    /** Logger instance */
+    private static Logger log = LoggerFactory.getLogger(LRUItemStateCache.class);
+
+    /** default maximum memory to use */
+    public static final int DEFAULT_MAX_MEM = 8 * 1024 * 1024;
+
+    /** the amount of memory the entries use */
+    private long totalMem;
+
+    /** the maximum of memory the cache may use */
+    private final long maxMem;
+
+    /**
+     * A cache for <code>ItemState</code> instances
+     */
+    private final LinkedMap cache = new LinkedMap();
+
+    /**
+     * Constructs a new, empty <code>ItemStateCache</code> with a maximum amount
+     * of memory of {@link #DEFAULT_MAX_MEM}.
+     */
+    public MLRUItemStateCache() {
+        this(DEFAULT_MAX_MEM);
+    }
+
+    /**
+     * Constructs a new, empty <code>ItemStateCache</code> with the specified
+     * maximum memory.
+     *
+     * @param maxMem the maximum amount of memory this cache may use.
+     */
+    public MLRUItemStateCache(int maxMem) {
+        this.maxMem = maxMem;
+    }
+
+    //-------------------------------------------------------< ItemStateCache >
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isCached(ItemId id) {
+        synchronized (cache) {
+            return cache.containsKey(id);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public ItemState retrieve(ItemId id) {
+        synchronized (cache) {
+            Entry entry = (Entry) cache.remove(id);
+            if (entry != null) {
+                // 'touch' item, by adding at end of list
+                cache.put(id, entry);
+                return entry.state;
+            } else {
+                return null;
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void cache(ItemState state) {
+        synchronized (cache) {
+            ItemId id = state.getId();
+            if (cache.containsKey(id)) {
+                log.warn("overwriting cached entry " + id);
+                evict(id);
+            }
+            Entry entry = new Entry(state);
+            cache.put(id, entry);
+            totalMem += entry.size;
+            // remove items, if too many
+            while (totalMem > maxMem) {
+                id = (ItemId) cache.firstKey();
+                evict(id);
+            }
+            if (log.isDebugEnabled()) {
+                log.info(this + " size=" + cache.size() + ", " + totalMem + "/" + maxMem);
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void evict(ItemId id) {
+        synchronized (cache) {
+            Entry entry = (Entry) cache.remove(id);
+            if (entry != null) {
+                totalMem -= entry.size;
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void evictAll() {
+        synchronized (cache) {
+            cache.clear();
+            totalMem = 0;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEmpty() {
+        synchronized (cache) {
+            return cache.isEmpty();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public int size() {
+        synchronized (cache) {
+            return cache.size();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Set keySet() {
+        synchronized (cache) {
+            return Collections.unmodifiableSet(cache.keySet());
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Collection values() {
+        synchronized (cache) {
+            ArrayList list = new ArrayList(cache.size());
+            Iterator iter = cache.values().iterator();
+            while (iter.hasNext()) {
+                Entry entry = (Entry) iter.next();
+                list.add(entry.state);
+            }
+            return list;
+        }
+    }
+
+    /**
+     * Internal cache entry
+     */
+    private static class Entry {
+
+        private final ItemState state;
+
+        private final long size;
+
+        public Entry(ItemState state) {
+            this.state = state;
+            this.size = 64 + state.getMemoryConsumption();
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/MLRUItemStateCache.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url rev

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java?rev=418263&r1=418262&r2=418263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
Fri Jun 30 06:26:16 2006
@@ -773,6 +773,33 @@
     }
 
     //-------------------------------------------------< misc. helper methods >
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getMemoryConsumption() {
+        /*
+        private QName nodeTypeName;
+        private Set mixinTypeNames = Collections.EMPTY_SET;
+        private NodeId id;
+        private NodeId parentId;
+        private NodeDefId defId;
+        private ChildNodeEntries childNodeEntries = new ChildNodeEntries();
+        private boolean sharedChildNodeEntries = false;
+        private HashSet propertyNames = new HashSet();
+        private boolean sharedPropertyNames = false;
+
+        we assume an average QName localname of 30 chars.
+        NodeId = 8 + UUID(24) + hashcode(4) = 36
+        QName = 8 + hash(4) + string(38+2*len) + namespace(4) + localName(38+2*len) ~ 250
+        NodeDefId = 8 + id(4) = 12
+        ChildNodeEntries = 8 + n * (name(256) + index(4) + id(36) + hashentry(16)) ~ n*300
+        PropNames = 8 + n * ( name(250))
+        */
+        return 350 + mixinTypeNames.size() * 250 + childNodeEntries.size() * 300
+                + propertyNames.size() * 250;
+    }
+
     /**
      * Notify the listeners that a child node entry has been added
      */

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java?rev=418263&r1=418262&r2=418263&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java
Fri Jun 30 06:26:16 2006
@@ -226,6 +226,26 @@
         return values;
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public long getMemoryConsumption() {
+        /*
+        private PropertyId id;
+        private InternalValue[] values;
+        private int type;
+        private boolean multiValued;
+        private PropDefId defId;
+
+        we assume an average QName localname of 30 chars.
+        PropertyId = 8 + nodeId(36) * name(250) + hash(4) ~ 300;
+        NodeDefId = 8 + id(4) = 12
+        InternalValue = 8 + n * (values) ~ 8 + n*100;
+        value=approx 100 bytes.
+        */
+        return 350 + values.length * 100;
+    }
+    
     //-------------------------------------------------< Serializable support >
     private void writeObject(ObjectOutputStream out) throws IOException {
         // important: fields must be written in same order as they are



Mime
View raw message