public Node createNode() {
   int id = idGenerator.nextId(Node.class);
   NodeImpl node = new NodeImpl(id, true, this);
   acquireLock(node, LockType.WRITE);
   boolean success = false;
   try {
     persistenceManager.nodeCreate(id);
     nodeCache.put((int) node.getId(), node);
     success = true;
     return new NodeProxy(id, this);
   } finally {
     releaseLock(node, LockType.WRITE);
     if (!success) {
       setRollbackOnly();
     }
   }
 }
예제 #2
0
 public void delete(NodeManager nodeManager) {
   nodeManager.acquireLock(this, LockType.WRITE);
   boolean success = false;
   try {
     ArrayMap<Integer, PropertyData> skipMap = nodeManager.getCowPropertyRemoveMap(this, true);
     ArrayMap<Integer, PropertyData> removedProps = nodeManager.deleteNode(this);
     if (removedProps.size() > 0) {
       for (int index : removedProps.keySet()) {
         skipMap.put(index, removedProps.get(index));
       }
     }
     success = true;
   } finally {
     nodeManager.releaseLock(this, LockType.WRITE);
     if (!success) {
       nodeManager.setRollbackOnly();
     }
   }
 }
  public Relationship createRelationship(Node startNode, Node endNode, RelationshipType type) {
    if (startNode == null || endNode == null || type == null) {
      throw new IllegalArgumentException(
          "Null parameter, startNode=" + startNode + ", endNode=" + endNode + ", type=" + type);
    }

    if (!relTypeHolder.isValidRelationshipType(type)) {
      relTypeHolder.addValidRelationshipType(type.name(), true);
    }
    int startNodeId = (int) startNode.getId();
    NodeImpl firstNode = getLightNode(startNodeId);
    if (firstNode == null) {
      setRollbackOnly();
      throw new NotFoundException("First node[" + startNode.getId() + "] deleted");
    }
    int endNodeId = (int) endNode.getId();
    NodeImpl secondNode = getLightNode(endNodeId);
    if (secondNode == null) {
      setRollbackOnly();
      throw new NotFoundException("Second node[" + endNode.getId() + "] deleted");
    }
    int id = idGenerator.nextId(Relationship.class);
    RelationshipImpl rel = new RelationshipImpl(id, startNodeId, endNodeId, type, true, this);
    boolean firstNodeTaken = false;
    boolean secondNodeTaken = false;
    acquireLock(rel, LockType.WRITE);
    boolean success = false;
    try {
      acquireLock(firstNode, LockType.WRITE);
      firstNodeTaken = true;
      acquireLock(secondNode, LockType.WRITE);
      secondNodeTaken = true;
      int typeId = getRelationshipTypeIdFor(type);
      persistenceManager.relationshipCreate(id, typeId, startNodeId, endNodeId);
      firstNode.addRelationship(type, id);
      secondNode.addRelationship(type, id);
      relCache.put((int) rel.getId(), rel);
      success = true;
      return new RelationshipProxy(id, this);
    } finally {
      boolean releaseFailed = false;
      if (firstNodeTaken) {
        try {
          releaseLock(firstNode, LockType.WRITE);
        } catch (Exception e) {
          releaseFailed = true;
          e.printStackTrace();
          log.severe("Failed to release lock");
        }
      }
      if (secondNodeTaken) {
        try {
          releaseLock(secondNode, LockType.WRITE);
        } catch (Exception e) {
          releaseFailed = true;
          e.printStackTrace();
          log.severe("Failed to release lock");
        }
      }
      releaseLock(rel, LockType.WRITE);
      if (!success) {
        setRollbackOnly();
      }
      if (releaseFailed) {
        throw new LockException(
            "Unable to release locks ["
                + startNode
                + ","
                + endNode
                + "] in relationship create->"
                + rel);
      }
    }
  }
예제 #4
0
  public void delete(NodeManager nodeManager) {
    NodeImpl startNode = null;
    NodeImpl endNode = null;
    boolean startNodeLocked = false;
    boolean endNodeLocked = false;
    boolean thisLocked = false;
    boolean success = false;
    try {
      startNode = nodeManager.getLightNode(getStartNodeId());
      if (startNode != null) {
        nodeManager.acquireLock(startNode, LockType.WRITE);
        startNodeLocked = true;
      }
      endNode = nodeManager.getLightNode(getEndNodeId());
      if (endNode != null) {
        nodeManager.acquireLock(endNode, LockType.WRITE);
        endNodeLocked = true;
      }
      nodeManager.acquireLock(this, LockType.WRITE);
      thisLocked = true;
      // no need to load full relationship, all properties will be
      // deleted when relationship is deleted

      ArrayMap<Integer, PropertyData> skipMap = nodeManager.getCowPropertyRemoveMap(this, true);
      ArrayMap<Integer, PropertyData> removedProps = nodeManager.deleteRelationship(this);
      if (removedProps.size() > 0) {
        for (int index : removedProps.keySet()) {
          skipMap.put(index, removedProps.get(index));
        }
      }
      success = true;
      RelationshipType type = getType(nodeManager);
      long id = getId();
      if (startNode != null) {
        startNode.removeRelationship(nodeManager, type, id);
      }
      if (endNode != null) {
        endNode.removeRelationship(nodeManager, type, id);
      }
      success = true;
    } finally {
      boolean releaseFailed = false;
      try {
        if (thisLocked) {
          nodeManager.releaseLock(this, LockType.WRITE);
        }
      } catch (Exception e) {
        releaseFailed = true;
        e.printStackTrace();
      }
      try {
        if (startNodeLocked) {
          nodeManager.releaseLock(startNode, LockType.WRITE);
        }
      } catch (Exception e) {
        releaseFailed = true;
        e.printStackTrace();
      }
      try {
        if (endNodeLocked) {
          nodeManager.releaseLock(endNode, LockType.WRITE);
        }
      } catch (Exception e) {
        releaseFailed = true;
        e.printStackTrace();
      }
      if (!success) {
        nodeManager.setRollbackOnly();
      }
      if (releaseFailed) {
        throw new LockException(
            "Unable to release locks ["
                + startNode
                + ","
                + endNode
                + "] in relationship delete->"
                + this);
      }
    }
  }