Search in sources :

Example 76 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class ImportCommandTest method shouldIgnoreWhitespaceAroundBooleans.

@Test
void shouldIgnoreWhitespaceAroundBooleans() throws Exception {
    // GIVEN
    Path data = file(fileName("whitespace.csv"));
    try (PrintStream writer = new PrintStream(Files.newOutputStream(data))) {
        writer.println(":LABEL,name,adult:boolean");
        writer.println("PERSON,'t1',true");
        writer.println("PERSON,'t2',  true");
        writer.println("PERSON,'t3',true  ");
        writer.println("PERSON,'t4',  true  ");
        writer.println("PERSON,'f1',false");
        writer.println("PERSON,'f2',  false");
        writer.println("PERSON,'f3',false  ");
        writer.println("PERSON,'f4',  false  ");
        writer.println("PERSON,'f5',  truebutactuallyfalse  ");
        writer.println("PERSON,'f6',  non true things are interpreted as false  ");
    }
    Path dbConfig = prepareDefaultConfigFile();
    // WHEN
    runImport("--additional-config", dbConfig.toAbsolutePath().toString(), "--quote", "'", "--nodes", data.toAbsolutePath().toString());
    // THEN
    GraphDatabaseAPI databaseApi = getDatabaseApi();
    try (Transaction tx = databaseApi.beginTx()) {
        for (Node node : tx.getAllNodes()) {
            String name = (String) node.getProperty("name");
            if (name.startsWith("t")) {
                assertTrue((boolean) node.getProperty("adult"), "Wrong value on " + name);
            } else {
                assertFalse((boolean) node.getProperty("adult"), "Wrong value on " + name);
            }
        }
        long nodeCount = Iterables.count(tx.getAllNodes());
        assertEquals(10, nodeCount);
        tx.commit();
    }
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 77 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method shouldOpenOldDbAndCreateAndWorkWithSomeFusionIndexes.

private void shouldOpenOldDbAndCreateAndWorkWithSomeFusionIndexes(String zippedDbName, Provider highestProviderInOldVersion) throws Exception {
    // given
    Path targetDirectory = databaseLayout.databaseDirectory();
    unzip(getClass(), zippedDbName, targetDirectory);
    IndexRecoveryTracker indexRecoveryTracker = new IndexRecoveryTracker();
    // when
    Path storeDir = testDirectory.homePath();
    managementService = setupDb(storeDir, indexRecoveryTracker);
    GraphDatabaseAPI db = getDefaultDatabase();
    // then
    Provider[] providers = providersUpToAndIncluding(highestProviderInOldVersion);
    Provider[] providersIncludingSubject = concat(providers, DEFAULT_PROVIDER);
    int fulltextIndexes = 4;
    int lookupIndexes = 1;
    int expectedNumberOfIndexes = fulltextIndexes + lookupIndexes + providers.length * 2;
    try {
        // All indexes needs to be rebuilt:
        verifyInitialState(indexRecoveryTracker, expectedNumberOfIndexes, InternalIndexState.POPULATING);
        // Wait for all populating indexes to finish, so we can verify their contents:
        try (Transaction tx = db.beginTx()) {
            tx.schema().awaitIndexesOnline(10, TimeUnit.MINUTES);
            tx.commit();
        }
        // then
        for (Provider provider : providers) {
            verifyIndexes(db, provider.label);
        }
        // when
        createIndexesAndData(db, DEFAULT_PROVIDER.label);
        // then
        verifyIndexes(db, DEFAULT_PROVIDER.label);
        // when
        for (Provider provider : providersIncludingSubject) {
            additionalUpdates(db, provider.label);
            // then
            verifyAfterAdditionalUpdate(db, provider.label);
        }
        // then
        try (Transaction tx = db.beginTx()) {
            IndexDefinition fts1 = tx.schema().getIndexByName("fts1");
            Iterator<Label> fts1labels = fts1.getLabels().iterator();
            assertTrue(fts1labels.hasNext());
            assertEquals(fts1labels.next().name(), "Fts1");
            assertFalse(fts1labels.hasNext());
            Iterator<String> fts1props = fts1.getPropertyKeys().iterator();
            assertTrue(fts1props.hasNext());
            assertEquals("prop1", fts1props.next());
            assertFalse(fts1props.hasNext());
            IndexDefinition fts2 = tx.schema().getIndexByName("fts2");
            Iterator<Label> fts2labels = fts2.getLabels().iterator();
            assertTrue(fts2labels.hasNext());
            assertEquals(fts2labels.next().name(), "Fts2");
            assertTrue(fts2labels.hasNext());
            assertEquals(fts2labels.next().name(), "Fts3");
            assertFalse(fts2labels.hasNext());
            Iterator<String> fts2props = fts2.getPropertyKeys().iterator();
            assertTrue(fts2props.hasNext());
            assertEquals("prop1", fts2props.next());
            assertTrue(fts2props.hasNext());
            assertEquals("prop2", fts2props.next());
            assertFalse(fts2props.hasNext());
            IndexDefinition fts3 = tx.schema().getIndexByName("fts3");
            Iterator<Label> fts3labels = fts3.getLabels().iterator();
            assertTrue(fts3labels.hasNext());
            assertEquals(fts3labels.next().name(), "Fts4");
            assertFalse(fts3labels.hasNext());
            Iterator<String> fts3props = fts3.getPropertyKeys().iterator();
            assertTrue(fts3props.hasNext());
            assertEquals("prop1", fts3props.next());
            assertFalse(fts3props.hasNext());
            // TODO verify the index configuration of 'fts3' -- it is eventually consistent.
            IndexDefinition fts4 = tx.schema().getIndexByName("fts4");
            Iterator<RelationshipType> fts4relTypes = fts4.getRelationshipTypes().iterator();
            assertTrue(fts4relTypes.hasNext());
            assertEquals(fts4relTypes.next().name(), "FtsRel1");
            assertTrue(fts4relTypes.hasNext());
            assertEquals(fts4relTypes.next().name(), "FtsRel2");
            assertFalse(fts4relTypes.hasNext());
            Iterator<String> fts4props = fts4.getPropertyKeys().iterator();
            assertTrue(fts4props.hasNext());
            assertEquals("prop1", fts4props.next());
            assertTrue(fts4props.hasNext());
            assertEquals("prop2", fts4props.next());
            assertFalse(fts4props.hasNext());
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts1', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts2', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts3', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryRelationships( 'fts4', 'abc' )").stream()) {
                assertEquals(result.count(), 2L);
            }
            tx.commit();
        }
        // and finally
        for (Provider provider : providersIncludingSubject) {
            verifyExpectedProvider(db, provider.label, provider.descriptor);
        }
    } finally {
        managementService.shutdown();
    }
    // when
    indexRecoveryTracker = new IndexRecoveryTracker();
    managementService = setupDb(storeDir, indexRecoveryTracker);
    try {
        int numberNewIndexes = 2;
        verifyInitialState(indexRecoveryTracker, expectedNumberOfIndexes + numberNewIndexes + NUMBER_OF_SYSTEM_INDEXES, InternalIndexState.ONLINE);
    } finally {
        managementService.shutdown();
    }
}
Also used : Path(java.nio.file.Path) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) GenericNativeIndexProvider(org.neo4j.kernel.impl.index.schema.GenericNativeIndexProvider) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition)

