Package org.neo4j.graphdb

Examples of org.neo4j.graphdb.Transaction


        return node;
    }

    public long getTimestampForNode( Node node )
    {
        Transaction tx = graphDb.beginTx();
        try
        {
            Traverser traverser = node.traverse( Traverser.Order.DEPTH_FIRST,
                    StopEvaluator.END_OF_GRAPH, new ReturnableEvaluator()
                    {
                        public boolean isReturnableNode(
                                TraversalPosition position )
                        {
                            Node currentNode = position.currentNode();
                            return currentNode != null
                                   && !currentNode.hasRelationship(
                                           RelTypes.TIMELINE_INSTANCE,
                                           Direction.INCOMING );
                        }
                    }, RelTypes.TIMELINE_INSTANCE, Direction.INCOMING );

            Iterator<Node> hits = traverser.iterator();
            Long result = null;
            if ( hits.hasNext() )
            {
                Node hit = hits.next();
                result = (Long) hit.getProperty( TIMESTAMP );
            }
            else
            {
                throw new RuntimeException(
                        "No timpestamp found for '" + node
                                + "' maybe it's not in the timeline?" );
            }
            tx.success();
            return result;
        }
        finally
        {
            tx.finish();
        }
    }
View Full Code Here


        }
        if ( nodeToRemove.equals( underlyingNode ) )
        {
            throw new IllegalArgumentException( "Cannot remove underlying node" );
        }
        Transaction tx = graphDb.beginTx();
        try
        {
            Relationship instanceRel = null;
            for ( Relationship rel : nodeToRemove.getRelationships( RelTypes.TIMELINE_INSTANCE ) )
            {
                if ( rel.getProperty( TIMELINE_NAME, "" ).equals( name ) )
                {
                    assert instanceRel == null;
                    instanceRel = rel;
                }
            }
            if ( instanceRel == null )
            {
                throw new IllegalArgumentException(
                        "Node[" + nodeToRemove.getId()
                                + "] not added to Timeline[" + name + "]" );
            }
            Node node = instanceRel.getStartNode();
            instanceRel.delete();
            if ( firstNode != null && firstNode.equals( nodeToRemove ) )
            {
                firstNode = null;
            }
            if ( lastNode != null && lastNode.equals( nodeToRemove ) )
            {
                lastNode = null;
            }
            if ( node.getRelationships( RelTypes.TIMELINE_INSTANCE ).iterator().hasNext() )
            {
                // still have instances connected to this time
                return;
            }
            Relationship incoming = node.getSingleRelationship(
                    RelTypes.TIMELINE_NEXT_ENTRY, Direction.INCOMING );
            if ( incoming == null )
            {
                throw new RuntimeException( "No incoming relationship of "
                                            + RelTypes.TIMELINE_NEXT_ENTRY
                                            + " found" );
            }
            Relationship outgoing = node.getSingleRelationship(
                    RelTypes.TIMELINE_NEXT_ENTRY, Direction.OUTGOING );
            if ( outgoing == null )
            {
                throw new RuntimeException( "No outgoing relationship of "
                                            + RelTypes.TIMELINE_NEXT_ENTRY
                                            + " found" );
            }
            Node previous = incoming.getStartNode();
            Node next = outgoing.getEndNode();
            incoming.delete();
            outgoing.delete();
            // TODO: this needs proper synchronization
            if ( node.hasProperty( INDEX_COUNT ) )
            {
                long nodeId = (Long) indexBTree.removeEntry( (Long) node.getProperty( TIMESTAMP ) );
                assert nodeId == node.getId();
                int count = (Integer) node.getProperty( INDEX_COUNT );
                count--;
                if ( !previous.equals( underlyingNode )
                     && !previous.hasProperty( INDEX_COUNT ) )
                {
                    previous.setProperty( INDEX_COUNT, count );
                    indexBTree.addEntry(
                            (Long) previous.getProperty( TIMESTAMP ),
                            previous.getId() );
                }
            }
            else
            {
                long timestamp = (Long) node.getProperty( TIMESTAMP );
                if ( indexed )
                {
                    Long nodeId = (Long) indexBTree.getClosestHigherEntry( timestamp );
                    if ( nodeId != null )
                    {
                        Node indexedNode = graphDb.getNodeById( nodeId );
                        int count = (Integer) indexedNode.getProperty( INDEX_COUNT );
                        count--;
                        indexedNode.setProperty( INDEX_COUNT, count );
                    }
                    else
                    {
                        if ( underlyingNode.hasProperty( INDEX_COUNT ) )
                        {
                            int count = (Integer) underlyingNode.getProperty( INDEX_COUNT );
                            count--;
                            underlyingNode.setProperty( INDEX_COUNT, count );
                        }
                    }
                }
            }
            node.delete();
            if ( !previous.equals( next ) )
            {
                previous.createRelationshipTo( next,
                        RelTypes.TIMELINE_NEXT_ENTRY );
            }
            tx.success();
        }
        finally
        {
            if(transactional)
            {
                tx.finish();
            }
        }
    }
