use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class MapValueUpdate method applyTo.
@Override
public FieldValue applyTo(FieldValue fval) {
if (fval instanceof Array) {
Array array = (Array) fval;
FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
element = update.applyTo(element);
array.set(((IntegerFieldValue) value).getInteger(), element);
} else if (fval instanceof WeightedSet) {
WeightedSet wset = (WeightedSet) fval;
WeightedSetDataType wtype = wset.getDataType();
Integer weight = wset.get(value);
if (weight == null) {
if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
weight = 0;
} else {
return fval;
}
}
weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
wset.put(value, weight);
if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
wset.remove(value);
}
}
return fval;
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readRemove.
FieldUpdate readRemove(DocumentUpdate update) throws XMLStreamException {
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("field".equals(reader.getAttributeName(i).toString())) {
Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
FieldValue value = f.getDataType().createFieldValue();
value.deserialize(f, this);
if (value instanceof Array) {
List<FieldValue> l = ((Array) value).getValues();
return FieldUpdate.createRemoveAll(f, l);
} else if (value instanceof WeightedSet) {
return FieldUpdate.createRemoveAll(f, ((WeightedSet) value));
} else {
throw newDeserializeException("Remove operation only applicable to multivalue lists");
}
}
}
throw newDeserializeException("Remove update without field attribute");
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readAdd.
FieldUpdate readAdd(DocumentUpdate update) throws XMLStreamException {
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("field".equals(reader.getAttributeName(i).toString())) {
Field f = update.getDocumentType().getField(reader.getAttributeValue(i));
FieldValue value = f.getDataType().createFieldValue();
value.deserialize(f, this);
if (value instanceof Array) {
List<FieldValue> l = ((Array) value).getValues();
return FieldUpdate.createAddAll(f, l);
} else if (value instanceof WeightedSet) {
return FieldUpdate.createAddAll(f, ((WeightedSet) value));
} else {
throw newDeserializeException("Add operation only applicable to multivalue lists");
}
}
}
throw newDeserializeException("Add update without field attribute");
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class AddValueUpdate method applyTo.
@Override
public FieldValue applyTo(FieldValue val) {
if (val instanceof WeightedSet) {
WeightedSet wset = (WeightedSet) val;
wset.put(value, weight);
} else if (val instanceof CollectionFieldValue) {
CollectionFieldValue fval = (CollectionFieldValue) val;
fval.add(value);
} else {
throw new IllegalStateException("Cannot add " + value + " to field of type " + val.getClass().getName());
}
return val;
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class JsonReaderTestCase method testWeightedSet.
@Test
public final void testWeightedSet() throws IOException {
InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testset::whee\"," + " \"fields\": { \"actualset\": {" + " \"nalle\": 2," + " \"tralle\": 7 }}}"));
JsonReader r = new JsonReader(types, rawDoc, parserFactory);
DocumentParseInfo parseInfo = r.parseDocument().get();
DocumentType docType = r.readDocumentType(parseInfo.documentId);
DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId));
new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put);
Document doc = put.getDocument();
FieldValue f = doc.getFieldValue(doc.getField("actualset"));
assertSame(WeightedSet.class, f.getClass());
WeightedSet<?> w = (WeightedSet<?>) f;
assertEquals(2, w.size());
assertEquals(new Integer(2), w.get(new StringFieldValue("nalle")));
assertEquals(new Integer(7), w.get(new StringFieldValue("tralle")));
}
Aggregations