void addNode( long id, Node data ) { nodes.put( id, data ); labels.addAll( Iterables.asCollection( data.getLabels() ) ); }
private String labelString( Node node ) { Iterator<Label> labels = node.getLabels().iterator(); if ( !labels.hasNext() ) { return ""; } StringBuilder result = new StringBuilder(); while ( labels.hasNext() ) { Label next = labels.next(); result.append( ':' ).append( quote( next.name() ) ); } return result.toString(); }
public ListRepresentation getNodeLabels( long nodeId ) throws NodeNotFoundException { Iterable<String> labels = new IterableWrapper<String,Label>( node( nodeId ).getLabels() ) { @Override protected String underlyingObjectToObject( Label object ) { return object.name(); } }; return ListRepresentation.string( labels ); }
@Override protected boolean matchesSafely( Node item, Description mismatchDescription ) { foundLabels = asLabelNameSet( item.getLabels() ); if ( foundLabels.size() == expectedLabels.size() && foundLabels.containsAll( expectedLabels ) ) { return true; } mismatchDescription.appendText( "was " + foundLabels.toString() ); return false; } };
private static String labelsOf( Node node ) { StringBuilder builder = new StringBuilder(); for ( Label label : node.getLabels() ) { builder.append( label.name() + " " ); } return builder.toString(); }
@Override protected boolean matchesSafely( Node item, Description mismatchDescription ) { boolean result = item.hasLabel( myLabel ); if ( !result ) { Set<String> labels = asLabelNameSet( item.getLabels() ); mismatchDescription.appendText( labels.toString() ); } return result; } };
private static Node mockNode( long id, Label[] labels, Properties properties ) { Node node = mockPropertyContainer( Node.class, properties ); when( node.getId() ).thenReturn( id ); when( node.getLabels() ).thenReturn( Iterables.asResourceIterable( asList( labels ) ) ); return node; }
private void assertNoLabels( long nodeId ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getNodeById( nodeId ).getLabels(), equalTo( Iterables.empty() ) ); } }
private void assertLabels( long nodeId, String label ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getNodeById( nodeId ).getLabels(), containsInAnyOrder( label( label ) ) ); } }
public Iterable<String> getNodeLabels( long node ) { return new IterableWrapper<String, Label>( database.getGraph().getNodeById( node ).getLabels() ) { @Override protected String underlyingObjectToObject( Label object ) { return object.name(); } }; }
private void modifyLabelsOnExistingNode( List<Pair<Long,Label[]>> existingNodes ) { int targetIndex = random.nextInt( existingNodes.size() ); Pair<Long,Label[]> existingPair = existingNodes.get( targetIndex ); long nodeId = existingPair.first(); Node node = db.getNodeById( nodeId ); node.getLabels().forEach( node::removeLabel ); Label[] newLabels = randomLabels(); for ( Label label : newLabels ) { node.addLabel( label ); } existingNodes.remove( targetIndex ); existingNodes.add( Pair.of( nodeId, newLabels ) ); }
private void modifyLabelsOnExistingNode( List<Pair<Long,Label[]>> existingNodes ) { int targetIndex = random.nextInt( existingNodes.size() ); Pair<Long,Label[]> existingPair = existingNodes.get( targetIndex ); long nodeId = existingPair.first(); Node node = db.getNodeById( nodeId ); node.getLabels().forEach( node::removeLabel ); Label[] newLabels = randomLabels(); for ( Label label : newLabels ) { node.addLabel( label ); } existingNodes.remove( targetIndex ); existingNodes.add( Pair.of( nodeId, newLabels ) ); }
private void verifyLabels( long nodeId, int startLabelIndex, int count ) { try ( Transaction tx = dbRule.beginTx() ) { Node node = dbRule.getNodeById( nodeId ); Set<String> labelNames = Iterables.asList( node.getLabels() ) .stream() .map( Label::name ) .sorted() .collect( toSet() ); assertEquals( count, labelNames.size() ); int endLabelIndex = startLabelIndex + count; for ( int i = startLabelIndex; i < endLabelIndex; i++ ) { assertTrue( labelNames.contains( labelName( i ) ) ); } tx.success(); } }
private void assertNodeEquals( InputEntity input, Node node ) { // properties assertPropertiesEquals( input, node ); // labels Set<String> expectedLabels = asSet( input.labels() ); for ( Label label : node.getLabels() ) { assertTrue( expectedLabels.remove( label.name() ) ); } assertTrue( expectedLabels.isEmpty() ); }
@Test public void shouldGiveNiceErrorWhenShutdownKernelApi() { GraphDatabaseService graphDb = graph; Node node = graphDb.createNode(); commit(); graphDb.shutdown(); try { asList( node.getLabels().iterator() ); fail( "Did not get a nice exception" ); } catch ( DatabaseShutdownException e ) { // good } }
private static void doStuff( GraphDatabaseService db, Label label, String propertyKey ) { try ( Transaction tx = db.beginTx() ) { for ( Node node : loop( db.findNodes( label, propertyKey, 3323 ) ) ) { count( node.getLabels() ); } } }
private long countNodes( String... labels ) { Set<Label> givenLabels = new HashSet<>( labels.length ); for ( String label : labels ) { givenLabels.add( Label.label( label ) ); } try ( Transaction transaction = graphdb().beginTx() ) { long count = 0; for ( Node node : graphdb().getAllNodes() ) { Set<Label> nodeLabels = Iterables.asSet( node.getLabels() ); if ( nodeLabels.containsAll( givenLabels ) ) { count++; } } transaction.failure(); return count; } }
@Mapping( "metadata" ) public MapRepresentation metadata() { if ( isDeleted() ) { return new MapRepresentation( map( "id", node.getId(), "deleted", Boolean.TRUE ) ); } else { Collection<String> labels = Iterables.asCollection( new IterableWrapper<String,Label>( node.getLabels() ) { @Override protected String underlyingObjectToObject( Label label ) { return label.name(); } } ); return new MapRepresentation( map( "id", node.getId(), "labels", labels ) ); } }
public long getLabelCount( long nodeId ) { try ( Transaction transaction = database.getGraph().beginTransaction( implicit, AnonymousContext.read() ) ) { return count( database.getGraph().getNodeById( nodeId ).getLabels()); } } }
@Test public void removingLabelDoesNotBreakPreviouslyCreatedLabelsIterator() { // GIVEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); Label label1 = label( "A" ); Label label2 = label( "B" ); try ( Transaction tx = db.beginTx() ) { Node node = db.createNode( label1, label2 ); for ( Label next : node.getLabels() ) { node.removeLabel( next ); } tx.success(); } }