Search in sources :

Example 31 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class AddRemoveCreator method createAddsOrRemoves.

// yes, this suppresswarnings ugliness is by intention, the code relies on
// the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) {
    FieldValue container = field.getDataType().createFieldValue();
    FieldUpdate singleUpdate;
    int initNesting = buffer.nesting();
    Preconditions.checkState(buffer.currentToken().isStructStart(), "Expected start of composite, got %s", buffer.currentToken());
    if (container instanceof CollectionFieldValue) {
        buffer.next();
        DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType();
        if (container instanceof WeightedSet) {
            // these are objects with string keys (which are the nested
            // types) and values which are the weight
            WeightedSet weightedSet = (WeightedSet) container;
            fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet);
            if (isRemove) {
                singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet);
            } else {
                singleUpdate = FieldUpdate.createAddAll(field, weightedSet);
            }
        } else {
            List<FieldValue> arrayContents = new ArrayList<>();
            ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents);
            if (buffer.currentToken() != JsonToken.END_ARRAY) {
                throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'.");
            }
            if (isRemove) {
                singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents);
            } else {
                singleUpdate = FieldUpdate.createAddAll(field, arrayContents);
            }
        }
    } else {
        throw new UnsupportedOperationException("Trying to add or remove from a field of a type the reader does not know how to handle: " + container.getClass().getName());
    }
    expectCompositeEnd(buffer.currentToken());
    update.addAll(singleUpdate);
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) ArrayList(java.util.ArrayList) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 32 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate 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 33 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class DocumentUpdateTestCase method testAddAll.

public void testAddAll() {
    DocumentType t1 = new DocumentType("doo");
    DocumentType t2 = new DocumentType("foo");
    Field f1 = new Field("field1", DataType.STRING);
    Field f2 = new Field("field2", DataType.STRING);
    t1.addField(f1);
    t2.addField(f2);
    DocumentUpdate du1 = new DocumentUpdate(t1, new DocumentId("doc:this:is:a:test"));
    DocumentUpdate du2 = new DocumentUpdate(t2, "doc:this:is:another:test");
    FieldUpdate fu1 = FieldUpdate.createAssign(f1, new StringFieldValue("banana"));
    FieldUpdate fu2 = FieldUpdate.createAssign(f2, new StringFieldValue("apple"));
    du1.addFieldUpdate(fu1);
    du2.addFieldUpdate(fu2);
    du1.addAll(null);
    try {
        du1.addAll(du2);
        fail("Should have gotten exception");
    } catch (IllegalArgumentException iae) {
    // ok!
    }
    DocumentUpdate du3 = new DocumentUpdate(t2, new DocumentId("doc:this:is:a:test"));
    try {
        du1.addAll(du3);
        fail("Should have gotten exception");
    } catch (IllegalArgumentException iae) {
    // ok!
    }
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 34 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class DocumentUpdateTestCase method testFieldUpdatesInDocUp.

public void testFieldUpdatesInDocUp() throws ParseException {
    DocumentType t1 = new DocumentType("doo");
    Field f1 = new Field("field1", DataType.STRING);
    Field f2 = new Field("field2", DataType.STRING);
    t1.addField(f1);
    t1.addField(f2);
    DocumentUpdate documentUpdate = new DocumentUpdate(t1, new DocumentId("doc:this:is:a:test"));
    assertEquals(0, documentUpdate.size());
    FieldUpdate fu1 = FieldUpdate.createAssign(f1, new StringFieldValue("banana"));
    FieldUpdate fu2 = FieldUpdate.createAssign(f2, new StringFieldValue("apple"));
    documentUpdate.addFieldUpdate(fu1);
    assertEquals(1, documentUpdate.size());
    documentUpdate.clearFieldUpdates();
    assertEquals(0, documentUpdate.size());
    documentUpdate.addFieldUpdate(fu1);
    documentUpdate.addFieldUpdate(fu2);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(f1));
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    assertSame(fu2, documentUpdate.getFieldUpdate(1));
    documentUpdate.setFieldUpdate(0, fu2);
    documentUpdate.setFieldUpdate(1, fu1);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(1));
    assertSame(fu2, documentUpdate.getFieldUpdate(0));
    try {
        documentUpdate.setFieldUpdates(null);
        fail("Should have gotten NullPointerException");
    } catch (NullPointerException npe) {
    // ok!
    }
    List<FieldUpdate> fus = new ArrayList<>();
    fus.add(fu1);
    fus.add(fu2);
    documentUpdate.setFieldUpdates(fus);
    assertEquals(2, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    assertSame(fu2, documentUpdate.getFieldUpdate(1));
    documentUpdate.removeFieldUpdate(1);
    assertEquals(1, documentUpdate.size());
    assertSame(fu1, documentUpdate.getFieldUpdate(0));
    documentUpdate.toString();
    assertFalse(documentUpdate.isEmpty());
    Iterator<FieldPathUpdate> fpUpdates = documentUpdate.iterator();
    assertFalse(fpUpdates.hasNext());
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) ArrayList(java.util.ArrayList) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate)

Example 35 with FieldUpdate

use of com.yahoo.document.update.FieldUpdate in project vespa by vespa-engine.

the class VespaXMLUpdateReader method readAlter.

FieldUpdate readAlter(DocumentUpdate update) throws XMLStreamException {
    Field f = null;
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        if ("field".equals(reader.getAttributeName(i).toString())) {
            f = update.getDocumentType().getField(reader.getAttributeValue(i));
        }
    }
    if (f == null) {
        throw newDeserializeException("Alter update without \"field\" attribute");
    }
    FieldUpdate fu = FieldUpdate.create(f);
    while (reader.hasNext()) {
        int type = reader.next();
        if (type == XMLStreamReader.START_ELEMENT) {
            if ("increment".equals(reader.getName().toString()) || "decrement".equals(reader.getName().toString()) || "multiply".equals(reader.getName().toString()) || "divide".equals(reader.getName().toString())) {
                update.addFieldUpdate(readArithmetic(update, reader.getName().toString(), f, fu));
                skipToEnd(reader.getName().toString());
            } else {
                throw newDeserializeException("Element \"" + reader.getName() + "\" not appropriate within alter element");
            }
        } else if (type == XMLStreamReader.END_ELEMENT) {
            break;
        }
    }
    return fu;
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate)

Aggregations

FieldUpdate (com.yahoo.document.update.FieldUpdate)37 Test (org.junit.Test)17 DocumentUpdate (com.yahoo.document.DocumentUpdate)16 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)15 AssignValueUpdate (com.yahoo.document.update.AssignValueUpdate)14 ValueUpdate (com.yahoo.document.update.ValueUpdate)12 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)8 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)5 ArithmeticValueUpdate (com.yahoo.document.update.ArithmeticValueUpdate)5 ClearValueUpdate (com.yahoo.document.update.ClearValueUpdate)5 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)5 HashMap (java.util.HashMap)5 DocumentId (com.yahoo.document.DocumentId)4 DocumentType (com.yahoo.document.DocumentType)4 FieldPathUpdate (com.yahoo.document.fieldpathupdate.FieldPathUpdate)4 ArrayList (java.util.ArrayList)4 Field (com.yahoo.document.Field)3 Array (com.yahoo.document.datatypes.Array)3 Struct (com.yahoo.document.datatypes.Struct)3 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)3