Skip to content

Commit

Permalink
remove FieldsEnum and replace with Iterator<String>
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/branch_4x@1372246 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rmuir committed Aug 13, 2012
1 parent 2f85e11 commit ac1d9eb
Show file tree
Hide file tree
Showing 50 changed files with 501 additions and 907 deletions.
4 changes: 4 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ API Changes
otherwise returns the current payload. You can now call it multiple
times per position if you want. (Robert Muir)

* Removed FieldsEnum. Fields API instead implements Iterable<String>
and exposes Iterator, so you can iterate over field names with
for (String field : fields) instead. (Robert Muir)

Bug Fixes

* LUCENE-4297: BooleanScorer2 would multiply the coord() factor
Expand Down
8 changes: 3 additions & 5 deletions lucene/MIGRATE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enumeration APIs. Here are the major changes:
by the BytesRef class (which provides an offset + length "slice"
into an existing byte[]).

* Fields are separately enumerated (FieldsEnum) from the terms
* Fields are separately enumerated (Fields.iterator()) from the terms
within each field (TermEnum). So instead of this:

TermEnum termsEnum = ...;
Expand All @@ -20,10 +20,8 @@ enumeration APIs. Here are the major changes:

Do this:

FieldsEnum fieldsEnum = ...;
String field;
while((field = fieldsEnum.next()) != null) {
TermsEnum termsEnum = fieldsEnum.terms();
for(String field : fields) {
TermsEnum termsEnum = fields.terms(field);
BytesRef text;
while((text = termsEnum.next()) != null) {
System.out.println("field=" + field + "; text=" + text.utf8ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
Expand Down Expand Up @@ -493,13 +492,13 @@ public void testReadTokens() throws Exception {

int totalTokenCount2 = 0;

FieldsEnum fields = MultiFields.getFields(reader).iterator();
String fieldName = null;
while((fieldName = fields.next()) != null) {
Fields fields = MultiFields.getFields(reader);

for (String fieldName : fields) {
if (fieldName.equals(DocMaker.ID_FIELD) || fieldName.equals(DocMaker.DATE_MSEC_FIELD) || fieldName.equals(DocMaker.TIME_SEC_FIELD)) {
continue;
}
Terms terms = fields.terms();
Terms terms = fields.terms(fieldName);
if (terms == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.Terms;
Expand All @@ -40,6 +39,7 @@
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.DoubleBarrelLRUCache;
import org.apache.lucene.util.UnmodifiableIterator;

/** Handles a terms dict, but decouples all details of
* doc/freqs/positions reading to an instance of {@link
Expand Down Expand Up @@ -184,8 +184,8 @@ public void close() throws IOException {
}

@Override
public FieldsEnum iterator() {
return new TermFieldsEnum();
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
}

@Override
Expand All @@ -199,32 +199,6 @@ public int size() {
return fields.size();
}

// Iterates through all fields
private class TermFieldsEnum extends FieldsEnum {
final Iterator<FieldReader> it;
FieldReader current;

TermFieldsEnum() {
it = fields.values().iterator();
}

@Override
public String next() {
if (it.hasNext()) {
current = it.next();
return current.fieldInfo.name;
} else {
current = null;
return null;
}
}

@Override
public Terms terms() throws IOException {
return current;
}
}

private class FieldReader extends Terms {
final long numTerms;
final FieldInfo fieldInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.Terms;
Expand All @@ -46,6 +45,7 @@
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.UnmodifiableIterator;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.apache.lucene.util.automaton.RunAutomaton;
import org.apache.lucene.util.automaton.Transition;
Expand Down Expand Up @@ -199,8 +199,8 @@ public void close() throws IOException {
}

@Override
public FieldsEnum iterator() {
return new TermFieldsEnum();
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
}

@Override
Expand All @@ -214,32 +214,6 @@ public int size() {
return fields.size();
}

// Iterates through all fields
private class TermFieldsEnum extends FieldsEnum {
final Iterator<FieldReader> it;
FieldReader current;

TermFieldsEnum() {
it = fields.values().iterator();
}

@Override
public String next() {
if (it.hasNext()) {
current = it.next();
return current.fieldInfo.name;
} else {
current = null;
return null;
}
}

@Override
public Terms terms() throws IOException {
return current;
}
}

// for debugging
String brToString(BytesRef b) {
if (b == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.MergeState;
import org.apache.lucene.index.SegmentWriteState; // javadocs
import org.apache.lucene.index.Terms;
Expand Down Expand Up @@ -53,13 +52,10 @@ public abstract class FieldsConsumer implements Closeable {
public abstract void close() throws IOException;

public void merge(MergeState mergeState, Fields fields) throws IOException {
FieldsEnum fieldsEnum = fields.iterator();
assert fieldsEnum != null;
String field;
while((field = fieldsEnum.next()) != null) {
for (String field : fields) {
mergeState.fieldInfo = mergeState.fieldInfos.fieldInfo(field);
assert mergeState.fieldInfo != null : "FieldInfo for field is null: "+ field;
Terms terms = fieldsEnum.terms();
Terms terms = fields.terms(field);
if (terms != null) {
final TermsConsumer termsConsumer = addField(mergeState.fieldInfo);
termsConsumer.merge(mergeState, terms.iterator(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.MergeState;
import org.apache.lucene.index.PayloadProcessorProvider.PayloadProcessor;
import org.apache.lucene.index.PayloadProcessorProvider.ReaderPayloadProcessor;
Expand Down Expand Up @@ -211,8 +210,6 @@ protected final void addAllDocVectors(Fields vectors, MergeState mergeState) thr
}
startDocument(numFields);

final FieldsEnum fieldsEnum = vectors.iterator();
String fieldName;
String lastFieldName = null;

TermsEnum termsEnum = null;
Expand All @@ -221,13 +218,13 @@ protected final void addAllDocVectors(Fields vectors, MergeState mergeState) thr
final ReaderPayloadProcessor readerPayloadProcessor = mergeState.currentReaderPayloadProcessor;
PayloadProcessor payloadProcessor = null;

while((fieldName = fieldsEnum.next()) != null) {
for(String fieldName : vectors) {
final FieldInfo fieldInfo = mergeState.fieldInfos.fieldInfo(fieldName);

assert lastFieldName == null || fieldName.compareTo(lastFieldName) > 0: "lastFieldName=" + lastFieldName + " fieldName=" + fieldName;
lastFieldName = fieldName;

final Terms terms = fieldsEnum.terms();
final Terms terms = vectors.terms(fieldName);
if (terms == null) {
// FieldsEnum shouldn't lie...
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -35,7 +36,6 @@
import org.apache.lucene.index.DocsAndPositionsEnum;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
Expand All @@ -44,7 +44,6 @@
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FuzzySet;
Expand Down Expand Up @@ -187,9 +186,8 @@ public BloomFilteredFieldsProducer(SegmentReadState state)

}

public FieldsEnum iterator() throws IOException {
return new BloomFilteredFieldsEnum(delegateFieldsProducer.iterator(),
bloomsByFieldName);
public Iterator<String> iterator() {
return delegateFieldsProducer.iterator();
}

public void close() throws IOException {
Expand Down Expand Up @@ -217,44 +215,6 @@ public long getUniqueTermCount() throws IOException {
return delegateFieldsProducer.getUniqueTermCount();
}

// Not all fields in a segment may be subject to a bloom filter. This class
// wraps Terms objects appropriately if a filtering request is present
class BloomFilteredFieldsEnum extends FieldsEnum {
private FieldsEnum delegateFieldsEnum;
private HashMap<String,FuzzySet> bloomsByFieldName;
private String currentFieldName;

public BloomFilteredFieldsEnum(FieldsEnum iterator,
HashMap<String,FuzzySet> bloomsByFieldName) {
this.delegateFieldsEnum = iterator;
this.bloomsByFieldName = bloomsByFieldName;
}

public AttributeSource attributes() {
return delegateFieldsEnum.attributes();
}

public String next() throws IOException {
currentFieldName = delegateFieldsEnum.next();
return currentFieldName;
}

public Terms terms() throws IOException {
FuzzySet filter = bloomsByFieldName.get(currentFieldName);
if (filter == null) {
return delegateFieldsEnum.terms();
} else {
Terms result = delegateFieldsEnum.terms();
if (result == null) {
return null;
}
// wrap the terms object with a bloom filter
return new BloomFilteredTerms(result, filter);
}
}

}

class BloomFilteredTerms extends Terms {
private Terms delegateTerms;
private FuzzySet filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.FieldsEnum;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.Term;
Expand All @@ -42,6 +41,7 @@
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util.UnmodifiableIterator;

/** Exposes flex API on a pre-flex index, as a codec.
* @lucene.experimental
Expand Down Expand Up @@ -133,8 +133,8 @@ protected boolean sortTermsByUnicode() {
}

@Override
public FieldsEnum iterator() throws IOException {
return new PreFlexFieldsEnum();
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
}

@Override
Expand Down Expand Up @@ -178,30 +178,6 @@ public void close() throws IOException {
proxStream.close();
}
}

private class PreFlexFieldsEnum extends FieldsEnum {
final Iterator<FieldInfo> it;
FieldInfo current;

public PreFlexFieldsEnum() throws IOException {
it = fields.values().iterator();
}

@Override
public String next() {
if (it.hasNext()) {
current = it.next();
return current.name;
} else {
return null;
}
}

@Override
public Terms terms() throws IOException {
return Lucene3xFields.this.terms(current.name);
}
}

private class PreTerms extends Terms {
final FieldInfo fieldInfo;
Expand Down
Loading

0 comments on commit ac1d9eb

Please sign in to comment.