Search in sources :

Example 16 with ValueUpdate

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);
    }
}
Also used : MapValueUpdate(com.yahoo.document.update.MapValueUpdate) RemoveValueUpdate(com.yahoo.document.update.RemoveValueUpdate) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) CollectionDataType(com.yahoo.document.CollectionDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) RemoveValueUpdate(com.yahoo.document.update.RemoveValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ReferenceFieldValue(com.yahoo.document.datatypes.ReferenceFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) ArrayDataType(com.yahoo.document.ArrayDataType) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate)

Example 17 with ValueUpdate

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());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 18 with ValueUpdate

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());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) Test(org.junit.Test)

Example 19 with ValueUpdate

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));
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) Test(org.junit.Test)

Example 20 with ValueUpdate

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());
}
Also used : ValueUpdate(com.yahoo.document.update.ValueUpdate) Test(org.junit.Test)

Aggregations

ValueUpdate (com.yahoo.document.update.ValueUpdate)21 Test (org.junit.Test)16 FieldUpdate (com.yahoo.document.update.FieldUpdate)12 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)10 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)8 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)7 DocumentUpdate (com.yahoo.document.DocumentUpdate)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)3 Struct (com.yahoo.document.datatypes.Struct)3 FieldValue (com.yahoo.document.datatypes.FieldValue)2 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)2 DocumentSerializer (com.yahoo.document.serialization.DocumentSerializer)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)2 HashMap (java.util.HashMap)2 Tuple2 (com.yahoo.collections.Tuple2)1 ArrayDataType (com.yahoo.document.ArrayDataType)1 CollectionDataType (com.yahoo.document.CollectionDataType)1