Author: rwatler Date: Thu Aug 20 19:10:38 2009 New Revision: 806308 URL: http://svn.apache.org/viewvc?rev=806308&view=rev Log: implement negative caching for missing persistent preferences
Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/NodeImplProxy.java portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/PersistenceBrokerPreferencesProvider.java Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/NodeImplProxy.java URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/NodeImplProxy.java?rev=806308&r1=806307&r2=806308&view=diff ============================================================================== --- portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/NodeImplProxy.java (original) +++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/NodeImplProxy.java Thu Aug 20 19:10:38 2009 @@ -26,6 +26,8 @@ public class NodeImplProxy implements Node { private Node node = null; + private String fullPath = null; + private int nodeType = -1; private boolean dirty = false; private static PersistenceBrokerPreferencesProvider provider; @@ -37,57 +39,110 @@ public Timestamp getCreationDate() { - return getNode().getCreationDate(); - } + Node node = getNode(); + if (node != null) + { + return node.getCreationDate(); + } + throw new RuntimeException("Node not defined, getCreationDate() invoked on negative cache entry"); + } public String getFullPath() { - return getNode().getFullPath(); + Node node = getNode(); + if (node != null) + { + return node.getFullPath(); + } + return fullPath; } public Timestamp getModifiedDate() { - return getNode().getModifiedDate(); + Node node = getNode(); + if (node != null) + { + return node.getModifiedDate(); + } + throw new RuntimeException("Node not defined, getModifiedDate() invoked on negative cache entry"); } public long getNodeId() { - return getNode().getNodeId(); + Node node = getNode(); + if (node != null) + { + return node.getNodeId(); + } + throw new RuntimeException("Node not defined, getNodeId() invoked on negative cache entry"); } public Collection getNodeKeys() { - return getNode().getNodeKeys(); + Node node = getNode(); + if (node != null) + { + return node.getNodeKeys(); + } + throw new RuntimeException("Node not defined, getNodeKeys() invoked on negative cache entry"); } public String getNodeName() { - return getNode().getNodeName(); + Node node = getNode(); + if (node != null) + { + return node.getNodeName(); + } + throw new RuntimeException("Node not defined, getNodeName() invoked on negative cache entry"); } public Collection getNodeProperties() { - return getNode().getNodeProperties(); + Node node = getNode(); + if (node != null) + { + return node.getNodeProperties(); + } + throw new RuntimeException("Node not defined, getNodeProperties() invoked on negative cache entry"); } public int getNodeType() { - return getNode().getNodeType(); + Node node = getNode(); + if (node != null) + { + return node.getNodeType(); + } + return nodeType; } public Long getParentNodeId() { - return getNode().getParentNodeId(); + Node node = getNode(); + if (node != null) + { + return node.getParentNodeId(); + } + throw new RuntimeException("Node not defined, getParentNodeId() invoked on negative cache entry"); } public void setCreationDate(Timestamp creationDate) { - getNode().setCreationDate(creationDate); + getNode().setCreationDate(creationDate); } public void setFullPath(String fullPath) { - getNode().setFullPath(fullPath); + Node node = getNode(); + if (node != null) + { + node.setFullPath(fullPath); + } + else + { + this.fullPath = fullPath; + } } public void setModifiedDate(Timestamp modifiedDate) @@ -108,7 +163,6 @@ public void setNodeName(String nodeName) { getNode().setNodeName(nodeName); - } public void setNodeProperties(Collection nodeProperties) @@ -118,7 +172,15 @@ public void setNodeType(int nodeType) { - getNode().setNodeType(nodeType); + Node node = getNode(); + if (node != null) + { + node.setNodeType(nodeType); + } + else + { + this.nodeType = nodeType; + } } public void setParentNodeId(Long parentNodeId) @@ -131,6 +193,12 @@ this.node = node; } + public NodeImplProxy(String fullPath, int nodeType) + { + this.fullPath = fullPath; + this.nodeType = nodeType; + } + public static void setProvider(PersistenceBrokerPreferencesProvider p) { provider = p; @@ -152,6 +220,8 @@ public void setNode(Node node) { this.node = node; + this.fullPath = null; + this.nodeType = -1; } protected void reset() Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/PersistenceBrokerPreferencesProvider.java URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/PersistenceBrokerPreferencesProvider.java?rev=806308&r1=806307&r2=806308&view=diff ============================================================================== --- portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/PersistenceBrokerPreferencesProvider.java (original) +++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.3-POSTRELEASE/components/prefs/src/java/org/apache/jetspeed/prefs/impl/PersistenceBrokerPreferencesProvider.java Thu Aug 20 19:10:38 2009 @@ -240,7 +240,12 @@ NodeCache hit = getNode(key.getCacheKey()); if (hit != null) { - return hit.getNode(); + NodeImplProxy proxy = hit.getNode(); + if (proxy.getNode() == null) + { + throw new NodeDoesNotExistException("No node of type " + nodeType + "found at path: " + fullPath); + } + return proxy; } Criteria c = new Criteria(); @@ -254,10 +259,10 @@ NodeImplProxy proxy = new NodeImplProxy(nodeObj); addToCache(new NodeCache(proxy)); return proxy; - } else { + addToCache(new NodeCache(new NodeImplProxy(fullPath, nodeType))); throw new NodeDoesNotExistException("No node of type " + nodeType + "found at path: " + fullPath); } } @@ -282,6 +287,9 @@ } else { + proxy.setNode(null); + proxy.setFullPath(fullPath); + addToCache(new NodeCache(new NodeImplProxy(fullPath, nodeType))); throw new NodeDoesNotExistException("No node of type " + nodeType + "found at path: " + fullPath); } } @@ -292,8 +300,12 @@ public boolean nodeExists(String fullPath, int nodeType) { NodeCache key = new NodeCache(fullPath, nodeType); - if (preferenceCache.isKeyInCache(key)) - return true; + NodeCache hit = getNode(key.getCacheKey()); + if (hit != null) + { + NodeImplProxy proxy = hit.getNode(); + return (proxy.getNode() != null); + } Criteria c = new Criteria(); c.addEqualTo("fullPath", fullPath); c.addEqualTo("nodeType", new Integer(nodeType)); @@ -308,6 +320,7 @@ } else { + addToCache(new NodeCache(new NodeImplProxy(fullPath, nodeType))); return false; } } @@ -318,20 +331,31 @@ public Node createNode(Node parent, String nodeName, int nodeType, String fullPath) throws FailedToCreateNodeException, NodeAlreadyExistsException { - if (nodeExists(fullPath, nodeType)) + // clear cache for node to be created and any children + NodeCache key = new NodeCache(fullPath, nodeType); + if (preferenceCache.removeQuiet(key.getCacheKey())) + { + clearCachedNodeAndAllChildren(fullPath); + } + // check to see if node exists in persistent store + Criteria c = new Criteria(); + c.addEqualTo("fullPath", fullPath); + c.addEqualTo("nodeType", new Integer(nodeType)); + Query query = QueryFactory.newQuery(NodeImpl.class, c); + Node nodeObj = (Node) getPersistenceBrokerTemplate().getObjectByQuery(query); + if (null != nodeObj) { throw new NodeAlreadyExistsException("Node of type " + nodeType + " already exists at path " + fullPath); } else { + // create new node and cache Long parentNodeId = null; if (null != parent) { parentNodeId = new Long(parent.getNodeId()); } - - Node nodeObj = new NodeImpl(parentNodeId, nodeName, nodeType, fullPath); - + nodeObj = new NodeImpl(parentNodeId, nodeName, nodeType, fullPath); try { getPersistenceBrokerTemplate().store(nodeObj); @@ -344,7 +368,6 @@ throw new FailedToCreateNodeException("Failed to create node of type " + nodeType + " for the path " + fullPath + ". " + e.toString(), e); } - } } @@ -514,6 +537,8 @@ parentKey.getChildren().remove(key.getCacheKey()); } } + clearCachedNodeAndAllChildren(node.getFullPath()); + addToCache(new NodeCache(new NodeImplProxy(node.getFullPath(), node.getNodeType()))); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: jetspeed-dev-unsubscr...@portals.apache.org For additional commands, e-mail: jetspeed-dev-h...@portals.apache.org