Search in sources :

Example 6 with GraphDatabaseAPI

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

the class IndexStatisticsTest method before.

@Before
public void before() {
    GraphDatabaseAPI graphDatabaseAPI = dbRule.getGraphDatabaseAPI();
    this.db = graphDatabaseAPI;
    DependencyResolver dependencyResolver = graphDatabaseAPI.getDependencyResolver();
    this.bridge = dependencyResolver.resolveDependency(ThreadToStatementContextBridge.class);
    graphDatabaseAPI.getDependencyResolver().resolveDependency(Monitors.class).addMonitorListener(indexOnlineMonitor);
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Monitors(org.neo4j.kernel.monitoring.Monitors) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) DependencyResolver(org.neo4j.graphdb.DependencyResolver) Before(org.junit.Before)

Example 7 with GraphDatabaseAPI

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

the class QueryEngineProviderTest method shouldPickTheOneAndOnlyQueryEngineAvailable.

@Test
public void shouldPickTheOneAndOnlyQueryEngineAvailable() throws Throwable {
    // Given
    QueryEngineProvider provider = mock(QueryEngineProvider.class);
    when(provider.enginePriority()).thenReturn(1);
    Dependencies deps = new Dependencies();
    GraphDatabaseAPI graphAPI = mock(GraphDatabaseAPI.class);
    QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
    when(provider.createEngine(any(), any())).thenReturn(executionEngine);
    // When
    Iterable<QueryEngineProvider> providers = Iterables.asIterable(provider);
    QueryExecutionEngine engine = QueryEngineProvider.initialize(deps, graphAPI, providers);
    // Then
    assertSame(executionEngine, engine);
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Dependencies(org.neo4j.kernel.impl.util.Dependencies) Test(org.junit.Test)

Example 8 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI 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 9 with GraphDatabaseAPI

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

the class BoltKernelExtension method newInstance.

@Override
public Lifecycle newInstance(KernelContext context, Dependencies dependencies) throws Throwable {
    Config config = dependencies.config();
    GraphDatabaseService gdb = dependencies.db();
    GraphDatabaseAPI api = (GraphDatabaseAPI) gdb;
    LogService logService = dependencies.logService();
    Clock clock = dependencies.clock();
    Log log = logService.getInternalLog(WorkerFactory.class);
    LifeSupport life = new LifeSupport();
    JobScheduler scheduler = dependencies.scheduler();
    InternalLoggerFactory.setDefaultFactory(new Netty4LoggerFactory(logService.getInternalLogProvider()));
    Authentication authentication = authentication(dependencies.authManager(), dependencies.userManagerSupplier());
    BoltFactory boltFactory = life.add(new BoltFactoryImpl(api, dependencies.usageData(), logService, dependencies.txBridge(), authentication, dependencies.sessionTracker(), config));
    WorkerFactory workerFactory = createWorkerFactory(boltFactory, scheduler, dependencies, logService, clock);
    List<ProtocolInitializer> connectors = config.enabledBoltConnectors().stream().map((connConfig) -> {
        ListenSocketAddress listenAddress = config.get(connConfig.listen_address);
        AdvertisedSocketAddress advertisedAddress = config.get(connConfig.advertised_address);
        SslContext sslCtx;
        boolean requireEncryption;
        final BoltConnector.EncryptionLevel encryptionLevel = config.get(connConfig.encryption_level);
        switch(encryptionLevel) {
            case REQUIRED:
                // Encrypted connections are mandatory, a self-signed certificate may be generated.
                requireEncryption = true;
                sslCtx = createSslContext(config, log, advertisedAddress);
                break;
            case OPTIONAL:
                // Encrypted connections are optional, a self-signed certificate may be generated.
                requireEncryption = false;
                sslCtx = createSslContext(config, log, advertisedAddress);
                break;
            case DISABLED:
                // Encryption is turned off, no self-signed certificate will be generated.
                requireEncryption = false;
                sslCtx = null;
                break;
            default:
                // In the unlikely event that we happen to fall through to the default option here,
                // there is a mismatch between the BoltConnector.EncryptionLevel enum and the options
                // handled in this switch statement. In this case, we'll log a warning and default to
                // disabling encryption, since this mirrors the functionality introduced in 3.0.
                log.warn(format("Unhandled encryption level %s - assuming DISABLED.", encryptionLevel.name()));
                requireEncryption = false;
                sslCtx = null;
                break;
        }
        final Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> versions = newVersions(logService, workerFactory);
        return new SocketTransport(listenAddress, sslCtx, requireEncryption, logService.getInternalLogProvider(), versions);
    }).collect(toList());
    if (connectors.size() > 0 && !config.get(GraphDatabaseSettings.disconnected)) {
        life.add(new NettyServer(scheduler.threadFactory(boltNetworkIO), connectors));
        log.info("Bolt Server extension loaded.");
        for (ProtocolInitializer connector : connectors) {
            logService.getUserLog(WorkerFactory.class).info("Bolt enabled on %s.", connector.address());
        }
    }
    return life;
}
Also used : Service(org.neo4j.helpers.Service) UsageData(org.neo4j.udc.UsageData) Log(org.neo4j.logging.Log) Authentication(org.neo4j.bolt.security.auth.Authentication) BiFunction(java.util.function.BiFunction) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KernelContext(org.neo4j.kernel.impl.spi.KernelContext) SocketTransport(org.neo4j.bolt.transport.SocketTransport) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) KeyStoreFactory(org.neo4j.bolt.security.ssl.KeyStoreFactory) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) GeneralSecurityException(java.security.GeneralSecurityException) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) Map(java.util.Map) KeyStoreInformation(org.neo4j.bolt.security.ssl.KeyStoreInformation) Groups.boltNetworkIO(org.neo4j.kernel.impl.util.JobScheduler.Groups.boltNetworkIO) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) BoltConnector(org.neo4j.kernel.configuration.BoltConnector) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) LogService(org.neo4j.kernel.impl.logging.LogService) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Settings.derivedSetting(org.neo4j.kernel.configuration.Settings.derivedSetting) List(java.util.List) Description(org.neo4j.configuration.Description) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) KernelExtensionFactory(org.neo4j.kernel.extension.KernelExtensionFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Settings.pathSetting(org.neo4j.kernel.configuration.Settings.pathSetting) Internal(org.neo4j.configuration.Internal) Monitors(org.neo4j.kernel.monitoring.Monitors) HashMap(java.util.HashMap) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Configuration(org.neo4j.graphdb.config.Configuration) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) Certificates(org.neo4j.bolt.security.ssl.Certificates) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NettyServer(org.neo4j.bolt.transport.NettyServer) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) Config(org.neo4j.kernel.configuration.Config) SslContext(io.netty.handler.ssl.SslContext) BoltConnectionTracker(org.neo4j.kernel.api.bolt.BoltConnectionTracker) Setting(org.neo4j.graphdb.config.Setting) IOException(java.io.IOException) PATH(org.neo4j.kernel.configuration.Settings.PATH) File(java.io.File) Channel(io.netty.channel.Channel) UserManagerSupplier(org.neo4j.kernel.api.security.UserManagerSupplier) BoltWorker(org.neo4j.bolt.v1.runtime.BoltWorker) Collectors.toList(java.util.stream.Collectors.toList) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Clock(java.time.Clock) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) AuthManager(org.neo4j.kernel.api.security.AuthManager) Config(org.neo4j.kernel.configuration.Config) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Clock(java.time.Clock) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) NettyServer(org.neo4j.bolt.transport.NettyServer) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) SslContext(io.netty.handler.ssl.SslContext) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Log(org.neo4j.logging.Log) SocketTransport(org.neo4j.bolt.transport.SocketTransport) Channel(io.netty.channel.Channel) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) Authentication(org.neo4j.bolt.security.auth.Authentication) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Map(java.util.Map) HashMap(java.util.HashMap) LogService(org.neo4j.kernel.impl.logging.LogService)

