Search in sources :

Example 11 with WeightedSet

use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.

the class JsonReaderTestCase method testAssignToWeightedSet.

@Test
public final void testAssignToWeightedSet() throws IOException {
    DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"assign\": {" + " \"person\": 37," + " \"another person\": 41}}}}");
    FieldUpdate x = doc.getFieldUpdate("actualset");
    assertEquals(1, x.size());
    AssignValueUpdate assign = (AssignValueUpdate) x.getValueUpdate(0);
    WeightedSet<?> w = (WeightedSet<?>) assign.getValue();
    assertEquals(2, w.size());
    assertEquals(new Integer(37), w.get(new StringFieldValue("person")));
    assertEquals(new Integer(41), w.get(new StringFieldValue("another person")));
}
Also used : DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test)

Example 12 with WeightedSet

use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.

the class ForEachExpression method doExecute.

@Override
protected void doExecute(final ExecutionContext ctx) {
    FieldValue input = ctx.getValue();
    if (input instanceof Array || input instanceof WeightedSet) {
        FieldValue next = new MyConverter(ctx, exp).convert(input);
        if (next == null) {
            VerificationContext vctx = new VerificationContext(ctx);
            vctx.setValue(input.getDataType()).execute(this);
            next = vctx.getValue().createFieldValue();
        }
        ctx.setValue(next);
    } else if (input instanceof Struct) {
        ctx.setValue(new MyConverter(ctx, exp).convert(input));
    } else {
        throw new IllegalArgumentException("Expected Array, Struct or WeightedSet input, got " + input.getDataType().getName() + ".");
    }
}
Also used : Array(com.yahoo.document.datatypes.Array) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Struct(com.yahoo.document.datatypes.Struct)

Example 13 with WeightedSet

use of com.yahoo.document.datatypes.WeightedSet 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 14 with WeightedSet

use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.

the class CompositeReader method populateComposite.

// TODO createComposite is extremely similar to add/remove, refactor
// yes, this suppresswarnings ugliness is by intention, the code relies on the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes" })
public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue) {
    JsonToken token = buffer.currentToken();
    if ((token != JsonToken.START_OBJECT) && (token != JsonToken.START_ARRAY)) {
        throw new IllegalArgumentException("Expected '[' or '{'. Got '" + token + "'.");
    }
    if (fieldValue instanceof CollectionFieldValue) {
        DataType valueType = ((CollectionFieldValue) fieldValue).getDataType().getNestedType();
        if (fieldValue instanceof WeightedSet) {
            fillWeightedSet(buffer, valueType, (WeightedSet) fieldValue);
        } else {
            fillArray(buffer, (CollectionFieldValue) fieldValue, valueType);
        }
    } else if (fieldValue instanceof MapFieldValue) {
        MapReader.fillMap(buffer, (MapFieldValue) fieldValue);
    } else if (fieldValue instanceof StructuredFieldValue) {
        StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue);
    } else if (fieldValue instanceof TensorFieldValue) {
        TensorReader.fillTensor(buffer, (TensorFieldValue) fieldValue);
    } else {
        throw new IllegalStateException("Has created a composite field" + " value the reader does not know how to handle: " + fieldValue.getClass().getName() + " This is a bug. token = " + token);
    }
    expectCompositeEnd(buffer.currentToken());
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) JsonToken(com.fasterxml.jackson.core.JsonToken) WeightedSet(com.yahoo.document.datatypes.WeightedSet) WeightedSetReader.fillWeightedSet(com.yahoo.document.json.readers.WeightedSetReader.fillWeightedSet)

Example 15 with WeightedSet

use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.

the class DocumentTestCase method validateCppDocNotMap.

@SuppressWarnings("unchecked")
public void validateCppDocNotMap(Document doc) throws IOException {
    // in practice to validate v6 serialization
    assertEquals("doc:serializetest:http://test.doc.id/", doc.getId().toString());
    assertEquals(new IntegerFieldValue(5), doc.getFieldValue("intfield"));
    assertEquals(new FloatFieldValue((float) -9.23), doc.getFieldValue("floatfield"));
    assertEquals(new StringFieldValue("This is a string."), doc.getFieldValue("stringfield"));
    assertEquals(new LongFieldValue(398420092938472983L), doc.getFieldValue("longfield"));
    assertEquals(new DoubleFieldValue(98374532.398820d), doc.getFieldValue("doublefield"));
    assertEquals(new StringFieldValue("http://this.is.a.test/"), doc.getFieldValue("urifield"));
    // NOTE: The value really is unsigned 254, which becomes signed -2:
    assertEquals(new ByteFieldValue(-2), doc.getFieldValue("bytefield"));
    ByteBuffer raw = ByteBuffer.wrap("RAW DATA".getBytes());
    assertEquals(new Raw(raw), doc.getFieldValue("rawfield"));
    Document docindoc = (Document) doc.getFieldValue("docfield");
    assertEquals(docMan.getDocumentType("docindoc"), docindoc.getDataType());
    assertEquals(new DocumentId("doc:docindoc:http://embedded"), docindoc.getId());
    Array<FloatFieldValue> array = (Array<FloatFieldValue>) doc.getFieldValue("arrayoffloatfield");
    assertEquals(new FloatFieldValue(1.0f), array.get(0));
    assertEquals(new FloatFieldValue(2.0f), array.get(1));
    WeightedSet<StringFieldValue> wset = (WeightedSet<StringFieldValue>) doc.getFieldValue("wsfield");
    assertEquals(new Integer(50), wset.get(new StringFieldValue("Weighted 0")));
    assertEquals(new Integer(199), wset.get(new StringFieldValue("Weighted 1")));
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Raw(com.yahoo.document.datatypes.Raw) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

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