Search in sources :

Example 1 with PrefetchingResourceIterator

use of org.neo4j.helpers.collection.PrefetchingResourceIterator 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)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 SnapshotDeletionPolicy (org.apache.lucene.index.SnapshotDeletionPolicy)1 ResourceIterator (org.neo4j.graphdb.ResourceIterator)1 Pair (org.neo4j.helpers.collection.Pair)1 PrefetchingResourceIterator (org.neo4j.helpers.collection.PrefetchingResourceIterator)1