chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1296444 - in /chemistry/playground/chemistry-opencmis-javascript-client/src: main/webapp/cmislib.js test/resources/testcmislib.js
Date Fri, 02 Mar 2012 20:44:31 GMT
Author: jens
Date: Fri Mar  2 20:44:30 2012
New Revision: 1296444

URL: http://svn.apache.org/viewvc?rev=1296444&view=rev
Log:
add experimental type cache

Modified:
    chemistry/playground/chemistry-opencmis-javascript-client/src/main/webapp/cmislib.js
    chemistry/playground/chemistry-opencmis-javascript-client/src/test/resources/testcmislib.js

Modified: chemistry/playground/chemistry-opencmis-javascript-client/src/main/webapp/cmislib.js
URL: http://svn.apache.org/viewvc/chemistry/playground/chemistry-opencmis-javascript-client/src/main/webapp/cmislib.js?rev=1296444&r1=1296443&r2=1296444&view=diff
==============================================================================
--- chemistry/playground/chemistry-opencmis-javascript-client/src/main/webapp/cmislib.js (original)
+++ chemistry/playground/chemistry-opencmis-javascript-client/src/main/webapp/cmislib.js Fri
Mar  2 20:44:30 2012
@@ -67,6 +67,60 @@ CmisError.prototype.toString = function(
     ", message: " + this.message;
 };
 
