private WorkerCommand<Void,Void> forceLogRotation( GraphDatabaseAPI db ) { return state -> { try ( Transaction tx = db.beginTx() ) { db.createNode( label ).setProperty( "a", 1 ); tx.success(); } db.getDependencyResolver().resolveDependency( LogRotation.class ).rotateLogFile(); return null; }; }
@After public void after() { db.shutdown(); }
@Override public Result execute( String query, Map<String, Object> parameters ) throws QueryExecutionException { return getGraphDatabaseAPI().execute( query, parameters ); }
private Node node( long id ) throws NodeNotFoundException { try ( Transaction tx = graphDb.beginTx() ) { Node node = graphDb.getNodeById( id ); tx.success(); return node; } catch ( NotFoundException e ) { throw new NodeNotFoundException( e ); } }
public boolean nodeIsIndexed( String indexName, String key, Object value, long nodeId ) { Index<Node> index = graphDb.index().forNodes( indexName ); Node expectedNode = graphDb.getNodeById( nodeId ); try ( IndexHits<Node> hits = index.get( key, value ) ) { return iterableContains( hits, expectedNode ); } }
public boolean relationshipIsIndexed( String indexName, String key, Object value, long relationshipId ) { Index<Relationship> index = graphDb.index().forRelationships( indexName ); Relationship expectedNode = graphDb.getRelationshipById( relationshipId ); try ( IndexHits<Relationship> hits = index.get( key, value ) ) { return iterableContains( hits, expectedNode ); } }
private void getOrCreateIds() throws KernelException { try ( Transaction tx = graphDb.beginTx() ) { ThreadToStatementContextBridge bridge = graphDb.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ); TokenWrite tokenWrite = bridge.getKernelTransactionBoundToThisThread( true ).tokenWrite(); labelId = tokenWrite.labelGetOrCreateForName( "Person" ); relTypeId = tokenWrite.relationshipTypeGetOrCreateForName( "Knows" ); propertyKeyId = tokenWrite.propertyKeyGetOrCreateForName( "name" ); relPropertyKeyId = tokenWrite.propertyKeyGetOrCreateForName( "duration" ); tx.success(); } }
private Node createNodeWithProperty( String key, String value, Label... labels ) { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode( labels ); node.setProperty( key, value ); tx.success(); return node; } }
@Test public void queryResultsMustNotIncludeRelationshipsWithRemovedIndexedProperties() { db = createDatabase(); try ( Transaction tx = db.beginTx() ) { createSimpleRelationshipIndex(); tx.success(); } long relId; try ( Transaction tx = db.beginTx() ) { awaitIndexesOnline(); Node node = db.createNode(); Relationship rel = node.createRelationshipTo( node, REL ); rel.setProperty( PROP, "value" ); relId = rel.getId(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.getRelationshipById( relId ).removeProperty( PROP ); assertQueryFindsIds( db, false, "rels", "value" ); tx.success(); } }
@Test public void queryShouldFindDataAddedInLaterTransactions() { db = createDatabase(); db.execute( format( NODE_CREATE, "node", array( "Label1", "Label2" ), array( "prop1", "prop2" ) ) ).close(); db.execute( format( RELATIONSHIP_CREATE, "rel", array( "Reltype1", "Reltype2" ), array( "prop1", "prop2" ) ) ).close(); awaitIndexesOnline(); long horseId; long horseRelId; try ( Transaction tx = db.beginTx() ) { Node zebra = db.createNode(); zebra.setProperty( "prop1", "zebra" ); Node horse = db.createNode( Label.label( "Label1" ) ); horse.setProperty( "prop2", "horse" ); horse.setProperty( "prop3", "zebra" ); Relationship horseRel = zebra.createRelationshipTo( horse, RelationshipType.withName( "Reltype1" ) ); horseRel.setProperty( "prop1", "horse" ); Relationship loop = horse.createRelationshipTo( horse, RelationshipType.withName( "loop" ) ); loop.setProperty( "prop2", "zebra" ); horseId = horse.getId(); horseRelId = horseRel.getId(); tx.success(); } assertQueryFindsIds( db, true, "node", "horse", newSetWith( horseId ) ); assertQueryFindsIds( db, true, "node", "horse zebra", newSetWith( horseId ) ); assertQueryFindsIds( db, false, "rel", "horse", newSetWith( horseRelId ) ); assertQueryFindsIds( db, false, "rel", "horse zebra", newSetWith( horseRelId ) ); }
@Test public void shouldNotNotifyOnDynamicPropertyLookupWithLabelHavingNoIndex() { Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.5" ).forEach( version -> { db().execute( "CREATE INDEX ON :Person(name)" ); db().execute( "Call db.awaitIndexes()" ); try ( Transaction tx = db().beginTx() ) { db().createNode().addLabel( label( "Foo" ) ); tx.success(); } shouldNotNotifyInStream( version, "EXPLAIN MATCH (n:Foo) WHERE n['key-' + n.name] = 'value' RETURN n" ); } ); }
private Set<IndexEntryUpdate<?>> createSomeBananas( Label label ) { Set<IndexEntryUpdate<?>> updates = new HashSet<>(); try ( Transaction tx = db.beginTx() ) { ThreadToStatementContextBridge ctxSupplier = db.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ); KernelTransaction ktx = ctxSupplier.getKernelTransactionBoundToThisThread( true ); int labelId = ktx.tokenRead().nodeLabel( label.name() ); int propertyKeyId = ktx.tokenRead().propertyKey( key ); LabelSchemaDescriptor schemaDescriptor = SchemaDescriptorFactory.forLabel( labelId, propertyKeyId ); for ( int number : new int[]{4, 10} ) { Node node = db.createNode( label ); node.setProperty( key, number ); updates.add( IndexEntryUpdate.add( node.getId(), schemaDescriptor, Values.of( number ) ) ); } tx.success(); return updates; } }
private void createAlistairAndStefanNodes() { try ( Transaction tx = graphDb.beginTx() ) { alistair = graphDb.createNode( label ); alistair.setProperty( "name", "Alistair" ); alistair.setProperty( "country", "UK" ); stefan = graphDb.createNode( label ); stefan.setProperty( "name", "Stefan" ); stefan.setProperty( "country", "Deutschland" ); aKnowsS = alistair.createRelationshipTo( stefan, relationshipType ); aKnowsS.setProperty( "duration", "long" ); aKnowsS.setProperty( "irrelevant", "prop" ); sKnowsA = stefan.createRelationshipTo( alistair, relationshipType ); sKnowsA.setProperty( "duration", "lengthy" ); sKnowsA.setProperty( "irrelevant", "prop" ); tx.success(); } }
@Test public void shouldTellIfRelExists() { // Given long node = createLabeledNode( db, map() ).getId(); long created; long createdAndRemoved; long neverExisted; try ( Transaction tx = db.beginTx() ) { created = db.createNode().createRelationshipTo( db.createNode(), withName( "Banana" ) ).getId(); createdAndRemoved = db.createNode().createRelationshipTo( db.createNode(), withName( "Banana" ) ).getId(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.getRelationshipById( createdAndRemoved ).delete(); tx.success(); } neverExisted = created + 99; // When & then assertTrue( relationshipExists( node )); assertFalse( relationshipExists( createdAndRemoved ) ); assertFalse( relationshipExists( neverExisted ) ); }
try ( Transaction tx = db.beginTx() ) node.addLabel( myLabel ); tx.success(); try ( Transaction tx = db.beginTx() ) ctxSupplier.getKernelTransactionBoundToThisThread( true ); TokenRead tokenRead = ktx.tokenRead(); int propertyKey1 = tokenRead.propertyKey( indexProperty ); int label = tokenRead.nodeLabel( myLabel.name() ); LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( label, propertyKey1 ); assertThat( writer.updatesCommitted, equalTo( asSet( IndexEntryUpdate.add( node.getId(), descriptor, Values.of( value ) ) ) ) ); tx.success();
@Test void skipMigrationIfFormatSpecifiedInConfig() { GraphDatabaseService database = startDatabaseWithFormatUnspecifiedUpgrade( storeDir, StandardV3_2.NAME ); try ( Transaction transaction = database.beginTx() ) { Node node = database.createNode(); node.setProperty( "a", "b" ); transaction.success(); } database.shutdown(); GraphDatabaseAPI nonUpgradedStore = (GraphDatabaseAPI) startDatabaseWithFormatUnspecifiedUpgrade( storeDir, StandardV3_2.NAME ); RecordStorageEngine storageEngine = nonUpgradedStore.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); assertEquals( StandardV3_2.NAME, storageEngine.testAccessNeoStores().getRecordFormats().name() ); nonUpgradedStore.shutdown(); }
@Test public void eventuallyConsistenIndexMustNotIncludeEntitiesAddedInTransaction() { db = createDatabase(); try ( Transaction tx = db.beginTx() ) { db.execute( format( NODE_CREATE, "nodes", array( LABEL.name() ), array( PROP ) + EVENTUALLY_CONSISTENT ) ).close(); db.execute( format( RELATIONSHIP_CREATE, "rels", array( REL.name() ), array( PROP ) + EVENTUALLY_CONSISTENT ) ).close(); tx.success(); } try ( Transaction tx = db.beginTx() ) { awaitIndexesOnline(); Node node = db.createNode( LABEL ); node.setProperty( PROP, "value" ); node.createRelationshipTo( node, REL ).setProperty( PROP, "value" ); assertQueryFindsIds( db, true, "nodes", "value" ); assertQueryFindsIds( db, false, "rels", "value" ); db.execute( AWAIT_REFRESH ).close(); assertQueryFindsIds( db, true, "nodes", "value" ); assertQueryFindsIds( db, false, "rels", "value" ); tx.success(); } }
private static void generateTransaction( GraphDatabaseAPI database ) { try ( Transaction transaction = database.beginTx() ) { Node startNode = database.createNode( Label.label( "startNode" ) ); startNode.setProperty( "key", "value" ); Node endNode = database.createNode( Label.label( "endNode" ) ); endNode.setProperty( "key", "value" ); startNode.createRelationshipTo( endNode, RelationshipType.withName( "connects" ) ); transaction.success(); } }
private long createRelationship( Map<String,Object> properties, RelationshipType relType, long fromNode, long toNode ) { try ( org.neo4j.graphdb.Transaction tx = db.beginTx() ) { Node node1 = db.getNodeById( fromNode ); Node node2 = db.getNodeById( toNode ); Relationship relationship = node1.createRelationshipTo( node2, relType ); for ( Map.Entry<String,Object> property : properties.entrySet() ) { relationship.setProperty( property.getKey(), property.getValue() ); } tx.success(); return relationship.getId(); } }
private NeoStores resolveNeoStores() { DependencyResolver resolver = db.getDependencyResolver(); RecordStorageEngine storageEngine = resolver.resolveDependency( RecordStorageEngine.class ); return storageEngine.testAccessNeoStores(); }