use of org.apache.lucene.util.mutable.MutableValueStr in project lucene-solr by apache.
the class MultiStringFunction method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final FunctionValues[] valsArr = new FunctionValues[sources.length];
for (int i = 0; i < sources.length; i++) {
valsArr[i] = sources[i].getValues(context, readerContext);
}
return new StrDocValues(this) {
@Override
public String strVal(int doc) throws IOException {
CharSequence cs = func(doc, valsArr);
return cs != null ? cs.toString() : null;
}
@Override
public boolean exists(int doc) throws IOException {
boolean exists = true;
for (FunctionValues val : valsArr) {
exists = exists & val.exists(doc);
}
return exists;
}
@Override
public boolean bytesVal(int doc, BytesRefBuilder bytes) throws IOException {
bytes.clear();
CharSequence cs = func(doc, valsArr);
if (cs != null) {
bytes.copyChars(func(doc, valsArr));
return true;
} else {
return false;
}
}
@Override
public String toString(int doc) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(name()).append('(');
boolean firstTime = true;
for (FunctionValues vals : valsArr) {
if (firstTime) {
firstTime = false;
} else {
sb.append(',');
}
sb.append(vals.toString(doc));
}
sb.append(')');
return sb.toString();
}
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueStr mval = new MutableValueStr();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) throws IOException {
mval.exists = bytesVal(doc, mval.value);
}
};
}
};
}
use of org.apache.lucene.util.mutable.MutableValueStr in project lucene-solr by apache.
the class TestGrouping method createSecondPassCollector.
// Basically converts searchGroups from MutableValue to BytesRef if grouping by ValueSource
@SuppressWarnings("unchecked")
private TopGroupsCollector<?> createSecondPassCollector(FirstPassGroupingCollector<?> firstPassGroupingCollector, String groupField, Collection<SearchGroup<BytesRef>> searchGroups, Sort groupSort, Sort sortWithinGroup, int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields) throws IOException {
if (firstPassGroupingCollector.getGroupSelector().getClass().isAssignableFrom(TermGroupSelector.class)) {
GroupSelector<BytesRef> selector = (GroupSelector<BytesRef>) firstPassGroupingCollector.getGroupSelector();
return new TopGroupsCollector<>(selector, searchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
} else {
ValueSource vs = new BytesRefFieldSource(groupField);
List<SearchGroup<MutableValue>> mvalSearchGroups = new ArrayList<>(searchGroups.size());
for (SearchGroup<BytesRef> mergedTopGroup : searchGroups) {
SearchGroup<MutableValue> sg = new SearchGroup<>();
MutableValueStr groupValue = new MutableValueStr();
if (mergedTopGroup.groupValue != null) {
groupValue.value.copyBytes(mergedTopGroup.groupValue);
} else {
groupValue.exists = false;
}
sg.groupValue = groupValue;
sg.sortValues = mergedTopGroup.sortValues;
mvalSearchGroups.add(sg);
}
ValueSourceGroupSelector selector = new ValueSourceGroupSelector(vs, new HashMap<>());
return new TopGroupsCollector<>(selector, mvalSearchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
}
}
use of org.apache.lucene.util.mutable.MutableValueStr in project lucene-solr by apache.
the class BytesRefFieldSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
// TODO: do it cleaner?
if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) {
final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field);
return new FunctionValues() {
int lastDocID = -1;
private BytesRef getValueForDoc(int doc) throws IOException {
if (doc < lastDocID) {
throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
}
lastDocID = doc;
int curDocID = binaryValues.docID();
if (doc > curDocID) {
curDocID = binaryValues.advance(doc);
}
if (doc == curDocID) {
return binaryValues.binaryValue();
} else {
return null;
}
}
@Override
public boolean exists(int doc) throws IOException {
return getValueForDoc(doc) != null;
}
@Override
public boolean bytesVal(int doc, BytesRefBuilder target) throws IOException {
BytesRef value = getValueForDoc(doc);
if (value == null || value.length == 0) {
return false;
} else {
target.copyBytes(value);
return true;
}
}
public String strVal(int doc) throws IOException {
final BytesRefBuilder bytes = new BytesRefBuilder();
return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null;
}
@Override
public Object objectVal(int doc) throws IOException {
return strVal(doc);
}
@Override
public String toString(int doc) throws IOException {
return description() + '=' + strVal(doc);
}
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueStr mval = new MutableValueStr();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) throws IOException {
BytesRef value = getValueForDoc(doc);
mval.exists = value != null;
mval.value.clear();
if (value != null) {
mval.value.copyBytes(value);
}
}
};
}
};
} else {
return new DocTermsIndexDocValues(this, readerContext, field) {
@Override
protected String toTerm(String readableValue) {
return readableValue;
}
@Override
public Object objectVal(int doc) throws IOException {
return strVal(doc);
}
@Override
public String toString(int doc) throws IOException {
return description() + '=' + strVal(doc);
}
};
}
}
use of org.apache.lucene.util.mutable.MutableValueStr in project lucene-solr by apache.
the class DocTermsIndexDocValues method getValueFiller.
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueStr mval = new MutableValueStr();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) throws IOException {
int ord = getOrdForDoc(doc);
mval.value.clear();
mval.exists = ord >= 0;
if (mval.exists) {
mval.value.copyBytes(termsIndex.lookupOrd(ord));
}
}
};
}
Aggregations