Search in sources :

Example 1 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class ExportTest method result.

@SuppressWarnings("unchecked")
private ExecutionResult result(String column, Object value) {
    ExecutionResult result = Mockito.mock(ExecutionResult.class);
    Mockito.when(result.columns()).thenReturn(asList(column));
    final Iterator<Map<String, Object>> inner = asList(singletonMap(column, value)).iterator();
    final ResourceIterator<Map<String, Object>> iterator = new ResourceIterator<Map<String, Object>>() {

        @Override
        public void close() {
        }

        @Override
        public boolean hasNext() {
            return inner.hasNext();
        }

        @Override
        public Map<String, Object> next() {
            return inner.next();
        }

        @Override
        public void remove() {
            inner.remove();
        }
    };
    Mockito.when(result.iterator()).thenReturn(iterator);
    Mockito.when(result.hasNext()).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return iterator.hasNext();
        }
    });
    Mockito.when(result.next()).thenAnswer(new Answer<Map<String, Object>>() {

        @Override
        public Map<String, Object> answer(InvocationOnMock invocation) throws Throwable {
            return iterator.next();
        }
    });
    return result;
}
Also used : ExecutionResult(org.neo4j.cypher.internal.javacompat.ExecutionResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 2 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class QueryLoggerIT method shouldLogTXMetaDataInQueryLog.

@Test
public void shouldLogTXMetaDataInQueryLog() throws Throwable {
    // turn on query logging
    databaseBuilder.setConfig(GraphDatabaseSettings.logs_directory, logsDirectory.getPath());
    databaseBuilder.setConfig(GraphDatabaseSettings.log_queries, Settings.TRUE);
    EmbeddedInteraction db = new EmbeddedInteraction(databaseBuilder, Collections.emptyMap());
    GraphDatabaseFacade graph = db.getLocalGraph();
    db.getLocalUserManager().setUserPassword("neo4j", "123", false);
    EnterpriseSecurityContext subject = db.login("neo4j", "123");
    db.executeQuery(subject, "UNWIND range(0, 10) AS i CREATE (:Foo {p: i})", Collections.emptyMap(), ResourceIterator::close);
    // Set meta data and execute query in transaction
    try (InternalTransaction tx = db.beginLocalTransactionAsUser(subject, KernelTransaction.Type.explicit)) {
        graph.execute("CALL dbms.setTXMetaData( { User: 'Johan' } )", Collections.emptyMap());
        graph.execute("CALL dbms.procedures() YIELD name RETURN name", Collections.emptyMap()).close();
        graph.execute("MATCH (n) RETURN n", Collections.emptyMap()).close();
        graph.execute(QUERY, Collections.emptyMap());
        tx.success();
    }
    // Ensure that old meta data is not retained
    try (InternalTransaction tx = db.beginLocalTransactionAsUser(subject, KernelTransaction.Type.explicit)) {
        graph.execute("CALL dbms.setTXMetaData( { Location: 'Sweden' } )", Collections.emptyMap());
        graph.execute("MATCH ()-[r]-() RETURN count(r)", Collections.emptyMap()).close();
        tx.success();
    }
    db.tearDown();
    // THEN
    List<String> logLines = readAllLines(logFilename);
    assertThat(logLines, hasSize(7));
    assertThat(logLines.get(0), not(containsString("User: 'Johan'")));
    // we don't care if setTXMetaData contains the meta data
    //assertThat( logLines.get( 1 ), containsString( "User: Johan" ) );
    assertThat(logLines.get(2), containsString("User: 'Johan'"));
    assertThat(logLines.get(3), containsString("User: 'Johan'"));
    assertThat(logLines.get(4), containsString("User: 'Johan'"));
    // we want to make sure that the new transaction does not carry old meta data
    assertThat(logLines.get(5), not(containsString("User: 'Johan'")));
    assertThat(logLines.get(6), containsString("Location: 'Sweden'"));
}
Also used : EnterpriseSecurityContext(org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext) EmbeddedInteraction(org.neo4j.server.security.enterprise.auth.EmbeddedInteraction) Matchers.containsString(org.hamcrest.Matchers.containsString) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) ResourceIterator(org.neo4j.graphdb.ResourceIterator) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Test(org.junit.Test)

Example 3 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class QueryLoggerIT method shouldLogCustomUserName.

@Test
public void shouldLogCustomUserName() throws Throwable {
    // turn on query logging
    final Map<String, String> config = stringMap(GraphDatabaseSettings.logs_directory.name(), logsDirectory.getPath(), GraphDatabaseSettings.log_queries.name(), Settings.TRUE);
    EmbeddedInteraction db = new EmbeddedInteraction(databaseBuilder, config);
    // create users
    db.getLocalUserManager().newUser("mats", "neo4j", false);
    db.getLocalUserManager().newUser("andres", "neo4j", false);
    db.getLocalUserManager().addRoleToUser("architect", "mats");
    db.getLocalUserManager().addRoleToUser("reader", "andres");
    EnterpriseSecurityContext mats = db.login("mats", "neo4j");
    // run query
    db.executeQuery(mats, "UNWIND range(0, 10) AS i CREATE (:Foo {p: i})", Collections.emptyMap(), ResourceIterator::close);
    db.executeQuery(mats, "CREATE (:Label)", Collections.emptyMap(), ResourceIterator::close);
    // switch user, run query
    EnterpriseSecurityContext andres = db.login("andres", "neo4j");
    db.executeQuery(andres, "MATCH (n:Label) RETURN n", Collections.emptyMap(), ResourceIterator::close);
    db.tearDown();
    // THEN
    List<String> logLines = readAllLines(logFilename);
    assertThat(logLines, hasSize(3));
    assertThat(logLines.get(0), containsString("mats"));
    assertThat(logLines.get(1), containsString("mats"));
    assertThat(logLines.get(2), containsString("andres"));
}
Also used : EnterpriseSecurityContext(org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext) EmbeddedInteraction(org.neo4j.server.security.enterprise.auth.EmbeddedInteraction) Matchers.containsString(org.hamcrest.Matchers.containsString) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Test(org.junit.Test)