Example 10 with GraphDatabaseAPI

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

the class NoChangeWriteTransactionTest method shouldDetectNoChangesInCommitsAlsoForTheIndexes.

@Test
public void shouldDetectNoChangesInCommitsAlsoForTheIndexes() throws Exception {
    // GIVEN a transaction that has seen some changes, where all those changes result in a net 0 change set
    // a good way of producing such state is to add a label to an existing node, and then remove it.
    GraphDatabaseAPI db = dbr.getGraphDatabaseAPI();
    TransactionIdStore txIdStore = db.getDependencyResolver().resolveDependency(TransactionIdStore.class);
    long startTxId = txIdStore.getLastCommittedTransactionId();
    Node node = createEmptyNode(db);
    Index<Node> index = createNodeIndex(db);
    try (Transaction tx = db.beginTx()) {
        node.addLabel(TestLabels.LABEL_ONE);
        node.removeLabel(TestLabels.LABEL_ONE);
        index.add(node, "key", "value");
        index.remove(node, "key", "value");
        tx.success();
    }
    // WHEN closing that transaction
    // THEN it should not have been committed
    assertEquals("Expected last txId to be what it started at + 3 " + "(1 for the empty node, 1 for index, and one for the label)", startTxId + 3, txIdStore.getLastCommittedTransactionId());
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)133 Test (org.junit.Test)96 Transaction (org.neo4j.graphdb.Transaction)50 File (java.io.File)25 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)25 Node (org.neo4j.graphdb.Node)20 DependencyResolver (org.neo4j.graphdb.DependencyResolver)19 Config (org.neo4j.kernel.configuration.Config)15 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)12 IOException (java.io.IOException)8 PageCache (org.neo4j.io.pagecache.PageCache)8 RecordStorageEngine (org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine)8 Lifespan (org.neo4j.kernel.lifecycle.Lifespan)8 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)6 TransactionIdStore (org.neo4j.kernel.impl.transaction.log.TransactionIdStore)6 Before (org.junit.Before)5 Label (org.neo4j.graphdb.Label)5 GraphDatabaseBuilder (org.neo4j.graphdb.factory.GraphDatabaseBuilder)5 GuardTimeoutException (org.neo4j.kernel.guard.GuardTimeoutException)5