Example 78 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class BuiltInProceduresTest method setup.

@BeforeEach
void setup() throws Exception {
    procs.registerComponent(KernelTransaction.class, ctx -> ctx.internalTransaction().kernelTransaction(), false);
    procs.registerComponent(DependencyResolver.class, Context::dependencyResolver, false);
    procs.registerComponent(GraphDatabaseAPI.class, Context::graphDatabaseAPI, false);
    procs.registerComponent(Transaction.class, Context::internalTransaction, true);
    procs.registerComponent(SecurityContext.class, Context::securityContext, true);
    procs.registerComponent(ProcedureCallContext.class, Context::procedureCallContext, true);
    procs.registerComponent(SystemGraphComponents.class, ctx -> systemGraphComponents, false);
    procs.registerComponent(Log.class, ctx -> log, false);
    procs.registerType(Node.class, NTNode);
    procs.registerType(Relationship.class, NTRelationship);
    procs.registerType(Path.class, NTPath);
    new SpecialBuiltInProcedures("1.3.37", Edition.COMMUNITY.toString()).accept(procs);
    procs.registerProcedure(BuiltInProcedures.class);
    procs.registerProcedure(BuiltInDbmsProcedures.class);
    when(transaction.kernelTransaction()).thenReturn(tx);
    when(tx.tokenRead()).thenReturn(tokens);
    when(tx.dataRead()).thenReturn(read);
    when(tx.schemaRead()).thenReturn(schemaRead);
    when(tx.securityContext()).thenReturn(SecurityContext.AUTH_DISABLED);
    when(callContext.isCalledFromCypher()).thenReturn(false);
    when(schemaRead.snapshot()).thenReturn(schemaReadCore);
    when(tokens.propertyKeyGetAllTokens()).thenAnswer(asTokens(propKeys));
    when(tokens.labelsGetAllTokens()).thenAnswer(asTokens(labels));
    when(tokens.relationshipTypesGetAllTokens()).thenAnswer(asTokens(relTypes));
    when(schemaReadCore.indexesGetAll()).thenAnswer(i -> Iterators.concat(indexes.iterator(), uniqueIndexes.iterator()));
    when(schemaReadCore.index(any(SchemaDescriptor.class))).thenAnswer((Answer<IndexDescriptor>) invocationOnMock -> {
        SchemaDescriptor schema = invocationOnMock.getArgument(0);
        return getIndexReference(schema);
    });
    when(schemaReadCore.constraintsGetAll()).thenAnswer(i -> constraints.iterator());
    when(tokens.propertyKeyName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
    when(tokens.nodeLabelName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
    when(tokens.relationshipTypeName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
    when(tokens.propertyKeyGetName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
    when(tokens.labelGetName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
    when(tokens.relationshipTypeGetName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
    when(tokens.entityTokensGetNames(any(), any())).then(invocation -> {
        EntityType type = invocation.getArgument(0);
        int[] ids = invocation.getArgument(1);
        Map<Integer, String> mapping = type == EntityType.NODE ? labels : relTypes;
        return Arrays.stream(ids).mapToObj(mapping::get).toArray(String[]::new);
    });
    when(schemaReadCore.constraintsGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.indexesGetForLabel(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.indexesGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.constraintsGetForLabel(anyInt())).thenReturn(emptyIterator());
    when(read.countsForNode(anyInt())).thenReturn(1L);
    when(read.countsForRelationship(anyInt(), anyInt(), anyInt())).thenReturn(1L);
    when(schemaReadCore.indexGetState(any(IndexDescriptor.class))).thenReturn(InternalIndexState.ONLINE);
}
Also used : SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) BasicContext.buildContext(org.neo4j.kernel.api.procedure.BasicContext.buildContext) Context(org.neo4j.kernel.api.procedure.Context) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SystemGraphComponent(org.neo4j.dbms.database.SystemGraphComponent) MapUtil(org.neo4j.internal.helpers.collection.MapUtil) Log(org.neo4j.logging.Log) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EMPTY(org.neo4j.kernel.api.index.IndexProvider.EMPTY) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Value(org.neo4j.values.storable.Value) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Statement(org.neo4j.kernel.api.Statement) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Collections.singletonList(java.util.Collections.singletonList) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) EMPTY_RESOURCE_TRACKER(org.neo4j.kernel.api.ResourceTracker.EMPTY_RESOURCE_TRACKER) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Transaction(org.neo4j.graphdb.Transaction) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) SettingValueParsers(org.neo4j.configuration.SettingValueParsers) TextValue(org.neo4j.values.storable.TextValue) Collections.emptyIterator(java.util.Collections.emptyIterator) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) List(java.util.List) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) EntityType(org.neo4j.common.EntityType) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Mockito.mock(org.mockito.Mockito.mock) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnyValue(org.neo4j.values.AnyValue) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) SettingImpl(org.neo4j.configuration.SettingImpl) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) NTNode(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTNode) Node(org.neo4j.graphdb.Node) ConstraintDescriptorFactory(org.neo4j.internal.schema.constraints.ConstraintDescriptorFactory) ArrayList(java.util.ArrayList) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures) Values(org.neo4j.values.storable.Values) DefaultValueMapper(org.neo4j.kernel.impl.util.DefaultValueMapper) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SchemaDescriptor.forLabel(org.neo4j.internal.schema.SchemaDescriptor.forLabel) SystemGraphComponents(org.neo4j.dbms.database.SystemGraphComponents) TestSystemGraphComponent(org.neo4j.dbms.database.TestSystemGraphComponent) DependencyResolver(org.neo4j.common.DependencyResolver) IndexConfig(org.neo4j.internal.schema.IndexConfig) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) IntSupplier(java.util.function.IntSupplier) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) Iterator(java.util.Iterator) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) Edition(org.neo4j.common.Edition) Setting(org.neo4j.graphdb.config.Setting) BasicContext.buildContext(org.neo4j.kernel.api.procedure.BasicContext.buildContext) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) Mockito.when(org.mockito.Mockito.when) Context(org.neo4j.kernel.api.procedure.Context) NTPath(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTPath) GlobalProceduresRegistry(org.neo4j.procedure.impl.GlobalProceduresRegistry) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Relationship(org.neo4j.graphdb.Relationship) NTRelationship(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTRelationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NamedToken(org.neo4j.token.api.NamedToken) ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) EntityType(org.neo4j.common.EntityType) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 79 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class DatabaseStartupTest method startDatabaseWithoutStoreFilesAndWithTransactionLogFilesFailure.

