use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class ValueUpdateToDocumentTestCase method requireThatStringFieldsAreConverted.
@Test
public void requireThatStringFieldsAreConverted() {
DocumentType docType = new DocumentType("my_type");
Field field = new Field("my_str", DataType.STRING);
docType.addField(field);
ValueUpdate update = ValueUpdate.createAssign(new StringFieldValue("42"));
Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("doc:foo:1"), field, update);
assertNotNull(doc);
assertEquals("42", ((StringFieldValue) doc.getFieldValue("my_str")).getString());
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class SimpleAdapterFactory method newUpdateAdapterList.
@Override
public List<UpdateAdapter> newUpdateAdapterList(DocumentUpdate upd) {
List<UpdateAdapter> ret = new ArrayList<>();
DocumentType docType = upd.getDocumentType();
DocumentId docId = upd.getId();
Document complete = new Document(docType, upd.getId());
for (FieldPathUpdate fieldUpd : upd) {
if (FieldPathUpdateHelper.isComplete(fieldUpd)) {
FieldPathUpdateHelper.applyUpdate(fieldUpd, complete);
} else {
Document partial = FieldPathUpdateHelper.newPartialDocument(docId, fieldUpd);
ret.add(new FieldPathUpdateAdapter(newDocumentAdapter(partial, true), fieldUpd));
}
}
for (FieldUpdate fieldUpd : upd.getFieldUpdates()) {
Field field = fieldUpd.getField();
for (ValueUpdate valueUpd : fieldUpd.getValueUpdates()) {
if (FieldUpdateHelper.isComplete(field, valueUpd)) {
FieldUpdateHelper.applyUpdate(field, valueUpd, complete);
} else {
Document partial = FieldUpdateHelper.newPartialDocument(docType, docId, field, valueUpd);
ret.add(FieldUpdateAdapter.fromPartialUpdate(expressionSelector.selectExpression(docType, field.getName()), newDocumentAdapter(partial, true), valueUpd));
}
}
}
ret.add(FieldUpdateAdapter.fromCompleteUpdate(newDocumentAdapter(complete, true)));
return ret;
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class JsonReaderTestCase method testArithmeticOperators.
@SuppressWarnings({ "cast", "unchecked", "rawtypes" })
@Test
public final void testArithmeticOperators() throws IOException {
Tuple2[] operations = new Tuple2[] { new Tuple2<String, Operator>(UPDATE_DECREMENT, ArithmeticValueUpdate.Operator.SUB), new Tuple2<String, Operator>(UPDATE_DIVIDE, ArithmeticValueUpdate.Operator.DIV), new Tuple2<String, Operator>(UPDATE_INCREMENT, ArithmeticValueUpdate.Operator.ADD), new Tuple2<String, Operator>(UPDATE_MULTIPLY, ArithmeticValueUpdate.Operator.MUL) };
for (Tuple2<String, Operator> operator : operations) {
DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"match\": {" + " \"element\": \"person\"," + " \"" + (String) operator.first + "\": 13}}}}");
Map<String, Tuple2<Number, Operator>> matches = new HashMap<>();
FieldUpdate x = doc.getFieldUpdate("actualset");
for (ValueUpdate v : x.getValueUpdates()) {
MapValueUpdate adder = (MapValueUpdate) v;
final String key = ((StringFieldValue) adder.getValue()).getString();
Operator op = ((ArithmeticValueUpdate) adder.getUpdate()).getOperator();
Number n = ((ArithmeticValueUpdate) adder.getUpdate()).getOperand();
matches.put(key, new Tuple2<>(n, op));
}
assertEquals(1, matches.size());
final String o = "person";
assertSame(operator.second, matches.get(o).second);
assertEquals(Double.valueOf(13), matches.get(o).first);
}
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class JsonReaderTestCase method testArrayIndexing.
@SuppressWarnings("rawtypes")
@Test
public final void testArrayIndexing() throws IOException {
DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testarray::whee\"," + " \"fields\": { " + "\"actualarray\": {" + " \"match\": {" + " \"element\": 3," + " \"assign\": \"nalle\"}}}}");
Map<Number, String> matches = new HashMap<>();
FieldUpdate x = doc.getFieldUpdate("actualarray");
for (ValueUpdate v : x.getValueUpdates()) {
MapValueUpdate adder = (MapValueUpdate) v;
final Number key = ((IntegerFieldValue) adder.getValue()).getNumber();
String op = ((StringFieldValue) ((AssignValueUpdate) adder.getUpdate()).getValue()).getString();
matches.put(key, op);
}
assertEquals(1, matches.size());
Number n = Integer.valueOf(3);
assertEquals("nalle", matches.get(n));
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class DocumentUpdateTestCase method testRequireThatAddAllCombinesFieldUpdates.
public void testRequireThatAddAllCombinesFieldUpdates() {
DocumentType docType = new DocumentType("my_type");
Field field = new Field("my_int", DataType.INT);
docType.addField(field);
FieldUpdate fooField = FieldUpdate.createAssign(field, new IntegerFieldValue(1));
DocumentUpdate fooUpdate = new DocumentUpdate(docType, new DocumentId("doc:foo:"));
fooUpdate.addFieldUpdate(fooField);
FieldUpdate barField = FieldUpdate.createAssign(field, new IntegerFieldValue(2));
DocumentUpdate barUpdate = new DocumentUpdate(docType, new DocumentId("doc:foo:"));
barUpdate.addFieldUpdate(barField);
fooUpdate.addAll(barUpdate);
assertEquals(1, fooUpdate.getFieldUpdates().size());
FieldUpdate fieldUpdate = fooUpdate.getFieldUpdate(0);
assertNotNull(fieldUpdate);
assertEquals(field, fieldUpdate.getField());
assertEquals(2, fieldUpdate.getValueUpdates().size());
ValueUpdate valueUpdate = fieldUpdate.getValueUpdate(0);
assertNotNull(valueUpdate);
assertTrue(valueUpdate instanceof AssignValueUpdate);
assertEquals(new IntegerFieldValue(1), valueUpdate.getValue());
assertNotNull(valueUpdate = fieldUpdate.getValueUpdate(1));
assertTrue(valueUpdate instanceof AssignValueUpdate);
assertEquals(new IntegerFieldValue(2), valueUpdate.getValue());
}
Aggregations