use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class VespaDocumentDeserializer42 method getValueUpdate.
public ValueUpdate getValueUpdate(DataType superType, DataType subType) {
int vuTypeId = getInt(null);
ValueUpdate.ValueUpdateClassID op = ValueUpdate.ValueUpdateClassID.getID(vuTypeId);
if (op == null) {
throw new IllegalArgumentException("Read type " + vuTypeId + " of bytebuffer, but this is not a legal value update type.");
}
switch(op) {
case ADD:
{
FieldValue fval = subType.createFieldValue();
fval.deserialize(this);
int weight = getInt(null);
return new AddValueUpdate(fval, weight);
}
case ARITHMETIC:
int opId = getInt(null);
ArithmeticValueUpdate.Operator operator = ArithmeticValueUpdate.Operator.getID(opId);
double operand = getDouble(null);
return new ArithmeticValueUpdate(operator, operand);
case ASSIGN:
{
byte contents = getByte(null);
FieldValue fval = null;
if (contents == (byte) 1) {
fval = superType.createFieldValue();
fval.deserialize(this);
}
return new AssignValueUpdate(fval);
}
case CLEAR:
return new ClearValueUpdate();
case MAP:
if (superType instanceof ArrayDataType) {
CollectionDataType type = (CollectionDataType) superType;
IntegerFieldValue index = new IntegerFieldValue();
index.deserialize(this);
ValueUpdate update = getValueUpdate(type.getNestedType(), null);
return new MapValueUpdate(index, update);
} else if (superType instanceof WeightedSetDataType) {
CollectionDataType type = (CollectionDataType) superType;
FieldValue fval = type.getNestedType().createFieldValue();
fval.deserialize(this);
ValueUpdate update = getValueUpdate(DataType.INT, null);
return new MapValueUpdate(fval, update);
} else {
throw new DeserializationException("MapValueUpdate only works for arrays and weighted sets");
}
case REMOVE:
FieldValue fval = ((CollectionDataType) superType).getNestedType().createFieldValue();
fval.deserialize(this);
return new RemoveValueUpdate(fval);
default:
throw new DeserializationException("Could not deserialize ValueUpdate, unknown valueUpdateClassID type " + vuTypeId);
}
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class DocumentUpdateTestCase method testCppDocUpd.
public void testCppDocUpd() throws IOException {
docMan = DocumentTestCase.setUpCppDocType();
byte[] data = DocumentTestCase.readFile("src/tests/data/serializeupdatecpp.dat");
DocumentDeserializer buf = DocumentDeserializerFactory.create42(docMan, GrowableByteBuffer.wrap(data));
DocumentType type = docMan.getDocumentType("serializetest");
DocumentUpdate upd = new DocumentUpdate(buf);
assertEquals(new DocumentId("doc:update:test"), upd.getId());
assertEquals(type, upd.getType());
FieldUpdate serAssignFU = upd.getFieldUpdate(0);
assertEquals(type.getField("intfield"), serAssignFU.getField());
ValueUpdate serAssign = serAssignFU.getValueUpdate(0);
assertEquals(ValueUpdate.ValueUpdateClassID.ASSIGN, serAssign.getValueUpdateClassID());
assertEquals(new IntegerFieldValue(4), serAssign.getValue());
FieldUpdate serAddFU = upd.getFieldUpdate(2);
assertEquals(type.getField("arrayoffloatfield"), serAddFU.getField());
ValueUpdate serAdd1 = serAddFU.getValueUpdate(0);
assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd1.getValueUpdateClassID());
FloatFieldValue addParam1 = (FloatFieldValue) serAdd1.getValue();
assertEquals(new FloatFieldValue(5.00f), addParam1);
ValueUpdate serAdd2 = serAddFU.getValueUpdate(1);
assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd2.getValueUpdateClassID());
FloatFieldValue addparam2 = (FloatFieldValue) serAdd2.getValue();
assertEquals(new FloatFieldValue(4.23f), addparam2);
ValueUpdate serAdd3 = serAddFU.getValueUpdate(2);
assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd3.getValueUpdateClassID());
FloatFieldValue addparam3 = (FloatFieldValue) serAdd3.getValue();
assertEquals(new FloatFieldValue(-1.00f), addparam3);
FieldUpdate arithFU = upd.getFieldUpdate(3);
assertEquals(type.getField("intfield"), serAssignFU.getField());
ValueUpdate serArith = arithFU.getValueUpdate(0);
assertEquals(ValueUpdate.ValueUpdateClassID.ARITHMETIC, serArith.getValueUpdateClassID());
FieldUpdate wsetFU = upd.getFieldUpdate(4);
assertEquals(type.getField("wsfield"), wsetFU.getField());
assertEquals(2, wsetFU.size());
ValueUpdate mapUpd = wsetFU.getValueUpdate(0);
assertEquals(ValueUpdate.ValueUpdateClassID.MAP, mapUpd.getValueUpdateClassID());
mapUpd = wsetFU.getValueUpdate(1);
assertEquals(ValueUpdate.ValueUpdateClassID.MAP, mapUpd.getValueUpdateClassID());
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class ValueUpdateToDocumentTestCase method requireThatIntegerFieldsAreConverted.
@Test
public void requireThatIntegerFieldsAreConverted() {
DocumentType docType = new DocumentType("my_type");
Field field = new Field("my_int", DataType.INT);
docType.addField(field);
ValueUpdate update = ValueUpdate.createAssign(new IntegerFieldValue(42));
Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("doc:foo:1"), field, update);
assertNotNull(doc);
assertEquals(42, ((IntegerFieldValue) doc.getFieldValue("my_int")).getInteger());
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class ValueUpdateToDocumentTestCase method requireThatArrayFieldsAreConverted.
@SuppressWarnings({ "unchecked" })
@Test
public void requireThatArrayFieldsAreConverted() {
DocumentType docType = new DocumentType("my_type");
ArrayDataType arrType = DataType.getArray(DataType.INT);
Field field = new Field("my_arr", arrType);
docType.addField(field);
Array<IntegerFieldValue> arrVal = arrType.createFieldValue();
arrVal.add(new IntegerFieldValue(6));
arrVal.add(new IntegerFieldValue(9));
ValueUpdate update = ValueUpdate.createAssign(arrVal);
Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("doc:foo:1"), field, update);
assertNotNull(doc);
FieldValue obj = doc.getFieldValue("my_arr");
assertTrue(obj instanceof Array);
Array arr = (Array) obj;
assertEquals(2, arr.size());
assertEquals(new IntegerFieldValue(6), arr.get(0));
assertEquals(new IntegerFieldValue(9), arr.get(1));
}
use of com.yahoo.document.update.ValueUpdate in project vespa by vespa-engine.
the class ValueUpdateToDocumentTestCase method requireThatRemoveIsConverted.
@Test
public void requireThatRemoveIsConverted() {
DocumentType docType = new DocumentType("my_type");
ArrayDataType arrType = DataType.getArray(DataType.INT);
Field field = new Field("my_arr", arrType);
docType.addField(field);
ValueUpdate update = ValueUpdate.createClear();
Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("doc:foo:1"), field, update);
assertNotNull(doc);
FieldValue obj = doc.getFieldValue("my_arr");
assertTrue(obj instanceof Array);
Array arr = (Array) obj;
assertEquals(0, arr.size());
}
Aggregations