Author: sklevenz
Date: Mon Feb 22 13:32:51 2010
New Revision: 912557

URL: http://svn.apache.org/viewvc?rev=912557&view=rev
Log:
LRU Client Runtime Cache introduced
https://issues.apache.org/jira/browse/CMIS-124

Added:
    
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/
    
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java
   (with props)
Modified:
    
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/main/java/org/apache/opencmis/client/runtime/cache/CacheImpl.java

Modified: 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/main/java/org/apache/opencmis/client/runtime/cache/CacheImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/main/java/org/apache/opencmis/client/runtime/cache/CacheImpl.java?rev=912557&r1=912556&r2=912557&view=diff
==============================================================================
--- 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/main/java/org/apache/opencmis/client/runtime/cache/CacheImpl.java
 (original)
+++ 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/main/java/org/apache/opencmis/client/runtime/cache/CacheImpl.java
 Mon Feb 22 13:32:51 2010
@@ -19,39 +19,84 @@
 package org.apache.opencmis.client.runtime.cache;
 
 import java.io.Serializable;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import org.apache.opencmis.client.api.CmisObject;
-import org.apache.opencmis.commons.exceptions.CmisRuntimeException;
 
+/**
+ * Non synchronized cache implementation. The cache is limited to a specific
+ * size of entries and works in a LRU mode
+ */
 public class CacheImpl implements Cache, Serializable {
 
+       private LinkedHashMap<String, CmisObject> idMap = null;
+       private LinkedHashMap<String, CmisObject> pathMap = null;
+
+       private static final float hashTableLoadFactor = 0.75f;
+
+       private int cacheSize = 1000; // default
+
        /**
         * serialization
         */
        private static final long serialVersionUID = 1978445442452564094L;
 
-       public boolean containsId(String objectId){
-               throw new CmisRuntimeException("not implemented");
+       public CacheImpl() {
+               this.idMap = this.createLruCache();
+               this.pathMap = this.createLruCache();
+       }
+
+       public CacheImpl(int cacheSize) {
+               this.cacheSize = cacheSize;
+
+               this.idMap = this.createLruCache();
+               this.pathMap = this.createLruCache();
+       }
+
+       private LinkedHashMap<String, CmisObject> createLruCache() {
+               int hashTableCapacity = (int) Math
+                               .ceil(cacheSize / hashTableLoadFactor) + 1;
+
+               LinkedHashMap<String, CmisObject> map = new 
LinkedHashMap<String, CmisObject>(
+                               hashTableCapacity, hashTableLoadFactor) {
+
+                       // (an anonymous inner class)
+                       private static final long serialVersionUID = 
-3928413932856712672L;
+
+                       @Override
+                       protected boolean removeEldestEntry(
+                                       Map.Entry<String, CmisObject> eldest) {
+                               return size() > CacheImpl.this.cacheSize;
+                       }
+               };
+               return map;
+       }
+
+       public boolean containsId(String objectId) {
+               return this.idMap.containsKey(objectId);
        }
 
        public void clear() {
-               throw new CmisRuntimeException("not implemented");
+               this.idMap.clear();
+               this.pathMap.clear();
        }
 
        public boolean containsPath(String path) {
-               throw new CmisRuntimeException("not implemented");
+               return this.pathMap.containsKey(path);
        }
 
        public CmisObject get(String objectId) {
-               throw new CmisRuntimeException("not implemented");
+               return this.idMap.get(objectId);
        }
 
        public CmisObject getByPath(String path) {
-               throw new CmisRuntimeException("not implemented");
+               return this.pathMap.get(path);
        }
 
        public void put(CmisObject object) {
-               throw new CmisRuntimeException("not implemented");
+               this.idMap.put(object.getId(), object);
+               this.pathMap.put(object.getPath(), object);
        }
-       
+
 }

Added: 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java?rev=912557&view=auto
==============================================================================
--- 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java
 (added)
+++ 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java
 Mon Feb 22 13:32:51 2010
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.opencmis.client.runtime.misc;
+
+import junit.framework.Assert;
+
+import org.apache.opencmis.client.api.CmisObject;
+import org.apache.opencmis.client.runtime.cache.Cache;
+import org.apache.opencmis.client.runtime.cache.CacheImpl;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CacheTest {
+
+       @Before
+       public void setup() {
+       }
+
+       @Test
+       public void cacheSingleobject() {
+               Cache cache = new CacheImpl();
+
+               String id = "1";
+               String path = "/1";
+
+               // add object
+               CmisObject obj1 = this.createCmisObject(id, path);
+               cache.put(obj1);
+
+               // access object
+               Assert.assertTrue(cache.containsId(id));
+               Assert.assertTrue(cache.containsPath(path));
+
+               // access object
+               CmisObject obj2 = cache.get(id);
+               Assert.assertEquals(obj1, obj2);
+               CmisObject obj3 = cache.getByPath(path);
+               Assert.assertEquals(obj1, obj3);
+
+               // clear cache
+               cache.clear();
+
+               // access object (not found)
+               Assert.assertFalse(cache.containsId(id));
+               Assert.assertFalse(cache.containsPath(path));
+
+               // access object (not found)
+               CmisObject obj4 = cache.get(id);
+               Assert.assertNull(obj4);
+               CmisObject obj5 = cache.getByPath(path);
+               Assert.assertNull(obj5);
+       }
+
+       @Test
+       public void lruTest() {
+               int cacheSize = 3;
+               Cache cache = new CacheImpl(cacheSize);
+
+               for (int i = 0; i < cacheSize + 1; i++) {
+                       CmisObject obj = this.createCmisObject("id" + i, "path" 
+ i);
+                       cache.put(obj);
+               }
+               
+               Assert.assertNull(cache.get("id0"));    // thrown out
+               Assert.assertNotNull(cache.get("id1"));
+               Assert.assertNotNull(cache.get("id2"));
+               Assert.assertNotNull(cache.get("id3"));
+               
+       }
+
+       /**
+        * Create a Mock for testing Cache is sufficient.
+        * 
+        * @param id
+        * @param path
+        * @return a mocked object
+        */
+       private CmisObject createCmisObject(String id, String path) {
+               CmisObject obj = createNiceMock(CmisObject.class);
+
+               expect(obj.getId()).andReturn(id).anyTimes();
+               expect(obj.getPath()).andReturn(path).anyTimes();
+
+               replay(obj);
+
+               return obj;
+       }
+}

Propchange: 
incubator/chemistry/trunk/opencmis/opencmis-client/opencmis-client-impl/src/test/java/org/apache/opencmis/client/runtime/misc/CacheTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to