private void removeDataAndNotify(TransactionWorkspace workspace, WorkspaceNode workspaceNode, InvocationContext ctx) { if (workspaceNode == null) return; Map data = new HashMap(workspaceNode.getData()); // pre-notify notifier.notifyNodeModified(workspaceNode.getFqn(), true, REMOVE_DATA, data, ctx); workspaceNode.clearData(); workspace.addNode(workspaceNode); // post-notify notifier.notifyNodeModified(workspaceNode.getFqn(), false, REMOVE_DATA, data, ctx); }
@Override public Object visitRollbackCommand(InvocationContext ctx, RollbackCommand command) throws Throwable { TransactionWorkspace workspace; workspace = getTransactionWorkspace(ctx); workspace.clearNodes(); return invokeNextInterceptor(ctx, command); }
protected void undeleteWorkspaceNode(WorkspaceNode nodeToUndelete, TransactionWorkspace workspace) { undeleteWorkspaceNode(nodeToUndelete, workspace.getNode(nodeToUndelete.getFqn().getParent())); }
workspace.setVersioningImplicit(false); workspaceNode = workspace.getNode(Fqn.ROOT); if (debug) log.debug("GlobalTransaction: " + gtx + "; Root: " + workspaceNode); workspace.addNode(workspaceNode); log.debug("Created root node in workspace."); WorkspaceNode peekInWorkspace = workspace.getNode(Fqn.fromRelativeElements(workspaceNode.getFqn(), childName)); if (peekInWorkspace != null && peekInWorkspace.isCreated()) workspace.addNode(workspaceNode); if (!(workspaceNode.getVersion() instanceof DefaultDataVersion)) workspaceNode.setVersioningImplicit(false); if (isTargetFqn && !workspace.isVersioningImplicit()) versionToPassIn = version; workspace.addNode(workspaceNode); workspaceNode.markAsCreated(); workspaceNode = workspace.getNode(currentNode.getFqn()); if (isTargetFqn && !workspace.isVersioningImplicit()) workspace.addNode(workspaceNode);
if (log.isDebugEnabled()) log.debug("Locking nodes in transaction workspace for GlobalTransaction " + gtx); for (WorkspaceNode workspaceNode : workspace.getNodes().values())
private void setVersioning(InvocationContext ctx, TransactionWorkspace workspace, WorkspaceNode workspaceNode) { // use explicit versioning if (ctx.getOptionOverrides() != null && ctx.getOptionOverrides().getDataVersion() != null) { workspace.setVersioningImplicit(false); DataVersion version = ctx.getOptionOverrides().getDataVersion(); workspaceNode.setVersion(version); if (trace) log.trace("Setting versioning for node " + workspaceNode.getFqn() + " to explicit"); workspaceNode.setVersioningImplicit(false); } else { if (trace) log.trace("Setting versioning for node " + workspaceNode.getFqn() + " to implicit"); workspaceNode.setVersioningImplicit(true); } }
private boolean removeNode(TransactionWorkspace workspace, WorkspaceNode workspaceNode, boolean notify, InvocationContext ctx) throws CacheException { // it is already removed - we can ignore it if (workspaceNode == null) return false; Fqn parentFqn = workspaceNode.getFqn().getParent(); WorkspaceNode parentNode = fetchWorkspaceNode(ctx, parentFqn, workspace, false, true); if (parentNode == null) throw new NodeNotExistsException("Unable to find parent node with fqn " + parentFqn); // pre-notify if (notify) notifier.notifyNodeRemoved(workspaceNode.getFqn(), true, workspaceNode.getData(), ctx); Fqn nodeFqn = workspaceNode.getFqn(); parentNode.removeChild(nodeFqn.getLastElement()); SortedMap<Fqn, WorkspaceNode> tailMap = workspace.getNodesAfter(workspaceNode.getFqn()); for (WorkspaceNode toDelete : tailMap.values()) { if (toDelete.getFqn().isChildOrEquals(nodeFqn)) { if (trace) log.trace("marking node " + toDelete.getFqn() + " as deleted"); toDelete.setRemoved(true); } else { break;// no more children, we came to the end } } // post-notify if (notify) notifier.notifyNodeRemoved(workspaceNode.getFqn(), false, null, ctx); return workspaceNode.getNode().isValid(); }
Collection<WorkspaceNode> nodes = workspace.getNodes().values();
@Override public Object visitGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable { TransactionWorkspace workspace = getTransactionWorkspace(ctx); Object result; Fqn fqn = command.getFqn(); WorkspaceNode workspaceNode = fetchWorkspaceNode(ctx, fqn, workspace, false, false); if (workspaceNode == null) { if (trace) log.trace("unable to find node " + fqn + " in workspace."); result = null; } else { notifier.notifyNodeVisited(fqn, true, ctx); Object keySet = workspaceNode.getKeys(); workspace.addNode(workspaceNode); notifier.notifyNodeVisited(fqn, false, ctx); result = keySet; } return result; }
protected DataVersion getNodeVersion(TransactionWorkspace w, Fqn f) { if (w == null) return null; WorkspaceNode wn = w.getNode(f); if (wn == null) return null; // JBCACHE-1297 DataVersion v = wn.getVersion(); if (wn.isVersioningImplicit()) { // then send back an incremented version v = ((DefaultDataVersion) v).increment(); } return v; }
@Override protected void cleanupStaleLocks(InvocationContext ctx) throws Throwable { super.cleanupStaleLocks(ctx); TransactionContext transactionContext = ctx.getTransactionContext(); if (transactionContext != null) { ((OptimisticTransactionContext) transactionContext).getTransactionWorkSpace().clearNodes(); } }
private Object removeKeyAndNotify(Object removeKey, TransactionWorkspace workspace, WorkspaceNode workspaceNode, InvocationContext ctx) { if (workspaceNode == null) return null; if (notifier.shouldNotifyOnNodeModified())// pre-notify { notifier.notifyNodeModified(workspaceNode.getFqn(), true, REMOVE_DATA, workspaceNode.getData(), ctx); } Object old = workspaceNode.remove(removeKey); workspace.addNode(workspaceNode); if (notifier.shouldNotifyOnNodeModified()) { Map removedData = Collections.singletonMap(removeKey, old); // post-notify notifier.notifyNodeModified(workspaceNode.getFqn(), false, REMOVE_DATA, removedData, ctx); } return old; }
/** * Digs out the DataVersion for a given Fqn. If the versioning is explicit, it is passed as-is. If implicit, it is * cloned and then incremented, and the clone is returned. */ private DataVersion getVersionToBroadcast(TransactionWorkspace w, Fqn f) { WorkspaceNode n = w.getNode(f); if (n == null) { if (trace) log.trace("Fqn " + f + " not found in workspace; not using a data version."); return null; } if (n.isVersioningImplicit()) { DefaultDataVersion v = (DefaultDataVersion) n.getVersion(); if (trace) log.trace("Fqn " + f + " has implicit versioning. Broadcasting an incremented version."); // potential bug here - need to check if we *need* to increment at all, because of Configuration.isLockParentForChildInsertRemove() return v.increment(); } else { if (trace) log.trace("Fqn " + f + " has explicit versioning. Broadcasting the version as-is."); return n.getVersion(); } }
private Object putDataKeyValueAndNotify(Object key, Object value, TransactionWorkspace workspace, WorkspaceNode workspaceNode, InvocationContext ctx) { if (workspaceNode == null) throw new NodeNotExistsException("optimisticCreateIfNotExistsInterceptor should have created this node!"); if (notifier.shouldNotifyOnNodeModified())// pre-notify { notifier.notifyNodeModified(workspaceNode.getFqn(), true, PUT_DATA, workspaceNode.getData(), ctx); } Object old = workspaceNode.put(key, value); workspace.addNode(workspaceNode); if (notifier.shouldNotifyOnNodeModified())// post-notify { Map addedData = Collections.singletonMap(key, value); notifier.notifyNodeModified(workspaceNode.getFqn(), false, PUT_DATA, addedData, ctx); } return old; }
private void putDataMapAndNotify(Map<Object, Object> data, TransactionWorkspace workspace, WorkspaceNode workspaceNode, InvocationContext ctx) { if (workspaceNode == null) throw new NodeNotExistsException("optimisticCreateIfNotExistsInterceptor should have created this node!"); // pre-notify notifier.notifyNodeModified(workspaceNode.getFqn(), true, PUT_MAP, workspaceNode.getData(), ctx); workspaceNode.putAll(data); workspace.addNode(workspaceNode); // post-notify notifier.notifyNodeModified(workspaceNode.getFqn(), false, PUT_MAP, workspaceNode.getData(), ctx); }
@Override public Object visitGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable { TransactionWorkspace workspace = getTransactionWorkspace(ctx); Object result; WorkspaceNode workspaceNode = fetchWorkspaceNode(ctx, command.getFqn(), workspace, false, false); if (workspaceNode == null) { if (trace) log.trace("Unable to find node " + command.getFqn() + " in workspace."); result = null; } else { notifier.notifyNodeVisited(command.getFqn(), true, ctx); Object nameSet = workspaceNode.getChildrenNames(); workspace.addNode(workspaceNode); notifier.notifyNodeVisited(command.getFqn(), false, ctx); result = nameSet; } return result; }
@Override public Object visitGetDataMapCommand(InvocationContext ctx, GetDataMapCommand command) throws Throwable { TransactionWorkspace workspace = getTransactionWorkspace(ctx); Object result; WorkspaceNode workspaceNode = fetchWorkspaceNode(ctx, command.getFqn(), workspace, false, false); if (workspaceNode == null) { if (trace) log.trace("unable to find node " + command.getFqn() + " in workspace."); result = null; } else { notifier.notifyNodeVisited(command.getFqn(), true, ctx); Object data = workspaceNode.getData(); workspace.addNode(workspaceNode); notifier.notifyNodeVisited(command.getFqn(), false, ctx); result = data; } return result; }
@Override public Object visitGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable { TransactionWorkspace workspace = getTransactionWorkspace(ctx); Object result; WorkspaceNode workspaceNode = fetchWorkspaceNode(ctx, command.getFqn(), workspace, false, false); if (workspaceNode == null) { if (trace) log.trace("Unable to find node " + command.getFqn() + " in workspace."); result = null; } else if (workspaceNode.isRemoved()) { if (trace) log.trace("Attempted to retrieve node " + command.getFqn() + " but it has been deleted!"); result = null; } else { notifier.notifyNodeVisited(command.getFqn(), true, ctx); workspace.addNode(workspaceNode); notifier.notifyNodeVisited(command.getFqn(), false, ctx); result = workspaceNode.getNode(); } return result; }