use of org.apache.lucene.util.mutable.MutableValueInt in project elasticsearch by elastic.
the class CopyOnWriteHashMap method copyAndPut.
/**
* Associate <code>key</code> with <code>value</code> and return a new copy
* of the hash table. The current hash table is not modified.
*/
public CopyOnWriteHashMap<K, V> copyAndPut(K key, V value) {
if (key == null) {
throw new IllegalArgumentException("null keys are not supported");
}
if (value == null) {
throw new IllegalArgumentException("null values are not supported");
}
final int hash = key.hashCode();
final MutableValueInt newValue = new MutableValueInt();
final InnerNode<K, V> newRoot = root.put(key, hash, TOTAL_HASH_BITS, value, newValue);
final int newSize = size + newValue.value;
return new CopyOnWriteHashMap<>(newRoot, newSize);
}
use of org.apache.lucene.util.mutable.MutableValueInt in project lucene-solr by apache.
the class EnumFieldSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final NumericDocValues arr = DocValues.getNumeric(readerContext.reader(), field);
return new IntDocValues(this) {
final MutableValueInt val = new MutableValueInt();
int lastDocID;
private int getValueForDoc(int doc) throws IOException {
if (doc < lastDocID) {
throw new AssertionError("docs were sent out-of-order: lastDocID=" + lastDocID + " vs doc=" + doc);
}
lastDocID = doc;
int curDocID = arr.docID();
if (doc > curDocID) {
curDocID = arr.advance(doc);
}
if (doc == curDocID) {
return (int) arr.longValue();
} else {
return 0;
}
}
@Override
public int intVal(int doc) throws IOException {
return getValueForDoc(doc);
}
@Override
public String strVal(int doc) throws IOException {
Integer intValue = intVal(doc);
return intValueToStringValue(intValue);
}
@Override
public boolean exists(int doc) throws IOException {
getValueForDoc(doc);
return arr.docID() == doc;
}
@Override
public ValueSourceScorer getRangeScorer(LeafReaderContext readerContext, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
Integer lower = stringValueToIntValue(lowerVal);
Integer upper = stringValueToIntValue(upperVal);
if (lower == null) {
lower = Integer.MIN_VALUE;
} else {
if (!includeLower && lower < Integer.MAX_VALUE)
lower++;
}
if (upper == null) {
upper = Integer.MAX_VALUE;
} else {
if (!includeUpper && upper > Integer.MIN_VALUE)
upper--;
}
final int ll = lower;
final int uu = upper;
return new ValueSourceScorer(readerContext, this) {
@Override
public boolean matches(int doc) throws IOException {
if (!exists(doc))
return false;
int val = intVal(doc);
return val >= ll && val <= uu;
}
};
}
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueInt mval = new MutableValueInt();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) throws IOException {
mval.value = intVal(doc);
mval.exists = arr.docID() == doc;
}
};
}
};
}
use of org.apache.lucene.util.mutable.MutableValueInt in project lucene-solr by apache.
the class GroupConverter method toMutable.
static Collection<SearchGroup<MutableValue>> toMutable(SchemaField field, Collection<SearchGroup<BytesRef>> values) {
FieldType fieldType = field.getType();
List<SearchGroup<MutableValue>> result = new ArrayList<>(values.size());
for (SearchGroup<BytesRef> original : values) {
SearchGroup<MutableValue> converted = new SearchGroup<MutableValue>();
// ?
converted.sortValues = original.sortValues;
NumberType type = fieldType.getNumberType();
final MutableValue v;
switch(type) {
case INTEGER:
MutableValueInt mutableInt = new MutableValueInt();
if (original.groupValue == null) {
mutableInt.value = 0;
mutableInt.exists = false;
} else {
mutableInt.value = (Integer) fieldType.toObject(field, original.groupValue);
}
v = mutableInt;
break;
case FLOAT:
MutableValueFloat mutableFloat = new MutableValueFloat();
if (original.groupValue == null) {
mutableFloat.value = 0;
mutableFloat.exists = false;
} else {
mutableFloat.value = (Float) fieldType.toObject(field, original.groupValue);
}
v = mutableFloat;
break;
case DOUBLE:
MutableValueDouble mutableDouble = new MutableValueDouble();
if (original.groupValue == null) {
mutableDouble.value = 0;
mutableDouble.exists = false;
} else {
mutableDouble.value = (Double) fieldType.toObject(field, original.groupValue);
}
v = mutableDouble;
break;
case LONG:
MutableValueLong mutableLong = new MutableValueLong();
if (original.groupValue == null) {
mutableLong.value = 0;
mutableLong.exists = false;
} else {
mutableLong.value = (Long) fieldType.toObject(field, original.groupValue);
}
v = mutableLong;
break;
case DATE:
MutableValueDate mutableDate = new MutableValueDate();
if (original.groupValue == null) {
mutableDate.value = 0;
mutableDate.exists = false;
} else {
mutableDate.value = ((Date) fieldType.toObject(field, original.groupValue)).getTime();
}
v = mutableDate;
break;
default:
throw new AssertionError();
}
converted.groupValue = v;
result.add(converted);
}
return result;
}
use of org.apache.lucene.util.mutable.MutableValueInt in project lucene-solr by apache.
the class CursorPagingTest method assertFullWalkNoDupsWithFacets.
/**
* Given a set of params, executes a cursor query using {@link CursorMarkParams#CURSOR_MARK_START}
* and then continuously walks the results using {@link CursorMarkParams#CURSOR_MARK_START} as long
* as a non-0 number of docs ar returned. This method records the the set of all id's
* (must be positive ints) encountered and throws an assertion failure if any id is
* encountered more than once, or if the set grows above maxSize.
*
* Also checks that facets are the same with each page, and that they are correct.
*/
public SentinelIntSet assertFullWalkNoDupsWithFacets(int maxSize, SolrParams params) throws Exception {
final String facetField = params.get("facet.field");
assertNotNull("facet.field param not specified", facetField);
assertFalse("facet.field param contains multiple values", facetField.contains(","));
assertEquals("facet.limit param not set to -1", "-1", params.get("facet.limit"));
final Map<String, MutableValueInt> facetCounts = new HashMap<>();
SentinelIntSet ids = new SentinelIntSet(maxSize, -1);
String cursorMark = CURSOR_MARK_START;
int docsOnThisPage = Integer.MAX_VALUE;
List previousFacets = null;
while (0 < docsOnThisPage) {
String json = assertJQ(req(params, CURSOR_MARK_PARAM, cursorMark));
Map rsp = (Map) ObjectBuilder.fromJSON(json);
assertTrue("response doesn't contain " + CURSOR_MARK_NEXT + ": " + json, rsp.containsKey(CURSOR_MARK_NEXT));
String nextCursorMark = (String) rsp.get(CURSOR_MARK_NEXT);
assertNotNull(CURSOR_MARK_NEXT + " is null", nextCursorMark);
List<Map<Object, Object>> docs = (List) (((Map) rsp.get("response")).get("docs"));
docsOnThisPage = docs.size();
if (null != params.getInt(CommonParams.ROWS)) {
int rows = params.getInt(CommonParams.ROWS);
assertTrue("Too many docs on this page: " + rows + " < " + docsOnThisPage, docsOnThisPage <= rows);
}
if (0 == docsOnThisPage) {
assertEquals("no more docs, but " + CURSOR_MARK_NEXT + " isn't same", cursorMark, nextCursorMark);
}
for (Map<Object, Object> doc : docs) {
int id = ((Long) doc.get("id")).intValue();
assertFalse("walk already seen: " + id, ids.exists(id));
ids.put(id);
assertFalse("id set bigger then max allowed (" + maxSize + "): " + ids.size(), maxSize < ids.size());
Object facet = doc.get(facetField);
// null: missing facet value
String facetString = null == facet ? null : facet.toString();
MutableValueInt count = facetCounts.get(facetString);
if (null == count) {
count = new MutableValueInt();
facetCounts.put(facetString, count);
}
++count.value;
}
cursorMark = nextCursorMark;
Map facetFields = (Map) ((Map) rsp.get("facet_counts")).get("facet_fields");
List facets = (List) facetFields.get(facetField);
if (null != previousFacets) {
assertEquals("Facets not the same as on previous page:\nprevious page facets: " + Arrays.toString(facets.toArray(new Object[facets.size()])) + "\ncurrent page facets: " + Arrays.toString(previousFacets.toArray(new Object[previousFacets.size()])), previousFacets, facets);
}
previousFacets = facets;
}
assertNotNull("previousFacets is null", previousFacets);
assertEquals("Mismatch in number of facets: ", facetCounts.size(), previousFacets.size() / 2);
int pos;
for (pos = 0; pos < previousFacets.size(); pos += 2) {
String label = (String) previousFacets.get(pos);
int expectedCount = ((Number) previousFacets.get(pos + 1)).intValue();
MutableValueInt count = facetCounts.get(label);
assertNotNull("Expected facet label #" + (pos / 2) + " not found: '" + label + "'", count);
assertEquals("Facet count mismatch for label #" + (pos / 2) + " '" + label + "'", expectedCount, facetCounts.get(label).value);
pos += 2;
}
return ids;
}
use of org.apache.lucene.util.mutable.MutableValueInt in project lucene-solr by apache.
the class TrieIntField method getSingleValueSource.
@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
return new SortedSetFieldSource(f.getName(), choice) {
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
// needed for nested anon class ref
SortedSetFieldSource thisAsSortedSetFieldSource = this;
SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
return new IntDocValues(thisAsSortedSetFieldSource) {
private int lastDocID;
private boolean setDoc(int docID) throws IOException {
if (docID < lastDocID) {
throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
}
if (docID > view.docID()) {
lastDocID = docID;
return docID == view.advance(docID);
} else {
return docID == view.docID();
}
}
@Override
public int intVal(int doc) throws IOException {
if (setDoc(doc)) {
BytesRef bytes = view.binaryValue();
assert bytes.length > 0;
return LegacyNumericUtils.prefixCodedToInt(bytes);
} else {
return 0;
}
}
@Override
public boolean exists(int doc) throws IOException {
return setDoc(doc);
}
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueInt mval = new MutableValueInt();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) throws IOException {
if (setDoc(doc)) {
mval.exists = true;
mval.value = LegacyNumericUtils.prefixCodedToInt(view.binaryValue());
} else {
mval.exists = false;
mval.value = 0;
}
}
};
}
};
}
};
}
Aggregations