Package org.jboss.cache.optimistic

Examples of org.jboss.cache.optimistic.TransactionWorkspace


    }


    private void lockNodes(GlobalTransaction gtx) throws Exception
    {
        TransactionWorkspace workspace = getTransactionWorkspace(gtx);
        TransactionEntry te = cache.getTransactionTable().get(gtx);
        log.debug("locking nodes");

        // should be an ordered list
        Collection nodes = workspace.getNodes().values();

        for (Iterator it = nodes.iterator(); it.hasNext();)
        {
            WorkspaceNode workspaceNode = (WorkspaceNode) it.next();
            DataNode node = workspaceNode.getNode();
View Full Code Here


      Object result = null;

      GlobalTransaction gtx = ctx.getGlobalTransaction();

      TransactionWorkspace workspace = getTransactionWorkspace(gtx);

      if (MethodDeclarations.isCrudMethod(meth))
      {
         if (tx == null || !isValid(tx))
         {
            throw new CacheException("Must be in a valid transaction " + m);
         }

         WorkspaceNode workspaceNode = getOrCreateWorkspaceNode(getFqn(args), workspace, true);
         if (workspaceNode == null && m.getMethodId() == MethodDeclarations.dataGravitationCleanupMethod_id)
         {
            workspaceNode = getOrCreateWorkspaceNode(getBackupFqn(args), workspace, true);
         }


         if (workspaceNode != null)
         {
            // use explicit versioning
            if (ctx.getOptionOverrides() != null && ctx.getOptionOverrides().getDataVersion() != null)
            {
               workspace.setVersioningImplicit(false);
               DataVersion version = ctx.getOptionOverrides().getDataVersion();

               workspaceNode.setVersion(version);
               if (log.isTraceEnabled()) log.trace("Setting versioning for node " + workspaceNode.getFqn() + " to explicit");
               workspaceNode.setVersioningImplicit(false);
View Full Code Here

    {
      // TODO: MANIK - not the most elegant - temporary for now.
      if (template instanceof WorkspaceNode)
      {
          DataNode dataNodeParent = ((WorkspaceNode) parent).getNode();
          TransactionWorkspace workspace = ((WorkspaceNode) template).getTransactionWorkspace();
          return createWorkspaceNode(dataNodeParent, workspace);
      }

      if (parent instanceof DataNode)
      {
View Full Code Here

    }


    private void validateNodes(GlobalTransaction gtx) throws CacheException
    {
        TransactionWorkspace workspace;

        try
        {
            workspace = getTransactionWorkspace(gtx);
        }
        catch (CacheException e)
        {
            throw new CacheException("unable to retrieve workspace", e);
        }

        // should be an ordered list - get the set of nodes
        Collection nodes = workspace.getNodes().values();

        boolean validated;
        //we have all locks here so lets try and validate
        log.debug("validating nodes");
        simpleValidate(nodes);
View Full Code Here

        }
    }

    private void commit(GlobalTransaction gtx)
    {
        TransactionWorkspace workspace;

        try
        {
            workspace = getTransactionWorkspace(gtx);
        }
        catch (CacheException e)
        {
            log.trace("we can't rollback", e);
            return;
        }

        log.debug("commiting validated changes ");
        // should be an ordered list
        Collection nodes = workspace.getNodes().values();

        boolean trace = log.isTraceEnabled();
        for (Iterator it = nodes.iterator(); it.hasNext();)
        {
            WorkspaceNode wrappedNode = (WorkspaceNode) it.next();
View Full Code Here

    }

    private void rollBack(GlobalTransaction gtx)
    {
        TransactionWorkspace workspace;
        try
        {
            workspace = getTransactionWorkspace(gtx);
            Map nodes = workspace.getNodes();
            nodes.clear();
        }
        catch (CacheException e)
        {
            log.info("Unable to roll back", e);
View Full Code Here

      if (targetFqn == null) return;

      boolean debug = log.isDebugEnabled();

      GlobalTransaction gtx = getGlobalTransaction(ctx);
      TransactionWorkspace workspace = getTransactionWorkspace(gtx);

      WorkspaceNode workspaceNode;

      List<Fqn> nodesCreated = new ArrayList<Fqn>();

      DataVersion version = null;
      if (ctx.getOptionOverrides() != null && ctx.getOptionOverrides().getDataVersion() != null)
      {
         version = ctx.getOptionOverrides().getDataVersion();
         workspace.setVersioningImplicit(false);
      }

      // start with the ROOT node and then work our way down to the node necessary, creating nodes along the way.
      workspaceNode = workspace.getNode(Fqn.ROOT);
      if (debug) log.debug("GlobalTransaction: " + gtx + "; Root: " + workspaceNode);

      // we do not have the root in the workspace!  Put it into thr workspace now.
      if (workspaceNode == null)
      {
         NodeSPI node = cache.getRoot();
         workspaceNode = lockAndCreateWorkspaceNode(nodeFactory, node, workspace, gtx, lockAcquisitionTimeout);
         //workspaceNode = nodeFactory.createWorkspaceNode(node, workspace);
         workspace.addNode(workspaceNode);
         log.debug("Created root node in workspace.");
      }
      else
      {
         log.debug("Found root node in workspace.");
      }

      // iterate through the target Fqn's elements.
      int targetFqnSize = targetFqn.size(), currentDepth = 1;
      for (Object childName : targetFqn.peekElements())
      {
         boolean isTargetFqn = (currentDepth == targetFqnSize);
         currentDepth++;

         // current workspace node canot be null.
         // try and get the child of current node

         if (debug) log.debug("Attempting to get child " + childName);
         NodeSPI currentNode = workspaceNode.getNode().getChildDirect(childName);

         if (currentNode == null)
         {
            // first test that it exists in the workspace and has been created in thix tx!
            WorkspaceNode peekInWorkspace = workspace.getNode(new Fqn(workspaceNode.getFqn(), childName));
            if (peekInWorkspace != null && peekInWorkspace.isCreated())
            {
               // exists in workspace and has just been created.
               currentNode = peekInWorkspace.getNode();
               if (peekInWorkspace.isDeleted()) undeleteWorkspaceNode(peekInWorkspace, workspaceNode);
            }
         }

         if (currentNode == null)
         {
            // no child exists with this name; create it in the underlying data structure and then add it to the workspace.
            if (trace) log.trace("Creating new child, since it doesn't exist in the cache.");
            // we put the parent node into the workspace as we are changing it's children.
            // at this point "workspaceNode" refers to the parent of the current node.  It should never be null if
            // you got this far!
            if (workspaceNode.isDeleted())
            {
               //add a new one or overwrite an existing one that has been deleted
               if (trace)
                  log.trace("Parent node doesn't exist in workspace or has been deleted.  Adding to workspace.");
               workspace.addNode(workspaceNode);
               if (!(workspaceNode.getVersion() instanceof DefaultDataVersion))
                  workspaceNode.setVersioningImplicit(false);
            }
            else
            {
               if (trace) log.trace("Parent node exists: " + workspaceNode);
            }

            // get the version passed in, if we need to use explicit versioning.
            DataVersion versionToPassIn = null;
            if (isTargetFqn && !workspace.isVersioningImplicit()) versionToPassIn = version;

            NodeSPI newUnderlyingChildNode = workspaceNode.createChild(childName, workspaceNode.getNode(), cache, versionToPassIn);

            // now assign "workspaceNode" to the new child created.
//            workspaceNode = nodeFactory.createWorkspaceNode(newUnderlyingChildNode, workspace);
            workspaceNode = lockAndCreateWorkspaceNode(nodeFactory, newUnderlyingChildNode, workspace, gtx, lockAcquisitionTimeout);
            workspaceNode.setVersioningImplicit(versionToPassIn == null || !isTargetFqn);
            if (trace)
               log.trace("setting versioning of " + workspaceNode.getFqn() + " to be " + (workspaceNode.isVersioningImplicit() ? "implicit" : "explicit"));

            // now add the wrapped child node into the transaction space
            workspace.addNode(workspaceNode);
            workspaceNode.markAsCreated();
            // save in list so we can broadcast our created nodes outside
            // the synch block
            nodesCreated.add(workspaceNode.getFqn());
         }
         else
         {
            // node does exist but might not be in the workspace
            workspaceNode = workspace.getNode(currentNode.getFqn());
            // wrap it up so we can put it in later if we need to
            if (workspaceNode == null)
            {
               if (trace)
                  log.trace("Child node " + currentNode.getFqn() + " doesn't exist in workspace or has been deleted.  Adding to workspace in gtx " + gtx);

//               workspaceNode = nodeFactory.createWorkspaceNode(currentNode, workspace);
               workspaceNode = lockAndCreateWorkspaceNode(nodeFactory, currentNode, workspace, gtx, lockAcquisitionTimeout);

               // if the underlying node is a tombstone then mark the workspace node as newly created
               if (!currentNode.isValid()) workspaceNode.markAsCreated();

               if (isTargetFqn && !workspace.isVersioningImplicit())
               {
                  workspaceNode.setVersion(version);
                  workspaceNode.setVersioningImplicit(false);
               }
               else
               {
                  workspaceNode.setVersioningImplicit(true);
               }
               if (trace)
                  log.trace("setting versioning of " + workspaceNode.getFqn() + " to be " + (workspaceNode.isVersioningImplicit() ? "implicit" : "explicit"));
               workspace.addNode(workspaceNode);
            }
            else if (workspaceNode.isDeleted())
            {
               if (trace) log.trace("Found node but it is deleted in this workspace.  Needs resurrecting.");
               undeleteWorkspaceNode(workspaceNode, workspace);
View Full Code Here

   }

   @Override
   protected Object handleOptimisticPrepareMethod(InvocationContext ctx, GlobalTransaction gtx, List modifications, Map data, Address address, boolean onePhaseCommit) throws Throwable
   {
      TransactionWorkspace workspace = getTransactionWorkspace(getGlobalTransaction(ctx));

      // There is no guarantee that this collection is in any order!
      Collection<WorkspaceNode> nodes = workspace.getNodes().values();

      //we ought to have all necessary locks here so lets try and validate
      if (log.isDebugEnabled()) log.debug("Validating " + nodes.size() + " nodes.");
      for (WorkspaceNode workspaceNode : nodes)
      {
View Full Code Here

   @Override
   protected Object handleCommitMethod(InvocationContext ctx, GlobalTransaction globalTransaction) throws Throwable
   {
      GlobalTransaction gtx = getGlobalTransaction(ctx);

      TransactionWorkspace workspace;

      try
      {
         workspace = getTransactionWorkspace(gtx);
      }
      catch (CacheException e)
      {
         log.warn("we can't rollback", e);
         return nextInterceptor(ctx);
      }

      if (log.isDebugEnabled()) log.debug("Commiting successfully validated changes for GlobalTransaction " + gtx);


      Collection<WorkspaceNode> workspaceNodes = workspace.getNodes().values();

      for (WorkspaceNode workspaceNode : workspaceNodes)
      {
         NodeSPI underlyingNode = workspaceNode.getNode();

         // short circuit if this node is deleted?
         if (workspaceNode.isDeleted())
         {
            if (trace) log.trace("Workspace node " + workspaceNode.getFqn() + " deleted; removing");

            if (underlyingNode.getFqn().isRoot())
            {
               throw new CacheException("An illegal attempt to delete the root node!");
            }
            else
            {
               // mark it as invalid so any direct references are marked as such
               underlyingNode.setValid(false, true);
               // we need to update versions here, too
               performVersionUpdate(underlyingNode, workspaceNode);

               if (!useTombstones)
               {
                  // don't retain the tombstone
                  NodeSPI parent = underlyingNode.getParent();
                  if (parent == null)
                  {
                     throw new CacheException("Underlying node " + underlyingNode + " has no parent");
                  }

                  parent.removeChildDirect(underlyingNode.getFqn().getLastElement());
               }
            }
         }
         else
         {
            boolean updateVersion = false;
            if (workspaceNode.isChildrenModified() || workspaceNode.isResurrected()) // if it is newly created make sure we remove all underlying children that may exist, to solve a remove-and-create-in-tx condition
            {
               if (trace) log.trace("Updating children since node has modified children");
               // merge children.
               List<Set<Fqn>> deltas = workspaceNode.getMergedChildren();

               if (trace) log.trace("Applying children deltas to parent node " + underlyingNode.getFqn());

               if (workspaceNode.isResurrected())
               {
                  // mark it as invalid so any direct references are marked as such
                  Map childNode = underlyingNode.getChildrenMapDirect();
                  if (childNode != null)
                  {
                     for (Object o : childNode.values())
                     {
                        NodeSPI cn = (NodeSPI) o;
                        cn.setValid(false, true);
                        if (!useTombstones) underlyingNode.removeChildDirect(cn.getFqn().getLastElement());
                     }
                  }
               }

               for (Fqn child : deltas.get(0))
               {
                  NodeSPI childNode = workspace.getNode(child).getNode();
                  underlyingNode.addChildDirect(childNode);
                  childNode.setValid(true, false);
               }

               for (Fqn child : deltas.get(1))
View Full Code Here

   }

   @Override
   protected Object handleRollbackMethod(InvocationContext ctx, GlobalTransaction globalTransaction) throws Throwable
   {
      TransactionWorkspace workspace;
      workspace = getTransactionWorkspace(getGlobalTransaction(ctx));
      workspace.clearNodes();
      return nextInterceptor(ctx);
   }
View Full Code Here

TOP

Related Classes of org.jboss.cache.optimistic.TransactionWorkspace

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.