@Test
void startDatabaseWithoutStoreFilesAndWithTransactionLogFilesFailure() throws IOException {
    // Create a store
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    DatabaseLayout databaseLayout = db.databaseLayout();
    try (Transaction tx = db.beginTx()) {
        tx.createNode();
        tx.commit();
    }
    managementService.shutdown();
    fs.deleteRecursively(databaseLayout.databaseDirectory());
    // Try to start
    managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    try {
        db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
        assertFalse(db.isAvailable(10));
        DatabaseStateService dbStateService = db.getDependencyResolver().resolveDependency(DatabaseStateService.class);
        Optional<Throwable> cause = dbStateService.causeOfFailure(db.databaseId());
        assertTrue(cause.isPresent());
        assertThat(cause.get()).hasStackTraceContaining("Fail to start '" + db.databaseId() + "' since transaction logs were found, while database ");
    } finally {
        managementService.shutdown();
    }
}
Also used : TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 80 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class DatabaseShutdownTest method shouldShutdownCorrectlyWhenCheckPointingOnShutdownFails.

@Test
void shouldShutdownCorrectlyWhenCheckPointingOnShutdownFails() {
    TestDatabaseManagementServiceBuilderWithFailingPageCacheFlush factory = new TestDatabaseManagementServiceBuilderWithFailingPageCacheFlush(databaseLayout.databaseDirectory(), fs);
    DatabaseManagementService managementService = factory.build();
    GraphDatabaseAPI databaseService = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    DatabaseStateService dbStateService = databaseService.getDependencyResolver().resolveDependency(DatabaseStateService.class);
    factory.setFailFlush(true);
    managementService.shutdown();
    assertTrue(dbStateService.causeOfFailure(databaseService.databaseId()).isPresent());
    assertEquals(LifecycleStatus.SHUTDOWN, factory.getDatabaseStatus());
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Aggregations

GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)262 Test (org.junit.Test)91 Test (org.junit.jupiter.api.Test)88 Transaction (org.neo4j.graphdb.Transaction)87 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)61 Node (org.neo4j.graphdb.Node)38 Path (java.nio.file.Path)30 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)26 File (java.io.File)25 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)24 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)19 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)18 DependencyResolver (org.neo4j.graphdb.DependencyResolver)17 Label (org.neo4j.graphdb.Label)15 PageCache (org.neo4j.io.pagecache.PageCache)15 Config (org.neo4j.kernel.configuration.Config)15 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)14 IOException (java.io.IOException)11