View Full Code Here

        "Null parameter underlyingNode=" + underlyingNode +
        " graphDb=" + graphDb );
    }
    this.underlyingNode = underlyingNode;
    this.graphDb = graphDb;
    Transaction tx = graphDb.beginTx();
    try
    {
      if ( underlyingNode.hasProperty( MAP_NAME ) )
      {
        String storedName = (String) underlyingNode.getProperty(
          MAP_NAME );
        if ( name != null && !storedName.equals( name ) )
        {
          throw new IllegalArgumentException( "Name of map " +
            "for node=" + underlyingNode.getId() + "," +
            storedName + " is not same as passed in name=" +
            name );
        }
        if ( name == null )
        {
          this.name = (String) underlyingNode.getProperty(
            MAP_NAME );
        }
        else
        {
          this.name = name;
        }
      }
      else
      {
        underlyingNode.setProperty( MAP_NAME, name );
        this.name = name;
      }
      Relationship bTreeRel = underlyingNode.getSingleRelationship(
        BTree.RelTypes.TREE_ROOT,
        Direction.OUTGOING );
      if ( bTreeRel != null )
      {
        bTree = new BTree( graphDb, bTreeRel.getEndNode() );
      }
      else
      {
        Node bTreeNode = graphDb.createNode();
        underlyingNode.createRelationshipTo( bTreeNode,
          BTree.RelTypes.TREE_ROOT );
        bTree = new BTree( graphDb, bTreeNode );
      }
      tx.success();
    }
    finally
    {
      tx.finish();
    }
  }
View Full Code Here

  {
    if ( key == null || value == null )
    {
      throw new IllegalArgumentException( "Null node" );
    }
    Transaction tx = graphDb.beginTx();
    try
    {
      int hashCode = key.hashCode();
      KeyEntry entry = bTree.addIfAbsent( hashCode, value );
      if ( entry != null )
      {
        entry.setKeyValue( key );
      }
      else
      {
        entry = bTree.getAsKeyEntry( hashCode );
        Object goOtherNode = entry.getKeyValue();
        Node bucketNode = null;
        if ( !goOtherNode.equals( GOTO_NODE ) )
        {
          Object prevValue = entry.getValue();
          Object prevKey = entry.getKeyValue();
          if ( prevKey.equals( key ) )
          {
            Object oldValue = entry.getValue();
            entry.setValue( value );
            tx.success();
            return (V) oldValue;
          }
          entry.setKeyValue( GOTO_NODE );
          bucketNode = graphDb.createNode();
          entry.setValue( bucketNode.getId() );
          Node prevEntry = graphDb.createNode();
          bucketNode.createRelationshipTo( prevEntry,
            RelTypes.MAP_ENTRY );
          prevEntry.setProperty( MAP_KEY, prevKey );
          prevEntry.setProperty( MAP_VALUE, prevValue );
          Node newEntry = graphDb.createNode();
          bucketNode.createRelationshipTo( newEntry,
            RelTypes.MAP_ENTRY );
          newEntry.setProperty( MAP_KEY, key );
          newEntry.setProperty( MAP_VALUE, value );
        }
        else
        {
          bucketNode = graphDb.getNodeById( (Long) entry.getValue() );
          for ( Relationship rel : bucketNode.getRelationships(
            RelTypes.MAP_ENTRY, Direction.OUTGOING ) )
          {
            Node entryNode = rel.getEndNode();
            if ( entryNode.getProperty( MAP_KEY ).equals( key ) )
            {
              entryNode.setProperty( MAP_VALUE, value );
              tx.success();
              return null;
            }
          }
          Node newEntry = graphDb.createNode();
          bucketNode.createRelationshipTo( newEntry,
            RelTypes.MAP_ENTRY );
          newEntry.setProperty( MAP_KEY, key );
          newEntry.setProperty( MAP_VALUE, value );
        }
      }
      tx.success();
      return null;
    }
    finally
    {
      tx.finish();
    }
  }
View Full Code Here

    }
  }
 
  public V remove( Object key )
  {
    Transaction tx = graphDb.beginTx();
    try
    {
      int hashCode = key.hashCode();
      KeyEntry entry = bTree.getAsKeyEntry( hashCode );
      if ( entry != null )
      {
        Object goOtherNode = entry.getKeyValue();
        if ( !goOtherNode.equals( GOTO_NODE ) )
        {
          if ( goOtherNode.equals( key ) )
          {
            Object value = entry.getValue();
            entry.remove();
            tx.success();
            return (V) value;
          }
        }
        else
        {
          Node bucketNode = graphDb.getNodeById(
            (Long) entry.getValue() );
          for ( Relationship rel : bucketNode.getRelationships(
            RelTypes.MAP_ENTRY, Direction.OUTGOING ) )
          {
            Node entryNode = rel.getEndNode();
            if ( entryNode.getProperty( MAP_KEY ).equals( key ) )
            {
              Object value = entryNode.getProperty( MAP_VALUE );
              rel.delete();
              entryNode.delete();
              tx.success();
              return (V) value;
            }
          }
        }
      }
      tx.success();
      return null;
    }
    finally
    {
      tx.finish();
    }
  }
