blob: 0df3d8c7f227770e2b25a06c8b685da6b2cb6206 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2015, 2017 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.r.core.rhelp.index;
import java.io.IOException;
import java.util.Set;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Scorer;
public class DocFieldVisitorCollector extends Collector {
public abstract static class Visitor extends StoredFieldVisitor {
private final Set<String> fieldNames;
public Visitor(final Set<String> fieldNames) {
this.fieldNames= fieldNames;
}
@Override
public Status needsField(final FieldInfo fieldInfo) throws IOException {
return this.fieldNames.contains(fieldInfo.name) ? Status.YES : Status.NO;
}
public abstract void newDocMatch(AtomicReader reader, int doc, float score) throws IOException;
public abstract void finalizeDocMatch() throws IOException;
}
private final Visitor visitor;
private Scorer scorer;
private AtomicReader reader;
public DocFieldVisitorCollector(final Visitor visitor) {
this.visitor= visitor;
}
@Override
public void setScorer(final Scorer scorer) throws IOException {
this.scorer= scorer;
}
@Override
public void setNextReader(final AtomicReaderContext context) throws IOException {
this.reader= context.reader();
}
@Override
public boolean acceptsDocsOutOfOrder() {
return true;
}
@Override
public void collect(final int doc) throws IOException {
final float score= this.scorer.score();
if (score > 0.0f) {
this.visitor.newDocMatch(this.reader, doc, score);
this.reader.document(doc, this.visitor);
this.visitor.finalizeDocMatch();
}
}
}