@Test public void shouldBeAbleToMakeRepeatedCallsToSetNodeProperty() throws Exception { File file = dbRule.databaseLayout().databaseDirectory(); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); long nodeId = inserter.createNode( Collections.emptyMap() ); final Object finalValue = 87; inserter.setNodeProperty( nodeId, "a", "some property value" ); inserter.setNodeProperty( nodeId, "a", 42 ); inserter.setNodeProperty( nodeId, "a", 3.14 ); inserter.setNodeProperty( nodeId, "a", true ); inserter.setNodeProperty( nodeId, "a", finalValue ); inserter.shutdown(); GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction ignored = db.beginTx() ) { assertThat( db.getNodeById( nodeId ).getProperty( "a" ), equalTo( finalValue ) ); } finally { db.shutdown(); } }
@Test public void createEntitiesWithEmptyPropertiesMap() { BatchInserter inserter = globalInserter; // Assert for node long nodeId = inserter.createNode( map() ); getNodeProperties( inserter, nodeId ); //cp=N U http://www.w3.org/1999/02/22-rdf-syntax-ns#type, c=N // Assert for relationship long anotherNodeId = inserter.createNode( null ); long relId = inserter.createRelationship( nodeId, anotherNodeId, RelTypes.BATCH_TEST, map() ); inserter.getRelationshipProperties( relId ); }
@Test public void givenBatchInserterWhenArrayPropertyUpdated4TimesThenShouldNotFail() throws IOException { BatchInserter batchInserter = BatchInserters.inserter( testDirectory.databaseDir(), fileSystemRule.get() ); long nodeId = batchInserter.createNode( Collections.emptyMap() ); for ( int i = 0; i < 4; i++ ) { batchInserter.setNodeProperty( nodeId, "array", new byte[]{2, 3, 98, 1, 43, 50, 3, 33, 51, 55, 116, 16, 23, 56, 9, -10, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1} ); } batchInserter.getNodeProperties( nodeId ); //fails here batchInserter.shutdown(); } }
private void createRelationships( BatchInserter inserter, long node, RelationshipType relType, int out ) { for ( int i = 0; i < out; i++ ) { inserter.createRelationship( node, inserter.createNode( null ), relType, null ); } for ( int i = 0; i < out; i++ ) { inserter.createRelationship( inserter.createNode( null ), node, relType, null ); } for ( int i = 0; i < out; i++ ) { inserter.createRelationship( node, node, relType, null ); } }
@Test public void shouldIgnoreRemovingNonExistentRelationshipProperty() { // given BatchInserter inserter = globalInserter; Map<String,Object> noProperties = Collections.emptyMap(); long nodeId1 = inserter.createNode( noProperties ); long nodeId2 = inserter.createNode( noProperties ); long id = inserter.createRelationship( nodeId1, nodeId2, MyRelTypes.TEST, noProperties ); // when inserter.removeRelationshipProperty( id, "non-existent" ); // then no exception should be thrown, this mimics GraphDatabaseService behaviour }
@Test public void shouldNotIndexNodesWithWrongLabel() throws Exception { // Given File file = new File( dbRule.getDatabaseDirAbsolutePath() ); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); inserter.createNode( map("name", "Bob"), label( "User" ), label("Admin")); inserter.createDeferredSchemaIndex( label( "Banana" ) ).on( "name" ).create(); // When inserter.shutdown(); // Then GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertThat( count( db.findNodes( label( "Banana" ), "name", "Bob" ) ), equalTo( 0L ) ); } finally { db.shutdown(); } }
@Test public void shouldThrowWhenPopulatingWithNonUniquePoints() throws Exception { Config config = Config.defaults( stringMap( default_schema_provider.name(), schemaIndex.providerName() ) ); BatchInserter inserter = newBatchInserter( config ); PointValue point = Values.pointValue( CoordinateReferenceSystem.WGS84, 0.0, 0.0 ); inserter.createNode( MapUtil.map( "prop", point ), TestLabels.LABEL_ONE ); inserter.createNode( MapUtil.map( "prop", point ), TestLabels.LABEL_ONE ); inserter.createDeferredConstraint( TestLabels.LABEL_ONE ).assertPropertyIsUnique( "prop" ).create(); inserter.shutdown(); GraphDatabaseService db = graphDatabaseService( config ); try ( Transaction tx = db.beginTx() ) { Iterator<IndexDefinition> indexes = db.schema().getIndexes().iterator(); assertTrue( indexes.hasNext() ); IndexDefinition index = indexes.next(); Schema.IndexState indexState = db.schema().getIndexState( index ); assertEquals( Schema.IndexState.FAILED, indexState ); assertFalse( indexes.hasNext() ); tx.success(); } finally { db.shutdown(); } }
@Test public void lazyLoadWithinWriteTransaction() throws Exception { // Given FileSystemAbstraction fileSystem = fs.get(); BatchInserter inserter = BatchInserters.inserter( testDirectory.databaseDir(), fileSystem ); int count = 3000; long nodeId = inserter.createNode( mapWithManyProperties( count /* larger than initial property index load threshold */ ) ); inserter.shutdown(); GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem( fileSystem ).newImpermanentDatabase( testDirectory.databaseDir() ); // When try ( Transaction tx = db.beginTx() ) { db.createNode(); Node node = db.getNodeById( nodeId ); // Then assertEquals( count, Iterables.count( node.getPropertyKeys() ) ); tx.success(); } finally { db.shutdown(); } }
long startNode = graphDb.createNode( properties ); long otherNode = graphDb.createNode( properties ); long selfRelationship = graphDb.createRelationship( startNode, startNode, relTypeArray[0], properties ); long relationship = graphDb.createRelationship( startNode, otherNode, relTypeArray[0], properties ); for ( BatchRelationship rel : graphDb.getRelationships( startNode ) ) try ( Transaction ignored = db.beginTx() ) Node realStartNode = db.getNodeById( startNode ); Relationship realSelfRelationship = db.getRelationshipById( selfRelationship ); Relationship realRelationship = db.getRelationshipById( relationship ); assertEquals( realSelfRelationship,
@Test public void uniquenessConstraintShouldBeCheckedOnBatchInserterShutdownAndFailIfViolated() throws Exception { // Given Label label = label( "Foo" ); String property = "Bar"; String value = "Baz"; BatchInserter inserter = newBatchInserter(); // When inserter.createDeferredConstraint( label ).assertPropertyIsUnique( property ).create(); inserter.createNode( Collections.singletonMap( property, value ), label ); inserter.createNode( Collections.singletonMap( property, value ), label ); // Then GraphDatabaseService db = switchToEmbeddedGraphDatabaseService( inserter ); try ( Transaction tx = db.beginTx() ) { IndexDefinition index = db.schema().getIndexes( label ).iterator().next(); String indexFailure = db.schema().getIndexFailure( index ); assertThat( indexFailure, containsString( "IndexEntryConflictException" ) ); assertThat( indexFailure, containsString( value ) ); tx.success(); } finally { db.shutdown(); } }
@Test public void createBatchNodeAndRelationshipsDeleteAllInEmbedded() throws Exception { /* * ()--[REL_TYPE1]-->(node)--[BATCH_TEST]->() */ BatchInserter inserter = newBatchInserter(); long nodeId = inserter.createNode( null ); inserter.createRelationship( nodeId, inserter.createNode( null ), RelTypes.BATCH_TEST, null ); inserter.createRelationship( inserter.createNode( null ), nodeId, RelTypes.REL_TYPE1, null ); // Delete node and all its relationships GraphDatabaseService db = switchToEmbeddedGraphDatabaseService( inserter ); try ( Transaction tx = db.beginTx() ) { Node node = db.getNodeById( nodeId ); for ( Relationship relationship : node.getRelationships() ) { relationship.delete(); } node.delete(); tx.success(); } db.shutdown(); }
@Test public void testBatchIndexToAutoIndex() throws IOException { BatchInserter inserter = BatchInserters.inserter(new File(path)); BatchInserterIndexProvider indexProvider = new LuceneBatchInserterIndexProvider(inserter); BatchInserterIndex index = indexProvider.nodeIndex("node_auto_index", MapUtil.stringMap("type", "exact")); long node = inserter.createNode(MapUtil.map("foo", "bar")); index.add(node, MapUtil.map("foo", "bar")); index.flush(); assertThat("Batch indexed node can be retrieved", index.get("foo", "bar").next(), is(node)); indexProvider.shutdown(); inserter.shutdown(); graphDb = getGraphDb(); try (Transaction tx = graphDb.beginTx()) { assertThat("AutoIndex is not enabled after reopening the graph", graphDb.index() .getNodeAutoIndexer().isEnabled(), is(false)); assertThat("AutoIndexed properties are not maintained after closing the graph", graphDb .index().getNodeAutoIndexer().getAutoIndexedProperties(), is(empty())); assertThat("Batch index properties are in the index", graphDb.index().getNodeAutoIndexer() .getAutoIndex().query("foo", "bar").size(), is(1)); tx.success(); } }
@Test public void batchInserterShouldUseConfiguredIndexProvider() throws Exception { Config config = Config.defaults( stringMap( default_schema_provider.name(), schemaIndex.providerName() ) ); BatchInserter inserter = newBatchInserter( config ); inserter.createDeferredSchemaIndex( TestLabels.LABEL_ONE ).on( "key" ).create(); inserter.shutdown(); GraphDatabaseService db = graphDatabaseService( config ); awaitIndexesOnline( db ); try ( Transaction tx = db.beginTx() ) { DependencyResolver dependencyResolver = ((GraphDatabaseAPI) db).getDependencyResolver(); ThreadToStatementContextBridge threadToStatementContextBridge = dependencyResolver.resolveDependency( ThreadToStatementContextBridge.class ); KernelTransaction kernelTransaction = threadToStatementContextBridge.getKernelTransactionBoundToThisThread( true ); TokenRead tokenRead = kernelTransaction.tokenRead(); SchemaRead schemaRead = kernelTransaction.schemaRead(); int labelId = tokenRead.nodeLabel( TestLabels.LABEL_ONE.name() ); int propertyId = tokenRead.propertyKey( "key" ); IndexReference index = schemaRead.index( labelId, propertyId ); assertTrue( unexpectedIndexProviderMessage( index ), schemaIndex.providerName().contains( index.providerKey() ) ); assertTrue( unexpectedIndexProviderMessage( index ), schemaIndex.providerName().contains( index.providerVersion() ) ); tx.success(); } finally { db.shutdown(); } }
private long dbWithIndexAndSingleIndexedNode() throws Exception { IndexPopulator populator = mock( IndexPopulator.class ); IndexProvider provider = mock( IndexProvider.class ); when( provider.getProviderDescriptor() ).thenReturn( DESCRIPTOR ); when( provider.getPopulator( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class ) ) ) .thenReturn( populator ); when( provider.bless( any( IndexDescriptor.class ) ) ).thenCallRealMethod(); BatchInserter inserter = newBatchInserterWithIndexProvider( singleInstanceIndexProviderFactory( KEY, provider ), provider.getProviderDescriptor() ); inserter.createDeferredSchemaIndex( label("Hacker") ).on( "handle" ).create(); long nodeId = inserter.createNode( map( "handle", "Jakewins" ), label( "Hacker" ) ); inserter.shutdown(); return nodeId; }
@Test void explicitIndexPopulationWithBunchOfFields() { assertTimeout( ofMillis( TEST_TIMEOUT ), () -> { BatchInserter batchNode = BatchInserters.inserter( directory.databaseDir() ); LuceneBatchInserterIndexProvider provider = new LuceneBatchInserterIndexProvider( batchNode ); try { BatchInserterIndex batchIndex = provider.nodeIndex( "node_auto_index", stringMap( IndexManager.PROVIDER, "lucene", "type", "fulltext" ) ); Map<String,Object> properties = IntStream.range( 0, 2000 ) .mapToObj( i -> Pair.of( Integer.toString( i ), randomAlphabetic( 200 ) ) ) .collect( toMap( Pair::first, Pair::other ) ); long node = batchNode.createNode( properties, Label.label( "NODE" ) ); batchIndex.add( node, properties ); } finally { provider.shutdown(); batchNode.shutdown(); } } ); } }
@Test public void shouldCreateDeferredSchemaIndexesInEmptyDatabase() throws Exception { // GIVEN BatchInserter inserter = newBatchInserter(); // WHEN IndexDefinition definition = inserter.createDeferredSchemaIndex( label( "Hacker" ) ).on( "handle" ).create(); // THEN assertEquals( "Hacker", single( definition.getLabels() ).name() ); assertEquals( asCollection( iterator( "handle" ) ), Iterables.asCollection( definition.getPropertyKeys() ) ); inserter.shutdown(); }
@Test public void shouldCreateDeferredUniquenessConstraintInEmptyDatabase() throws Exception { // GIVEN BatchInserter inserter = newBatchInserter(); // WHEN ConstraintDefinition definition = inserter.createDeferredConstraint( label( "Hacker" ) ).assertPropertyIsUnique( "handle" ).create(); // THEN assertEquals( "Hacker", definition.getLabel().name() ); assertEquals( ConstraintType.UNIQUENESS, definition.getConstraintType() ); assertEquals( asSet( "handle" ), Iterables.asSet( definition.getPropertyKeys() ) ); inserter.shutdown(); }
@Test public void shouldSortLabelIdsWhenGetOrCreate() { // GIVEN BatchInserter inserter = globalInserter; // WHEN long nodeId = inserter.createNode( map( "Item", 123456789123L ), label( "AA" ), label( "BB" ), label( "CC" ), label( "DD" ) ); inserter.setNodeLabels( nodeId, label( "CC" ), label( "AA" ), label( "DD" ), label( "EE" ), label( "FF" ) ); // THEN NodeStore nodeStore = getFlushedNeoStores( inserter ).getNodeStore(); NodeRecord node = nodeStore.getRecord( nodeId, nodeStore.newRecord(), RecordLoad.NORMAL ); NodeLabels labels = NodeLabelsField.parseLabelsField( node ); long[] labelIds = labels.get( nodeStore ); long[] sortedLabelIds = labelIds.clone(); Arrays.sort( sortedLabelIds ); assertArrayEquals( sortedLabelIds, labelIds ); }
@Test public void setSingleProperty() throws Exception { BatchInserter inserter = newBatchInserter(); long node = inserter.createNode( null ); String value = "Something"; String key = "name"; inserter.setNodeProperty( node, key, value ); GraphDatabaseService db = switchToEmbeddedGraphDatabaseService( inserter ); assertThat( getNodeInTx( node, db ), inTx( db, hasProperty( key ).withValue( value ) ) ); db.shutdown(); }