View Full Code Here

    bTree.validateTree();
  }
 
  public V get( Object key )
  {
    Transaction tx = graphDb.beginTx();
    try
    {
      int hashCode = key.hashCode();
      KeyEntry entry = bTree.getAsKeyEntry( hashCode );
      if ( entry != null )
      {
        Object goOtherNode = entry.getKeyValue();
        if ( !goOtherNode.equals( GOTO_NODE ) )
        {
          if ( goOtherNode.equals( key ) )
          {
            tx.success();
            return (V) entry.getValue();
          }
        }
        else
        {
          Node bucketNode = graphDb.getNodeById(
            (Long) entry.getValue() );
          for ( Relationship rel : bucketNode.getRelationships(
            RelTypes.MAP_ENTRY, Direction.OUTGOING ) )
          {
            Node entryNode = rel.getEndNode();
            if ( entryNode.getProperty( MAP_KEY ).equals( key ) )
            {
              tx.success();
              return (V) entryNode.getProperty( MAP_VALUE );
            }
          }
        }
      }
      tx.success();
      return null;
    }
    finally
    {
      tx.finish();
    }
  }
View Full Code Here

    }

    @Test
    public void testAddToIndex() throws Exception {
        Transaction tx = db.beginTx();
        Map<String, String> config = TimelineNodeIndex.CONFIG;
        IndexManager indexMan = db.index();
        Index<Node> index = indexMan.forNodes("timeline1", config);
        assertNotNull(index);
        Node n1 = db.createNode();
        n1.setProperty("time", 123);
        index.add(n1, "timestamp", 123L);
        Node n2 = db.createNode();
        n2.setProperty("time", 123);
        index.add(n2, "timestamp", 123L);
        Node n3 = db.createNode();
        n3.setProperty("time", 124);
        index.add(n3, "timestamp", 124L);
        tx.success();
        tx.finish();

        tx = db.beginTx();
        GraphvizWriter writer = new GraphvizWriter();
        writer.emit(System.out, Walker.fullGraph(db));
        IndexHits<Node> hits = index.get("timestamp", 123L);
        assertEquals(2, hits.size());
        hits = index.query("[122 TO 125]");
        assertEquals(3, hits.size());

        ExecutionEngine engine = new ExecutionEngine(db);
        ExecutionResult result = engine.execute("start n=node:timeline1('[100 TO 200]') return n");
        System.out.println(result.toString());
        tx.success();
        tx.finish();
    }
View Full Code Here

        FutureTask<Boolean> reader = new ThrowingFutureTask<Boolean>( new Callable<Boolean>()
        {
            @Override
            public Boolean call() throws Exception
            {
                Transaction tx = graphDb().beginTx();
                try
                {
                    ArrayList<Node> innerNodes = new ArrayList<Node>( nodes.subList( 0, 3 ) );
                    Collections.reverse( innerNodes );
                    assertTrue( sync.wait( States.READ ) );
                    UnrolledLinkedList innerList = new UnrolledLinkedList( list.getBaseNode() );

                    int count = 0;
                    for ( Node node : innerList )
                    {
                        assertEquals( innerNodes.get( count ), node );
                        if ( ++count == 2 )
                        {
                            // wont receive read signal as writer will be waiting to gain a write lock on base node
                            // will timeout then finish its reading process, whereby the writer will gain write lock
                            // and complete its write process
                            assertFalse( sync.signalAndWait( States.WRITE, States.READ, 1, TimeUnit.SECONDS ) );
                        }
                    }

                    tx.success();
                    return true;
                }
                finally
                {
                    tx.finish();
                }
            }
        } );
        executorService.execute( reader );
View Full Code Here

        return cleanDb(Long.MAX_VALUE);
    }

    public Map<String, Object> cleanDb(long maxNodesToDelete) {
        Map<String, Object> result = new HashMap<String, Object>();
        Transaction tx = graph.beginTx();
        try {
            clearIndex(result);
            removeNodes(result, maxNodesToDelete);
            tx.success();
        } finally {
            tx.finish();
        }
        return result;
    }
View Full Code Here

    private String createDeleteURI(String key) {
        return String.format(neoServer.baseUri().toString()  + "%s/%s", CONTEXT_PATH, key);
    }

    private void createData(GraphDatabaseAPI db, int max) {
        Transaction tx = db.beginTx();
        try {
            final IndexManager indexManager = db.index();
            Node[] nodes = new Node[max];
            for (int i = 0; i < max; i++) {
                nodes[i] = db.createNode();
                final Index<Node> index = indexManager.forNodes("node_index_" + String.valueOf(i % 5));
                index.add(nodes[i],"ID",i);
            }
            Random random = new Random();
            for (int i = 0; i < max * 2; i++) {
                int from = random.nextInt(max);
                final int to = (from + 1 + random.nextInt(max - 1)) % max;
                final Relationship relationship = nodes[from].createRelationshipTo(nodes[to], DynamicRelationshipType.withName("TEST_" + i));
                final Index<Relationship> index = indexManager.forRelationships("rel_index_" + String.valueOf(i % 5));
                index.add(relationship, "ID", i);
            }
            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.