Search in sources :

Example 6 with WeightedSet

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;
}
Also used : Array(com.yahoo.document.datatypes.Array) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 7 with WeightedSet

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");
}
Also used : Array(com.yahoo.document.datatypes.Array) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 8 with WeightedSet

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");
}
Also used : Array(com.yahoo.document.datatypes.Array) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 9 with WeightedSet

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;
}
Also used : CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 10 with WeightedSet

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")));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) ByteArrayInputStream(java.io.ByteArrayInputStream) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test)

Aggregations

WeightedSet (com.yahoo.document.datatypes.WeightedSet)18 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)11 FieldValue (com.yahoo.document.datatypes.FieldValue)10 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)10 Array (com.yahoo.document.datatypes.Array)9 Test (org.junit.Test)7 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)6 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)6 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)6 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)6 Raw (com.yahoo.document.datatypes.Raw)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 DataType (com.yahoo.document.DataType)3 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)3 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)3 Struct (com.yahoo.document.datatypes.Struct)3 ByteBuffer (java.nio.ByteBuffer)3 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)2 FieldUpdate (com.yahoo.document.update.FieldUpdate)2