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();
}
}
}
};
}
Aggregations