org.neo4j.graphdb.Node.getId()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中org.neo4j.graphdb.Node.getId()方法的一些代码示例,展示了Node.getId()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.getId()方法的具体详情如下:
包路径:org.neo4j.graphdb.Node
类名称:Node
方法名:getId

Node.getId介绍

[英]Returns the unique id of this node. Ids are garbage collected over time so they are only guaranteed to be unique during a specific time span: if the node is deleted, it's likely that a new node at some point will get the old id. Note: This makes node ids brittle as public APIs.
[中]返回此节点的唯一id。id会随着时间的推移被垃圾收集,因此它们只能在特定的时间跨度内被保证是唯一的:如果节点被删除,很可能新节点在某个时候会获得旧id。注意:这使得节点id作为公共API变得脆弱。

代码示例

代码示例来源:origin: neo4j/neo4j

private long initWithNode( GraphDatabaseService db )
{
  try ( Transaction tx = db.beginTx() )
  {
    Node theNode = db.createNode();
    long id = theNode.getId();
    tx.success();
    return id;
  }
}

代码示例来源:origin: neo4j/neo4j

private long nodeWithNoLabel( GraphDatabaseService graphDb, Object value )
{
  Node node = graphDb.createNode();
  node.setProperty( "prop", value );
  return node.getId();
}

代码示例来源:origin: neo4j/neo4j