+function TypeCache() {
+    this.types = {}; // a map from a type id to to a CMIS type definition
+    this.lastAccessed = {}; // a map from a type id to a time stamp when it was last use
+    this.maxSize = 10; // number of types to be cached
+    
+    this.reset = function () {
+        this.types = {};
+        this.lastAccessed = {};        
+    };
+    
+    this.getType = function (typeId) {
+        return this.types[typeId]; // type id or null
+    };
+    
+    this.addType = function (typeDef) {
+        this.removeOldestElements();
+        this.types[typeDef.id] = typeDef;
+        this.lastAccessed[typeDef.id] = new Date();
+        return this;
+    };
+    
+    this.isTypeInCache = function(typeId) {
+        return this.types.hasOwnProperty(typeId);
+    };
+    
+    this.removeOldestElements  = function () {
+        while (this.getSize() >= this.maxSize)
+            this.removeOldestElement();
+        return this;
+    };
+    
+    this.removeOldestElement  = function () {
+        var oldest = new Date();
+        var oldestKey = null;
+        // find oldest element
+        for (var key in this.lastAccessed) {
+            if (this.lastAccessed[key] < oldest) {
+                oldest = this.lastAccessed[key];
+                oldestKey = key;
+            }
+        }
+        // remove oldest key
+        delete this.types[oldestKey];
+        delete this.lastAccessed[oldestKey];
+    };
+    
+    this.getSize = function() {
+        var size = 0, key;
+        for (key in this.types) {
+            if (this.types.hasOwnProperty(key)) size++;
+        }
+        return size;
+    }
+};
 
 function CmisSession(urlPrefix, repositoryId) {
     this.repId = repositoryId;;
@@ -74,6 +128,7 @@ function CmisSession(urlPrefix, reposito
     this.rootUrl = null;
     this.opCtx = new OperationContext();
     this.rootFolderId; // just use a place to store the id, must be filled by user of a CmisSession
+    this.typeCache = new TypeCache();
     
 	this.getUrl = function() {
 		return this.urlPrefix + "/" + this.repId;
@@ -92,7 +147,7 @@ function CmisSession(urlPrefix, reposito
               jsonObj.stacktrace);
     };
 
-  this.getRepositoryInfo = function(cbFct) {
+    this.getRepositoryInfo = function(cbFct) {
         var url = this.getUrl();
         var data = {
                 cmisSelector: "repositoryInfo"
@@ -188,7 +243,18 @@ function CmisSession(urlPrefix, reposito
         };
 
         console.log("getTypeDefinition() " + url);
-        this.doJson(url, data, cbFct);        
+        // try cache first:
+        var cache = this.typeCache;
+        var cachedTypeDef = this.typeCache.getType(typeId);
+        if (null == cachedTypeDef) { // not found in cache get it from server
+            this.doJson(url, data, function(type) {
+                cache.addType(type); // add found type to the cache
+                return cbFct.call(this, type);
+            });
+        } else {
+            // call callback function with cached type
+            return cbFct.call(this, cachedTypeDef);            
+        }
     }
     
     this.getTypeChildren = function(typeIdent, cbFct) {
@@ -213,8 +279,7 @@ function CmisSession(urlPrefix, reposito
             success: cbFct
         });
 	};
-	
-	
+		
 };
 
 CmisSession.defaultErrorHandler = function (event, jqXHR, settings, excep) {

Modified: chemistry/playground/chemistry-opencmis-javascript-client/src/test/resources/testcmislib.js
URL: http://svn.apache.org/viewvc/chemistry/playground/chemistry-opencmis-javascript-client/src/test/resources/testcmislib.js?rev=1296444&r1=1296443&r2=1296444&view=diff
==============================================================================
--- chemistry/playground/chemistry-opencmis-javascript-client/src/test/resources/testcmislib.js
(original)
+++ chemistry/playground/chemistry-opencmis-javascript-client/src/test/resources/testcmislib.js
Fri Mar  2 20:44:30 2012
@@ -26,6 +26,14 @@ function trace(text) {
 
 }
 
+function getArraySize(obj) {
+    var size = 0, key;
+    for (key in obj) {
+        if (obj.hasOwnProperty(key)) size++;
+    }
+    return size;
+}
+
 module("Module CmisLib", {
     setup: function() {
         
@@ -184,7 +192,7 @@ test("testGetTypeDefinition()", function
     this.session.getTypeDefinition("cmis:document", function(type) {
         var count = 0;
         trace("Found type definition: " + type.id);
-        ok(type.exception == null, "gGetTypeDefinition() exception");
+        ok(type.exception == null, "getTypeDefinition() exception");
         ok(type.id != null, "Type definition has id");
         for (var key in type.propertyDefinitions) {
             trace("Found property definition: " + type.propertyDefinitions[key].id);
@@ -211,3 +219,105 @@ test("testGetTypeChildren()", function()
         start();
      });
 });
+
+test("testTypeCache", function() {
+    trace("testTypeCache ");
+    expect(30);
+
+    var types = [ 
+            {id : "type1", more: "other type attributes" }, 
+            {id : "type2", more: "other type attributes" }, 
+            {id : "type3", more: "other type attributes" }, 
+            {id : "type4", more: "other type attributes" }, 
+            {id : "type5", more: "other type attributes" }, 
+            {id : "type6", more: "other type attributes" }, 
+                ];
+    
+    var typeCache = new TypeCache();
+    var testMaxSize = 3;
+    typeCache.maxSize = testMaxSize;    
+
+    typeCache.addType(types[0]);
+    ok(null == typeCache.getType("foo"), "Dummy Test.");
+
+    typeCache.addType(types[0]).addType(types[1]).addType(types[2]);
+
+    // fake some dates
+    typeCache.lastAccessed[types[0].id] = typeCache.lastAccessed[types[0].id] - 1000;
+    typeCache.lastAccessed[types[1].id] = typeCache.lastAccessed[types[1].id] - 5000;
+    typeCache.lastAccessed[types[2].id] = typeCache.lastAccessed[types[2].id] - 100;
+    
+    typeCache.removeOldestElement();
+    ok(null == typeCache.getType(types[1].id), "Oldest element is gone");
+    ok(types[0] == typeCache.getType(types[0].id), "Not oldest element is there.");
+    ok(types[2] == typeCache.getType(types[2].id), "Not oldest element is there.");
+
+    typeCache.removeOldestElement();
+    ok(null == typeCache.getType(types[1].id), "Oldest element is gone");
+    ok(null == typeCache.getType(types[0].id), "Second oldest element is gone");
+    ok(types[2] == typeCache.getType(types[2].id), "Not oldest element is there.");
+    
+    typeCache.reset();
+    ok(getArraySize(typeCache.lastAccessed) == 0, "last accessed is empty after reset")
+    ok(typeCache.getSize() == 0, "type map is empty after reset")
+    
+    typeCache.addType(types[0]).addType(types[1]).addType(types[2]);
+    // fake some dates
+    typeCache.lastAccessed[types[0].id] = typeCache.lastAccessed[types[0].id] - 1000;
+    typeCache.lastAccessed[types[1].id] = typeCache.lastAccessed[types[1].id] - 5000;
+    typeCache.lastAccessed[types[2].id] = typeCache.lastAccessed[types[2].id] - 100;
+
+    typeCache.addType(types[3]);
+    ok(getArraySize(typeCache.lastAccessed) == testMaxSize, "last accessed max size is not
exceeded")
+    ok(typeCache.getSize() == testMaxSize, "type map max sizes is not exceeded")
+    ok(null == typeCache.getType(types[1].id), "Oldest element is gone");
+    ok(types[0] == typeCache.getType(types[0].id), "Not oldest element is there.");
+    ok(types[2] == typeCache.getType(types[2].id), "Not oldest element is there.");
+    ok(types[3] == typeCache.getType(types[3].id), "Just added element is there.");
+
+    typeCache.addType(types[4]);
+    ok(getArraySize(typeCache.lastAccessed) == testMaxSize, "last accessed max size is not
exceeded")
+    ok(typeCache.getSize() == testMaxSize, "type map max sizes is not exceeded")
+    ok(null == typeCache.getType(types[1].id), "Oldest element is gone");
+    ok(null == typeCache.getType(types[0].id), "Second oldest element is gone");
+    ok(types[2] == typeCache.getType(types[2].id), "Not oldest element is there.");
+    ok(types[3] == typeCache.getType(types[3].id), "Just before added element is there.");
+    ok(types[4] == typeCache.getType(types[4].id), "Just added element is there.");
+    
+    typeCache.addType(types[5]);
+    ok(getArraySize(typeCache.lastAccessed) == testMaxSize, "last accessed max size is not
exceeded")
+    ok(typeCache.getSize() == testMaxSize, "type map max sizes is not exceeded")
+    ok(null == typeCache.getType(types[1].id), "Oldest element is gone");
+    ok(null == typeCache.getType(types[0].id), "Second oldest element is gone");
+    ok(null == typeCache.getType(types[2].id), "Third oldest element is gone");
+    ok(types[3] == typeCache.getType(types[3].id), "Just before added element is there.");
+    ok(types[4] == typeCache.getType(types[4].id), "Just before added element is there.");
+    ok(types[5] == typeCache.getType(types[5].id), "Just added element is there.");   
+});
+
+test("testGetTypeDefinitionWithCache()", function() {
+    trace("testGetTypeDefinitionWithCache ");
+    expect(5);
+
+    var ctx = this;
+    var typeId = "cmis:document";
+    $(document).unbind('ajaxError'); 
+    stop(2);
+    this.session.getTypeDefinition(typeId, function(type) {
+        var count = 0;
+        trace("Found type definition: " + type.id);
+        ok(type.exception == null, "getTypeDefinition() exception");
+        ok(type.id != null, "Type definition has id");
+        // try getting the type a second time:
+        ok(ctx.session.typeCache.isTypeInCache(typeId), "Type is in cache after call.");
+        ctx.session.urlPrefix = null; 
+            // make sure that we get it from the cache this time by cutting the network connection
+        ctx.session.getTypeDefinition(typeId, function(type2) {
+            trace("Found type definition: " + type2.id);
+            ok(type2.id != null, "Type definition has id");
+            ok(type == type2, "Type is same as before");
+            start();
+         });        
+        start();
+     });
+});



Mime
View raw message