Package org.neo4j.graphdb

Examples of org.neo4j.graphdb.Transaction


  public int nextValue(RowKey rowKey, int increment) {
    return sequence( rowKey, increment );
  }

  private int sequence(RowKey rowKey, int increment) {
    Transaction tx = neo4jDb.beginTx();
    Lock lock = null;
    try {
      Node sequence = getSequence( rowKey );
      lock = tx.acquireWriteLock( sequence );
      int nextValue = updateSequenceValue( sequence, increment );
      tx.success();
      lock.release();
      return nextValue;
    }
    finally {
      tx.close();
    }
  }
View Full Code Here


     * @see org.cedj.geekseek.domain.relation.RelationRepositoryTe#add(org.cedj.geekseek.domain.model.Identifiable, java.lang.String, org.cedj.geekseek.domain.model.Identifiable)
     */
    @Override
    public Relation add(Identifiable source, final String type, Identifiable target) {

        Transaction tx = graph.beginTx();
        try {
            Node root =graph.getNodeById(0);
            String sourceTypeName = source.getClass().getSimpleName();
            String targetTypeName = target.getClass().getSimpleName();
            Node sourceTypeNode = getOrCreateNodeType(sourceTypeName);
            Node targetTypeNode = getOrCreateNodeType(targetTypeName);
            getOrCreateRelationship(root, sourceTypeNode, Named.relation(sourceTypeName));
            getOrCreateRelationship(root, targetTypeNode, Named.relation(targetTypeName));

            Node sourceNode = getOrCreateNode(source, sourceTypeName);
            getOrCreateRelationship(sourceTypeNode, sourceNode, Named.relation(REL_TYPE_ALL));
            Node targetNode = getOrCreateNode(target, targetTypeName);
            getOrCreateRelationship(targetTypeNode, targetNode, Named.relation(REL_TYPE_ALL));

            getOrCreateRelationship(sourceNode, targetNode, Named.relation(type));

            tx.success();
        } catch(Exception e) {
            tx.failure();
            throw new RuntimeException(
                "Could not add relation of type " + type + " between " + source + " and " + target, e);
        } finally {
          tx.finish();
        }
        return new Relation(source.getId(), target.getId(), type);
    }
View Full Code Here

    }

    @Override
    public void remove(Identifiable source, String type, Identifiable target) {

        Transaction tx = graph.beginTx();
        try {
            Index<Node> nodeIndex = graph.index().forNodes(PROP_INDEX_NODE);
            Index<Relationship> relationIndex = graph.index().forRelationships(PROP_INDEX_REL);

            Node sourceNode = nodeIndex.get(PROP_ID, source.getId()).getSingle();
            Node targetNode = nodeIndex.get(PROP_ID, target.getId()).getSingle();
            for(Relationship rel : sourceNode.getRelationships(Named.relation(type))) {
                if(rel.getEndNode().equals(targetNode)) {
                    rel.delete();
                    relationIndex.remove(rel);
                }
            }

            tx.success();
        } catch(Exception e) {
            tx.failure();
            throw new RuntimeException(
                "Could not add relation of type " + type + " between " + source + " and " + target, e);
        } finally {
          tx.finish();
        }
    }
View Full Code Here

        Index<Node> index = graph.index().forNodes(PROP_INDEX_NODE);
        Node node = index.get(PROP_ID, source.getId()).getSingle();
        if(node == null) {
            return targets;
        }
        Transaction tx = graph.beginTx();

        try {
            Iterable<Relationship> relationships = node.getRelationships(Named.relation(type));
            for(Relationship relation : relationships) {
                String targetId = relation.getOtherNode(node).getProperty(PROP_ID).toString();
                T target = repo.get(targetId);
                if(target == null) {
                    // Do some auto clean up if target node not found in Target repo
                    relation.getOtherNode(node).delete();
                    for(Relationship other : relation.getOtherNode(node).getRelationships()) {
                        other.delete();
                    }
                } else {
                    targets.add(target);
                }
            }
            tx.success();
        } catch(Exception e) {
            tx.failure();
            throw new RuntimeException(
                "Could not clean up relation of type " + type + " from " + source, e);
        } finally {
          tx.finish();
        }
        return targets;
    }
View Full Code Here

    Node sequenceNode = getOrCreateSequence( rowKey, initialValue );
    return updateSequence( sequenceNode, increment );
  }

  private Node getOrCreateSequence(RowKey key, final int initialValue) {
    Transaction tx = neo4jDb.beginTx();
    try {
      UniqueFactory<Node> factory = nodeFactory( initialValue );
      Node sequenceNode = factory.getOrCreate( ID_SEQUENCE_PROPERTY, generateId( key ) );
      tx.success();
      return sequenceNode;
    }
    finally {
      tx.finish();
    }
  }
View Full Code Here

    };
    return factory;
  }

  private int updateSequence(Node sequence, int increment) {
    Transaction tx = neo4jDb.beginTx();
    Lock lock = null;
    try {
      lock = tx.acquireWriteLock( sequence );
      int nextValue = updateSequenceValue( sequence, increment );
      tx.success();
      lock.release();
      return nextValue;
    }
    finally {
      tx.finish();
    }
  }
View Full Code Here

    public InputStream getInputStream() throws FileNotFoundException {
        return new FileInputStream(new File(filepath));
    }

    public void setName(String name) {
        Transaction tx = db.beginTx();
        try {
            node.setProperty(AssetDB.NAME,name);
            tx.success();
        } finally {
            tx.finish();
        }
    }