@Test
  public void testFromSimpleGraph()
  {
    final Node n0 = gdb.createNode();

    final Node n1 = gdb.createNode();
    n1.setProperty( "name", "Node1" );
    final Relationship relationship = n0.createRelationshipTo( n1, RelationshipType.withName( "REL" ) );
    relationship.setProperty( "related", true );
    final SubGraph graph = DatabaseSubGraph.from( gdb );
    assertEquals( "create (_" + n0.getId() + ")" + lineSeparator() +
        "create (_" + n1.getId() + " {`name`:\"Node1\"})" + lineSeparator() +
        "create (_" + n0.getId() + ")-[:`REL` {`related`:true}]->(_" + n1.getId() + ")" +
          lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
  public void shouldReuseExcessBatchIdsWhichWerentUsedBeforeClose() throws Exception
  {
    // given
    Node firstNode;
    try ( Transaction tx = db.beginTx() )
    {
      firstNode = db.createNode();
      tx.success();
    }

    // when
    db.restartDatabase();

    Node secondNode;
    try ( Transaction tx = db.beginTx() )
    {
      secondNode = db.createNode();
      tx.success();
    }

    // then
    assertEquals( firstNode.getId() + 1, secondNode.getId() );
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldIterateThroughNodesInReverse()
{
  // given
  Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} );
  Iterator<Node> iterator = path.reverseNodes().iterator();
  Node node;
  // then
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 3, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 2, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 1, node.getId() );
  assertFalse( iterator.hasNext() );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void mixingBeansApiWithKernelAPI() throws Exception
{
  // 1: Start your transactions through the Beans API
  Transaction transaction = db.beginTx();
  // 2: Get a hold of a KernelAPI transaction this way:
  KernelTransaction ktx = statementContextSupplier.getKernelTransactionBoundToThisThread( true );
  // 3: Now you can interact through both the statement context and the kernel API to manipulate the
  //    same transaction.
  Node node = db.createNode();
  int labelId = ktx.tokenWrite().labelGetOrCreateForName( "labello" );
  ktx.dataWrite().nodeAddLabel( node.getId(), labelId );
  // 4: Commit through the beans API
  transaction.success();
  transaction.close();
}

代码示例来源:origin: neo4j/neo4j

@Test
public void populatingConstraintMustAcceptDatasetThatGetsUpdatedWithUniqueEntries() throws Exception
{
  // Given
  givenUniqueDataset();
  // When
  Future<?> createConstraintTransaction = applyChangesToPopulatingUpdater(
      d.getId(), a.getId(), setProperty( d, "d1" ) );
  // Then observe that our constraint was created successfully:
  createConstraintTransaction.get();
  // Future.get() will throw an ExecutionException, if the Runnable threw an exception.
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldCreateNode() throws Exception
{
  long node;
  try ( Transaction tx = beginTransaction() )
  {
    node = tx.dataWrite().nodeCreate();
    tx.success();
  }
  try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() )
  {
    assertEquals( node, graphDb.getNodeById( node ).getId() );
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldIncludeNodesCreatedInSameTxInIndexSeek()
{
  // GIVEN
  createNodes( db, LABEL, nonMatching[0], nonMatching[1] );
  MutableLongSet expected = createNodes( db, LABEL, values );
  // WHEN
  MutableLongSet found = new LongHashSet();
  try ( Transaction tx = db.beginTx() )
  {
    expected.add( createNode( db, propertyMap( keys, values ), LABEL ).getId() );
    createNode( db, propertyMap( keys, nonMatching[2] ), LABEL );
    collectNodes( found, indexSeek.findNodes( keys, values, db ) );
  }
  // THEN
  assertThat( found, equalTo( expected ) );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void indexHitsFromQueryingRemovedDoesNotReturnNegativeCount()
{
  Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG );
  Node theNode = graphDb.createNode();
  index.remove( theNode );
  try ( IndexHits<Node> hits = index.query( "someRandomKey", theNode.getId() ) )
  {
    assertTrue( hits.size() >= 0 );
  }
}

代码示例来源:origin: neo4j/neo4j

private void assertReadNode( String propValue, long expectedNodeId )
  {
    try ( Transaction tx = db.beginTx() )
    {
      Node node = db.findNode( LABEL_ONE, propKey, propValue );
      assertNotNull( node );
      assertEquals( "node id", expectedNodeId, node.getId() );
      tx.success();
    }
  }
}

代码示例来源:origin: neo4j/neo4j

private void verifyIds( EmbeddedProxySPI actions, long relationshipId, long nodeId1, int typeId, long nodeId2 )
{
  RelationshipProxy proxy = new RelationshipProxy( actions, relationshipId, nodeId1, typeId, nodeId2 );
  assertEquals( relationshipId, proxy.getId() );
  // our mock above is known to return RelationshipTypeToken
  assertEquals( nodeId1, proxy.getStartNode().getId() );
  assertEquals( nodeId1, proxy.getStartNodeId() );
  assertEquals( nodeId2, proxy.getEndNode().getId() );
  assertEquals( nodeId2, proxy.getEndNodeId() );
  assertEquals( nodeId2, proxy.getOtherNode( nodeWithId( nodeId1 ) ).getId() );
  assertEquals( nodeId2, proxy.getOtherNodeId( nodeId1 ) );
  assertEquals( nodeId1, proxy.getOtherNode( nodeWithId( nodeId2 ) ).getId() );
  assertEquals( nodeId1, proxy.getOtherNodeId( nodeId2 ) );
}

代码示例来源:origin: neo4j/neo4j

private long createNode( Map<String, Object> properties, Label... labels )
{
  try ( org.neo4j.graphdb.Transaction tx = db.beginTx() )
  {
    Node node = db.createNode( labels );
    for ( Map.Entry<String,Object> property : properties.entrySet() )
    {
      node.setProperty( property.getKey(), property.getValue() );
    }
    tx.success();
    return node.getId();
  }
}

代码示例来源:origin: neo4j/neo4j

public MutableLongSet createNodes( GraphDatabaseService db, Label label, Object[]... propertyValueTuples )
{
  MutableLongSet expected = new LongHashSet();
  try ( Transaction tx = db.beginTx() )
  {
    for ( Object[] valueTuple : propertyValueTuples )
    {
      expected.add( createNode( db, propertyMap( keys, valueTuple ), label ).getId() );
    }
    tx.success();
  }
  return expected;
}

代码示例来源:origin: neo4j/neo4j

private long createNode()
{
  long node;
  try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
  {
    node = graphDb.createNode().getId();
    ctx.success();
  }
  return node;
}

代码示例来源:origin: neo4j/neo4j

@Test
public void testFromSimpleCypherResult()
{
  Node n = gdb.createNode();
  final ExecutionResult result = result( "node", n );
  final SubGraph graph = CypherResultSubGraph.from( result, gdb, false );
  assertEquals( "create (_" + n.getId() + ")" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldPrintCypherEsqueRelationshipToString()
{
  // GIVEN
  Node start;
  Node end;
  RelationshipType type = RelationshipType.withName( "NICE" );
  Relationship relationship;
  try ( Transaction tx = db.beginTx() )
  {
    // GIVEN
    start = db.createNode();
    end = db.createNode();
    relationship = start.createRelationshipTo( end, type );
    tx.success();
    // WHEN
    String toString = relationship.toString();
    // THEN
    assertEquals( "(" + start.getId() + ")-[" + type + "," + relationship.getId() + "]->(" + end.getId() + ")",
        toString );
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldIterateThroughNodes()
{
  // given
  Path path = new PathProxy( proxySPI, new long[] {1, 2, 3}, new long[] {100, 200}, new int[] {0, ~0} );
  Iterator<Node> iterator = path.nodes().iterator();
  Node node;
  // then
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 1, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 2, node.getId() );
  assertTrue( iterator.hasNext() );
  assertThat( node = iterator.next(), instanceOf( Node.class ) );
  assertEquals( 3, node.getId() );
  assertFalse( iterator.hasNext() );
}

代码示例来源:origin: neo4j/neo4j

@Test
public void queryResultsMustIncludeNodesAddedInThisTransaction()
{
  db = createDatabase();
  try ( Transaction tx = db.beginTx() )
  {
    createSimpleNodesIndex();
    tx.success();
  }
  awaitIndexesOnline();
  try ( Transaction tx = db.beginTx() )
  {
    Node node = db.createNode( LABEL );
    node.setProperty( PROP, "value" );
    assertQueryFindsIds( db, true, "nodes", "value", newSetWith( node.getId() ) );
    tx.success();
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void canAcquireReadLock()
{
  // when
  placeboTx.acquireReadLock( resource );
  // then
  verify( locks ).acquireSharedNodeLock( resource.getId() );
}

相关文章