Skip to content

Commit

Permalink
VUFIND-1342 Have our SolrFieldIterators implement Iterator and Iterable
Browse files Browse the repository at this point in the history
Also modifies PrintBrowseHeadings to take advantage of the Iterable.
  • Loading branch information
marktriggs authored and demiankatz committed Dec 12, 2023
1 parent 000f41d commit 57e587e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 54 additions & 2 deletions src/main/java/org/vufind/solr/indexing/SolrFieldIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +24,7 @@
import org.vufind.util.NormalizerFactory;


public class SolrFieldIterator implements AutoCloseable
public class SolrFieldIterator implements AutoCloseable, Iterator<BrowseEntry>, Iterable<BrowseEntry>
{
protected CompositeReader reader;
protected IndexSearcher searcher;
Expand All @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<BrowseEntry> iterator() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 57e587e

Please sign in to comment.