Example 4 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class LuceneDataSource method listStoreFiles.

public ResourceIterator<File> listStoreFiles(boolean includeLogicalLogs) throws IOException {
    // Never include logical logs since they are of little importance
    final Collection<File> files = new ArrayList<>();
    final Collection<Pair<SnapshotDeletionPolicy, IndexCommit>> snapshots = new ArrayList<>();
    makeSureAllIndexesAreInstantiated();
    for (IndexReference writer : getAllIndexes()) {
        SnapshotDeletionPolicy deletionPolicy = (SnapshotDeletionPolicy) writer.getWriter().getConfig().getIndexDeletionPolicy();
        File indexDirectory = getFileDirectory(baseStorePath, writer.getIdentifier());
        IndexCommit commit;
        try {
            // Throws IllegalStateException if no commits yet
            commit = deletionPolicy.snapshot();
        } catch (IllegalStateException e) {
            /*
                 * This is insane but happens if we try to snapshot an existing index
                 * that has no commits. This is a bad API design - it should return null
                 * or something. This is not exceptional.
                 *
                 * For the time being we just do a commit and try again.
                 */
            writer.getWriter().commit();
            commit = deletionPolicy.snapshot();
        }
        for (String fileName : commit.getFileNames()) {
            files.add(new File(indexDirectory, fileName));
        }
        snapshots.add(Pair.of(deletionPolicy, commit));
    }
    return new PrefetchingResourceIterator<File>() {

        private final Iterator<File> filesIterator = files.iterator();

        @Override
        protected File fetchNextOrNull() {
            return filesIterator.hasNext() ? filesIterator.next() : null;
        }

        @Override
        public void close() {
            for (Pair<SnapshotDeletionPolicy, IndexCommit> policyAndCommit : snapshots) {
                try {
                    policyAndCommit.first().release(policyAndCommit.other());
                } catch (IOException e) {
                    // TODO What to do?
                    e.printStackTrace();
                }
            }
        }
    };
}
Also used : PrefetchingResourceIterator(org.neo4j.helpers.collection.PrefetchingResourceIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SnapshotDeletionPolicy(org.apache.lucene.index.SnapshotDeletionPolicy) IndexCommit(org.apache.lucene.index.IndexCommit) ResourceIterator(org.neo4j.graphdb.ResourceIterator) PrefetchingResourceIterator(org.neo4j.helpers.collection.PrefetchingResourceIterator) Iterator(java.util.Iterator) File(java.io.File) Pair(org.neo4j.helpers.collection.Pair)

Example 5 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class IndexingService method snapshotStoreFiles.

public ResourceIterator<File> snapshotStoreFiles() throws IOException {
    Collection<ResourceIterator<File>> snapshots = new ArrayList<>();
    Set<SchemaIndexProvider.Descriptor> fromProviders = new HashSet<>();
    for (IndexProxy indexProxy : indexMapRef.getAllIndexProxies()) {
        Descriptor providerDescriptor = indexProxy.getProviderDescriptor();
        if (fromProviders.add(providerDescriptor)) {
            snapshots.add(providerMap.apply(providerDescriptor).snapshotMetaFiles());
        }
        snapshots.add(indexProxy.snapshotFiles());
    }
    return Iterators.concatResourceIterators(snapshots.iterator());
}
Also used : ArrayList(java.util.ArrayList) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Descriptor(org.neo4j.kernel.api.index.SchemaIndexProvider.Descriptor) ResourceIterator(org.neo4j.graphdb.ResourceIterator) HashSet(java.util.HashSet)

Aggregations

ResourceIterator (org.neo4j.graphdb.ResourceIterator)11 File (java.io.File)5 Test (org.junit.Test)5 Map (java.util.Map)4 Label (org.neo4j.graphdb.Label)4 Node (org.neo4j.graphdb.Node)4 ArrayList (java.util.ArrayList)3 TimeUnit (java.util.concurrent.TimeUnit)3 Transaction (org.neo4j.graphdb.Transaction)3 GraphDatabaseSettings (org.neo4j.graphdb.factory.GraphDatabaseSettings)3 Config (org.neo4j.kernel.configuration.Config)3 GraphDatabaseFacade (org.neo4j.kernel.impl.factory.GraphDatabaseFacade)3 Clock (java.time.Clock)2 Future (java.util.concurrent.Future)2 Predicate (java.util.function.Predicate)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 After (org.junit.After)2 AfterClass (org.junit.AfterClass)2 Assert.fail (org.junit.Assert.fail)2 BeforeClass (org.junit.BeforeClass)2