View Full Code Here

    return true;
  }

  public void intercept(InterceptorStack stack, ResourceMethod method,
      Object object) throws InterceptionException {
    Transaction tx = db.beginTx();
    try {
      stack.next(method, object);
      if(! validator.hasErrors()) {
        tx.success();
      } else {
        tx.failure();
      }
    } finally {
      tx.finish();
    }
  }
View Full Code Here

                    "Null parameter underlyingNode=" + underlyingNode
                            + " graphDb=" + graphDb );
        }
        this.underlyingNode = underlyingNode;
        this.graphDb = graphDb;
        Transaction tx = graphDb.beginTx();
        try
        {
            assertPropertyIsSame( TIMELINE_NAME, name );
            this.name = name;
            assertPropertyIsSame( TIMELINE_IS_INDEXED, indexed );
            this.indexed = indexed;
            if ( indexed )
            {
                Relationship bTreeRel = underlyingNode.getSingleRelationship(
                        BTree.RelTypes.TREE_ROOT, Direction.OUTGOING );
                if ( bTreeRel == null )
                {
                    Node bTreeNode = graphDb.createNode();
                    bTreeRel = underlyingNode.createRelationshipTo( bTreeNode,
                            BTree.RelTypes.TREE_ROOT );
                }
                indexBTree = new BTree( graphDb, bTreeRel.getEndNode() );
            }
            tx.success();
        }
        finally
        {
            tx.finish();
        }
    }
View Full Code Here

    {
        if ( nodeToAdd == null )
        {
            throw new IllegalArgumentException( "Null node" );
        }
        Transaction tx = graphDb.beginTx();
        try
        {
            for ( Relationship rel : nodeToAdd.getRelationships( RelTypes.TIMELINE_INSTANCE ) )
            {
                if ( rel.getProperty( TIMELINE_NAME, "" ).equals( name ) )
                {
                    throw new IllegalArgumentException(
                            "Node[" + nodeToAdd.getId()
                                    + "] already connected to Timeline[" + name
                                    + "]" );
                }
            }
            Relationship rel = underlyingNode.getSingleRelationship(
                    RelTypes.TIMELINE_NEXT_ENTRY, Direction.INCOMING );
            if ( rel == null )
            {
                // timeline was empty
                Node node = createNewTimeNode( timestamp, nodeToAdd );
                underlyingNode.createRelationshipTo( node,
                        RelTypes.TIMELINE_NEXT_ENTRY );
                node.createRelationshipTo( underlyingNode,
                        RelTypes.TIMELINE_NEXT_ENTRY );
                firstNode = nodeToAdd;
                lastNode = nodeToAdd;
                updateNodeAdded( timestamp );
            }
            else
            {
                Node previousLast = rel.getStartNode();
                long previousTime = (Long) previousLast.getProperty( TIMESTAMP );
                if ( timestamp > previousTime )
                {
                    // add it last in chain
                    Node node = createNewTimeNode( timestamp, nodeToAdd );
                    rel.delete();
                    previousLast.createRelationshipTo( node,
                            RelTypes.TIMELINE_NEXT_ENTRY );
                    node.createRelationshipTo( underlyingNode,
                            RelTypes.TIMELINE_NEXT_ENTRY );
                    lastNode = nodeToAdd;
                    updateNodeAdded( timestamp );
                }
                else if ( timestamp == previousTime )
                {
                  Relationship instanceRel = previousLast.createRelationshipTo( nodeToAdd,
                            RelTypes.TIMELINE_INSTANCE );
                  instanceRel.setProperty( TIMELINE_NAME, name );
                }
                else
                {
                    // find where to insert
                    Iterator<Node> itr = getAllTimeNodesAfter( timestamp ).iterator();
                    assert itr.hasNext();
                    Node next = itr.next();
                    rel = next.getSingleRelationship(
                            RelTypes.TIMELINE_NEXT_ENTRY, Direction.INCOMING );
                    assert rel != null;
                    Node previous = rel.getStartNode();
                    long previousTimestamp = Long.MIN_VALUE;
                    if ( !previous.equals( underlyingNode ) )
                    {
                        previousTimestamp = (Long) previous.getProperty( TIMESTAMP );
                    }
                    if ( previousTimestamp == timestamp )
                    {
                        // just connect previous with node to add
                      Relationship instanceRel = previous.createRelationshipTo( nodeToAdd,
                                RelTypes.TIMELINE_INSTANCE );
                      instanceRel.setProperty( TIMELINE_NAME, name );
                        return;
                    }
                    long nextTimestamp = (Long) next.getProperty( TIMESTAMP );
                    if ( nextTimestamp == timestamp )
                    {
                        // just connect next with node to add
                      Relationship instanceRel = next.createRelationshipTo( nodeToAdd,
                                RelTypes.TIMELINE_INSTANCE );
                      instanceRel.setProperty( TIMELINE_NAME, name );
                        return;
                    }

                    assert previousTimestamp < timestamp;
                    assert nextTimestamp > timestamp;

                    Node node = createNewTimeNode( timestamp, nodeToAdd );
                    rel.delete();
                    previous.createRelationshipTo( node,
                            RelTypes.TIMELINE_NEXT_ENTRY );
                    node.createRelationshipTo( next,
                            RelTypes.TIMELINE_NEXT_ENTRY );
                    if ( previous.equals( underlyingNode ) )
                    {
                        firstNode = nodeToAdd;
                    }
                    updateNodeAdded( timestamp );
                }
            }
            tx.success();
        }
        finally
        {
            tx.finish();
        }
    }
View Full Code Here

TOP

Related Classes of org.neo4j.graphdb.Transaction

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.