Search in sources :

Example 1 with BatchInserter

use of org.neo4j.unsafe.batchinsert.BatchInserter in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowCreationOfDeferredSchemaIndexAfterConstraintOnSameKeys.

@Test
public void shouldNotAllowCreationOfDeferredSchemaIndexAfterConstraintOnSameKeys() throws Exception {
    // GIVEN
    BatchInserter inserter = globalInserter;
    String labelName = "Hacker4-" + denseNodeThreshold;
    // WHEN
    inserter.createDeferredConstraint(label(labelName)).assertPropertyIsUnique("handle").create();
    try {
        inserter.createDeferredSchemaIndex(label(labelName)).on("handle").create();
        fail("Should have thrown exception.");
    } catch (ConstraintViolationException e) {
    // THEN Good
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 2 with BatchInserter

use of org.neo4j.unsafe.batchinsert.BatchInserter in project neo4j by neo4j.

the class BatchInsertTest method shouldRepopulatePreexistingIndexed.

@Test
public void shouldRepopulatePreexistingIndexed() throws Throwable {
    // GIVEN
    long jakewins = dbWithIndexAndSingleIndexedNode();
    IndexPopulator populator = mock(IndexPopulator.class);
    SchemaIndexProvider provider = mock(SchemaIndexProvider.class);
    when(provider.getProviderDescriptor()).thenReturn(InMemoryIndexProviderFactory.PROVIDER_DESCRIPTOR);
    when(provider.getPopulator(anyLong(), any(NewIndexDescriptor.class), any(IndexSamplingConfig.class))).thenReturn(populator);
    BatchInserter inserter = newBatchInserterWithSchemaIndexProvider(singleInstanceSchemaIndexProviderFactory(InMemoryIndexProviderFactory.KEY, provider));
    long boggle = inserter.createNode(map("handle", "b0ggl3"), label("Hacker"));
    // WHEN
    inserter.shutdown();
    // THEN
    verify(provider).init();
    verify(provider).start();
    verify(provider).getPopulator(anyLong(), any(NewIndexDescriptor.class), any(IndexSamplingConfig.class));
    verify(populator).create();
    verify(populator).add(singletonList(IndexEntryUpdate.add(jakewins, internalIndex.schema(), "Jakewins")));
    verify(populator).add(singletonList(IndexEntryUpdate.add(boggle, internalIndex.schema(), "b0ggl3")));
    verify(populator).verifyDeferredConstraints(any(PropertyAccessor.class));
    verify(populator).close(true);
    verify(provider).stop();
    verify(provider).shutdown();
    verifyNoMoreInteractions(populator);
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) PropertyAccessor(org.neo4j.kernel.api.index.PropertyAccessor) Test(org.junit.Test)

Example 3 with BatchInserter

use of org.neo4j.unsafe.batchinsert.BatchInserter in project neo4j by neo4j.

the class BatchInsertTest method shouldNotAllowCreationOfDuplicateIndex.

@Test
public void shouldNotAllowCreationOfDuplicateIndex() throws Exception {
    // GIVEN
    BatchInserter inserter = globalInserter;
    String labelName = "Hacker1-" + denseNodeThreshold;
    // WHEN
    inserter.createDeferredSchemaIndex(label(labelName)).on("handle").create();
    try {
        inserter.createDeferredSchemaIndex(label(labelName)).on("handle").create();
        fail("Should have thrown exception.");
    } catch (ConstraintViolationException e) {
    // THEN Good
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 4 with BatchInserter

use of org.neo4j.unsafe.batchinsert.BatchInserter in project neo4j by neo4j.

the class BatchInsertTest method shouldCreateConsistentUniquenessConstraint.

@Test
public void shouldCreateConsistentUniquenessConstraint() throws Exception {
    // given
    BatchInserter inserter = newBatchInserter();
    // when
    inserter.createDeferredConstraint(label("Hacker")).assertPropertyIsUnique("handle").create();
    // then
    GraphDatabaseAPI graphdb = (GraphDatabaseAPI) switchToEmbeddedGraphDatabaseService(inserter);
    try {
        NeoStores neoStores = graphdb.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
        SchemaStore store = neoStores.getSchemaStore();
        SchemaStorage storage = new SchemaStorage(store);
        List<Long> inUse = new ArrayList<>();
        DynamicRecord record = store.nextRecord();
        for (long i = 1, high = store.getHighestPossibleIdInUse(); i <= high; i++) {
            store.getRecord(i, record, RecordLoad.FORCE);
            if (record.inUse() && record.isStartRecord()) {
                inUse.add(i);
            }
        }
        assertEquals("records in use", 2, inUse.size());
        SchemaRule rule0 = storage.loadSingleSchemaRule(inUse.get(0));
        SchemaRule rule1 = storage.loadSingleSchemaRule(inUse.get(1));
        IndexRule indexRule;
        ConstraintRule constraintRule;
        if (rule0 instanceof IndexRule) {
            indexRule = (IndexRule) rule0;
            constraintRule = (ConstraintRule) rule1;
        } else {
            constraintRule = (ConstraintRule) rule0;
            indexRule = (IndexRule) rule1;
        }
        assertEquals("index should reference constraint", constraintRule.getId(), indexRule.getOwningConstraint().longValue());
        assertEquals("constraint should reference index", indexRule.getId(), constraintRule.getOwnedIndex());
    } finally {
        graphdb.shutdown();
    }
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) SchemaStorage(org.neo4j.kernel.impl.store.SchemaStorage) ConstraintRule(org.neo4j.kernel.impl.store.record.ConstraintRule) RecordStorageEngine(org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine) NeoStores(org.neo4j.kernel.impl.store.NeoStores) Matchers.anyLong(org.mockito.Matchers.anyLong) Test(org.junit.Test)

Example 5 with BatchInserter

use of org.neo4j.unsafe.batchinsert.BatchInserter in project neo4j by neo4j.

the class BatchInsertTest method testCleanupEmptyPropertyRecords.

/**
     * Test checks that during node property set we will cleanup not used property records
     * During initial node creation properties will occupy 5 property records.
     * Last property record will have only empty array for email.
     * During first update email property will be migrated to dynamic property and last property record will become
     * empty. That record should be deleted form property chain or otherwise on next node load user will get an
     * property record not in use exception.
     * @throws Exception
     */
@Test
public void testCleanupEmptyPropertyRecords() throws Exception {
    BatchInserter inserter = globalInserter;
    Map<String, Object> properties = new HashMap<>();
    properties.put("id", 1099511659993L);
    properties.put("firstName", "Edward");
    properties.put("lastName", "Shevchenko");
    properties.put("gender", "male");
    properties.put("birthday", new SimpleDateFormat("yyyy-MM-dd").parse("1987-11-08").getTime());
    properties.put("birthday_month", 11);
    properties.put("birthday_day", 8);
    properties.put("creationDate", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2010-04-22T18:05:40.912+0000").getTime());
    properties.put("locationIP", "46.151.255.205");
    properties.put("browserUsed", "Firefox");
    properties.put("email", new String[0]);
    properties.put("languages", new String[0]);
    long personNodeId = inserter.createNode(properties);
    assertEquals("Shevchenko", inserter.getNodeProperties(personNodeId).get("lastName"));
    assertThat((String[]) inserter.getNodeProperties(personNodeId).get("email"), is(emptyArray()));
    inserter.setNodeProperty(personNodeId, "email", new String[] { "Edward1099511659993@gmail.com" });
    assertThat((String[]) inserter.getNodeProperties(personNodeId).get("email"), arrayContaining("Edward1099511659993@gmail.com"));
    inserter.setNodeProperty(personNodeId, "email", new String[] { "Edward1099511659993@gmail.com", "backup@gmail.com" });
    assertThat((String[]) inserter.getNodeProperties(personNodeId).get("email"), arrayContaining("Edward1099511659993@gmail.com", "backup@gmail.com"));
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)60 Test (org.junit.Test)58 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)9 File (java.io.File)8 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)8 Transaction (org.neo4j.graphdb.Transaction)8 Label (org.neo4j.graphdb.Label)5 HashMap (java.util.HashMap)4 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)4 SchemaIndexProvider (org.neo4j.kernel.api.index.SchemaIndexProvider)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 IndexSamplingConfig (org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig)4 BatchRelationship (org.neo4j.unsafe.batchinsert.BatchRelationship)4 HashSet (java.util.HashSet)3 Matchers.anyLong (org.mockito.Matchers.anyLong)3 Node (org.neo4j.graphdb.Node)3 PropertyAccessor (org.neo4j.kernel.api.index.PropertyAccessor)3 NeoStores (org.neo4j.kernel.impl.store.NeoStores)3 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)3 Set (java.util.Set)2