Commit b774e9bb authored by Peter Harrison's avatar Peter Harrison

Resolving with issues with CacheImpl and mutiple id lists.

parent 86f24c91
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2015 Peter Harrrison
*
* This file is part of Gravity Workflow Automation.
*
......@@ -25,7 +26,7 @@ import java.util.Map;
public interface Cache<T> {
public void invalidate( String itemId );
public void invalidate( String... itemIds );
public T getItem(String... itemIds) throws Exception;
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
......@@ -31,12 +32,21 @@ abstract public class CacheImpl<T> implements Cache<T> {
private Map<String, T> cacheMap = new ConcurrentHashMap<String,T>();
private Map<String,String> cacheList = null;
private Map<String,Map<String,String>> cacheList = new ConcurrentHashMap<String, Map<String,String>>();
@Override
public void invalidate(String itemId) {
this.cacheMap.remove(itemId);
this.cacheList = null;
public void invalidate(String... itemIds ) {
String cacheId = getCacheId(itemIds);
this.cacheMap.remove(cacheId);
if(itemIds.length>1){
String[] listId = new String[itemIds.length-1];
for( int a=0; a<itemIds.length; a++){
listId[a] = itemIds[a];
}
this.cacheList.remove(getCacheId(listId));
}
}
@Override
......@@ -59,14 +69,35 @@ abstract public class CacheImpl<T> implements Cache<T> {
}
public Map<String,String> list(String... prefixs) throws Exception{
String cacheId = "default";
if(prefixs.length>0){
cacheId = this.getCacheId(prefixs);
}
Map<String, String> list = this.cacheList.get(cacheId);
if(list==null){
if(prefixs.length>0){
list = this.getListFromStore(prefixs);
} else {
list = this.getListFromStore("");
}
this.cacheList.put(cacheId, list);
}
return new HashMap<String,String>(this.cacheList.get(cacheId));
/*
if( this.cacheList == null){
if( prefixs.length>0){
this.cacheList = this.getListFromStore(prefixs);
cacheId = this.getCacheId(prefixs);
Map<String,String> list = this.getListFromStore(prefixs);
this.cacheList.put(cacheId, list);
} else {
this.cacheList = this.getListFromStore("");
Map<String,String> list = this.getListFromStore("");
this.cacheList.put(cacheId, list);
}
}
return new HashMap<String,String>(this.cacheList);
return new HashMap<String,String>(this.cacheList.get(cacheId));
*/
}
@Override
......@@ -84,11 +115,14 @@ abstract public class CacheImpl<T> implements Cache<T> {
public String getCacheId( String... ids ){
StringBuilder id = new StringBuilder();
for (int i = 0; i < ids.length; ++i) {
id.append(ids[i]);
id.append("-");
if(!"".equals(ids[i])){
id.append(ids[i]);
id.append("-");
}
}
String returnValue = id.toString();
return returnValue.substring(0, returnValue.length()-1);
String substring = returnValue.substring(0, returnValue.length()-1);
return substring;
}
abstract protected T getFromStore(String... itemIds) throws Exception;
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
......@@ -174,6 +175,6 @@ public class ListController {
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(LIST, listId);
this.cacheInvalidationManager.invalidate(LIST, listCache.getCacheId(boardId,listId));
}
}
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
......@@ -22,16 +23,14 @@
package nz.net.orcon.kanban.controllers;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.Map;
import javax.annotation.Resource;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import nz.net.orcon.kanban.automation.CacheInvalidationInterface;
import nz.net.orcon.kanban.tools.IdentifierTools;
import nz.net.orcon.kanban.tools.ListTools;
import nz.net.orcon.kanban.tools.OcmMapperFactory;
......@@ -42,13 +41,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ibm.wsdl.util.IOUtils;
@Controller
@RequestMapping("/board/{boardId}/resources")
public class ResourceController {
......@@ -63,6 +59,9 @@ public class ResourceController {
@Autowired
private ListTools listTools;
@Autowired
private ResourceCache resourceCache;
@Autowired
CacheInvalidationInterface cacheInvalidationManager;
......@@ -71,19 +70,7 @@ public class ResourceController {
public @ResponseBody String getResource(@PathVariable String boardId,
@PathVariable String resourceId) throws Exception {
//TODO USE CACHE
ObjectContentManager ocm = ocmFactory.getOcm();
Node node = ocm.getSession().getNode(String.format(URI.RESOURCE_URI, boardId, resourceId));
Property property = node.getProperty("resource");
String value = property.getString();
ocm.logout();
if(value==null){
throw new ResourceNotFoundException();
}
return value;
return resourceCache.getItem(boardId, IdentifierTools.getIdFromName(resourceId));
}
@PreAuthorize("hasPermission(#boardId, 'BOARD', 'ADMIN')")
......@@ -93,7 +80,6 @@ public class ResourceController {
HttpServletRequest request) throws Exception {
logger.info("Saving Resource " + boardId + "/" + resourceId);
BufferedReader reader = request.getReader();
reader.reset();
StringBuilder valueBuilder = new StringBuilder();
......@@ -133,7 +119,6 @@ public class ResourceController {
@PathVariable String resourceId) throws Exception {
logger.info("Deleting Resource " + resourceId);
ObjectContentManager ocm = ocmFactory.getOcm();
Node node = ocm.getSession().getNode(String.format(URI.RESOURCE_URI, boardId, resourceId));
if(node==null){
......@@ -144,19 +129,14 @@ public class ResourceController {
ocm.save();
ocm.logout();
this.cacheInvalidationManager.invalidate(RESOURCE, resourceId);
this.cacheInvalidationManager.invalidate(RESOURCE, resourceCache.getCacheId(boardId,resourceId));
}
@PreAuthorize("hasPermission(#boardId, 'BOARD', 'READ,WRITE,ADMIN')")
@RequestMapping(value = "", method=RequestMethod.GET)
public @ResponseBody Map<String,String> listResources(@PathVariable String boardId) throws Exception {
logger.info("Getting Resource List");
Session session = ocmFactory.getOcm().getSession();
Map<String,String> result = listTools.list(String.format(URI.RESOURCE_URI, boardId, ""), "name", session);
session.logout();
return result;
return this.resourceCache.list(boardId,"");
}
}
......@@ -26,7 +26,6 @@ import java.util.Map;
import javax.annotation.Resource;
import javax.jcr.Node;
import javax.jcr.Session;
import nz.net.orcon.kanban.automation.CacheInvalidationInterface;
import nz.net.orcon.kanban.gviz.GVGraph;
......@@ -113,12 +112,14 @@ public class RuleController {
@RequestMapping(value = "", method=RequestMethod.GET)
public @ResponseBody Map<String,String> listRules(@PathVariable String boardId) throws Exception {
ruleCache.list(boardId);
Map<String, String> ruleList = ruleCache.list(boardId,"");
Session session = ocmFactory.getOcm().getSession();
Map<String,String> result = listTools.list(String.format(URI.RULE_URI, boardId,""), "name", session);
session.logout();
return result;
//ruleCache.list(boardId);
//Session session = ocmFactory.getOcm().getSession();
//Map<String,String> result = listTools.list(String.format(URI.RULE_URI, boardId,""), "name", session);
//session.logout();
return ruleList;
}
@PreAuthorize("hasPermission(#boardId, 'BOARD', 'ADMIN')")
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
......@@ -22,7 +23,6 @@
package nz.net.orcon.kanban.controllers;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.jcr.Node;
......@@ -81,7 +81,7 @@ public class TemplateController {
node.remove();
ocm.save();
this.cacheInvalidationManager.invalidate(TEMPLATE, templateId);
this.cacheInvalidationManager.invalidate(TEMPLATE, templateCache.getCacheId(boardId,templateId));
} finally {
ocm.logout();
}
......@@ -102,8 +102,6 @@ public class TemplateController {
}
template.setPath(String.format(URI.TEMPLATE_URI, boardId, templateId));
ocm.insert(template);
//listTools.ensurePresence(String.format(URI.TEMPLATE_URI, templateId), "groups", ocm.getSession());
ocm.save();
this.cacheInvalidationManager.invalidate(TEMPLATE, templateId);
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.orcon.kanban.controllers;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CacheImplTest {
TestCache testCache;
@Before
public void setUp() throws Exception {
this.testCache = new TestCache();
}
@Test
public void testInvalidateSingle() throws Exception {
this.testCache.storeItem("TestSingleString1", "testid1");
this.testCache.storeItem("TestSingleString2", "testid2");
assertEquals("TestSingleString1",this.testCache.getItem("testid1"));
assertEquals("TestSingleString2",this.testCache.getItem("testid2"));
this.testCache.invalidate("testid1");
try{
this.testCache.getItem("testid1");
assertTrue(false);
} catch( ResourceNotFoundException e){
}
assertEquals(this.testCache.getItem("testid2"),"TestSingleString2");
}
@Test
public void testInvalidateMultiple() throws Exception {
this.testCache.storeItem("TestSingleString1", "id1", "sub1");
this.testCache.storeItem("TestSingleString2", "id1", "sub2");
this.testCache.storeItem("TestSingleString3", "id2", "sub1");
this.testCache.storeItem("TestSingleString4", "id2", "sub2");
this.testCache.invalidate("id1","sub2");
try{
this.testCache.getItem("id1","sub2");
assertTrue(false);
} catch( ResourceNotFoundException e){
}
assertEquals(this.testCache.getItem("id1","sub1"),"TestSingleString1");
assertEquals(this.testCache.getItem("id2","sub1"),"TestSingleString3");
assertEquals(this.testCache.getItem("id2","sub2"),"TestSingleString4");
}
@Test
public void testGetItemSingle() throws Exception {
this.testCache.storeItem("TestSingleString1", "id1");
this.testCache.storeItem("TestSingleString2", "id2");
assertEquals("TestSingleString1",this.testCache.getItem("id1"));
assertEquals("TestSingleString2",this.testCache.getItem("id2"));
}
@Test
public void testGetItemMultiple() throws Exception {
this.testCache.storeItem("TestSingleString1", "id1", "sub1");
this.testCache.storeItem("TestSingleString2", "id1", "sub2");
this.testCache.storeItem("TestSingleString3", "id1", "sub3");
this.testCache.storeItem("TestSingleString4", "id2", "sub1");
this.testCache.storeItem("TestSingleString5", "id2", "sub2");
this.testCache.storeItem("TestSingleString6", "id2", "sub3");
assertEquals("TestSingleString1",this.testCache.getItem("id1","sub1"));
assertEquals("TestSingleString2",this.testCache.getItem("id1","sub2"));
assertEquals("TestSingleString3",this.testCache.getItem("id1","sub3"));
assertEquals("TestSingleString4",this.testCache.getItem("id2","sub1"));
assertEquals("TestSingleString5",this.testCache.getItem("id2","sub2"));
assertEquals("TestSingleString6",this.testCache.getItem("id2","sub3"));
}
@Test
public void testClearCache() throws Exception {
this.testCache.storeItem("TestSingleString1", "testid1");
this.testCache.storeItem("TestSingleString2", "testid2");
assertEquals("TestSingleString1",this.testCache.getItem("testid1"));
assertEquals("TestSingleString2",this.testCache.getItem("testid2"));
this.testCache.clearCache();
try{
this.testCache.getItem("testid1");
assertTrue(false);
} catch( ResourceNotFoundException e){
}
try{
this.testCache.getItem("testid2");
assertTrue(false);
} catch( ResourceNotFoundException e){
}
}
@Test
public void testGetCacheId() {
assertEquals("first",this.testCache.getCacheId("first"));
assertEquals("first-second",this.testCache.getCacheId("first","second"));
assertEquals("first-second-third",this.testCache.getCacheId("first","second","third"));
}
}
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.orcon.kanban.controllers;
import java.util.Map;
public class TestCache extends CacheImpl<String> {
@Override
protected String getFromStore(String... itemIds) throws Exception {
return null;
}
@Override
protected Map<String, String> getListFromStore(String... prefixs)
throws Exception {
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment