From b34f6070ba9c78a912fbd6d6ec9483e9640d3777 Mon Sep 17 00:00:00 2001 From: Mark Triggs Date: Tue, 12 Dec 2023 13:02:26 +1100 Subject: [PATCH] VUFIND-1342 Have our SolrFieldIterators implement Iterator and Iterable Also modifies PrintBrowseHeadings to take advantage of the Iterable. --- .../solr/indexing/PrintBrowseHeadings.java | 3 +- .../solr/indexing/SolrFieldIterator.java | 56 ++++++++++++++++++- .../solr/indexing/StoredFieldIterator.java | 6 +- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/vufind/solr/indexing/PrintBrowseHeadings.java b/src/main/java/org/vufind/solr/indexing/PrintBrowseHeadings.java index 7285efc..da59bc7 100644 --- a/src/main/java/org/vufind/solr/indexing/PrintBrowseHeadings.java +++ b/src/main/java/org/vufind/solr/indexing/PrintBrowseHeadings.java @@ -51,8 +51,7 @@ private void loadHeadings(SolrFieldIterator fieldIterator, Predicate predicate) throws Exception { - BrowseEntry h; - while ((h = fieldIterator.next()) != null) { + for (BrowseEntry h : fieldIterator) { // We use a byte array for the sort key instead of a string to ensure // consistent sorting even if the index tool and browse handler are running // with different locale settings. Using strings results in less predictable diff --git a/src/main/java/org/vufind/solr/indexing/SolrFieldIterator.java b/src/main/java/org/vufind/solr/indexing/SolrFieldIterator.java index 89df5e7..c0889c7 100644 --- a/src/main/java/org/vufind/solr/indexing/SolrFieldIterator.java +++ b/src/main/java/org/vufind/solr/indexing/SolrFieldIterator.java @@ -3,7 +3,9 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import org.apache.lucene.index.CompositeReader; import org.apache.lucene.index.DirectoryReader; @@ -22,7 +24,7 @@ import org.vufind.util.NormalizerFactory; -public class SolrFieldIterator implements AutoCloseable +public class SolrFieldIterator implements AutoCloseable, Iterator, Iterable { protected CompositeReader reader; protected IndexSearcher searcher; @@ -34,6 +36,8 @@ public class SolrFieldIterator implements AutoCloseable TermsEnum tenum = null; + private BrowseEntry nextEntry = null; + private boolean exhausted = false; public SolrFieldIterator(String indexPath, String field) throws Exception { @@ -83,7 +87,7 @@ private boolean termExists(String t) // // If there's no currently selected TermEnum, create one from the reader. // - public BrowseEntry next() throws Exception + protected BrowseEntry readNext() throws IOException { for (;;) { if (tenum == null) { @@ -123,4 +127,52 @@ public BrowseEntry next() throws Exception // Try the next term } } + + + public void tryReadNext() { + if (nextEntry != null) { + // Already have one + return; + } + + if (exhausted) { + // Nothing more to read + } + + try { + nextEntry = readNext(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + if (nextEntry == null) { + exhausted = true; + } + } + + @Override + public BrowseEntry next() { + tryReadNext(); + + if (nextEntry == null) { + throw new NoSuchElementException(); + } + + BrowseEntry result = nextEntry; + nextEntry = null; + + return result; + } + + @Override + public boolean hasNext() { + tryReadNext(); + + return nextEntry != null; + } + + @Override + public Iterator iterator() { + return this; + } } diff --git a/src/main/java/org/vufind/solr/indexing/StoredFieldIterator.java b/src/main/java/org/vufind/solr/indexing/StoredFieldIterator.java index 9364c5e..ca3ba0e 100644 --- a/src/main/java/org/vufind/solr/indexing/StoredFieldIterator.java +++ b/src/main/java/org/vufind/solr/indexing/StoredFieldIterator.java @@ -3,6 +3,7 @@ // Build a browse list by walking the docs in an index and extracting sort key // and values from a pair of stored fields. import java.io.File; +import java.io.IOException; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; @@ -56,8 +57,7 @@ public StoredFieldIterator(String indexPath, String field) throws Exception } - private void loadDocument(IndexReader reader, int docid) - throws Exception + private void loadDocument(IndexReader reader, int docid) throws IOException { Document doc = reader.storedFields().document(currentDoc, fieldSelection); @@ -88,7 +88,7 @@ private void loadDocument(IndexReader reader, int docid) } - public BrowseEntry next() throws Exception + protected BrowseEntry readNext() throws IOException { while (buffer.isEmpty()) { if (currentDoc < reader.maxDoc()) {