Search in sources :

Example 36 with FieldValue

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

the class JsonReaderTestCase method smokeTestDoc.

private void smokeTestDoc(Document doc) {
    FieldValue f = doc.getFieldValue(doc.getField("nalle"));
    assertSame(StringFieldValue.class, f.getClass());
    StringFieldValue s = (StringFieldValue) f;
    assertEquals("bamse", s.getString());
}
Also used : 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)

Example 37 with FieldValue

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

the class DocumentTestCase method testTypeChecking.

@Test
public void testTypeChecking() {
    DocumentType type = new DocumentType("test");
    type.addField(new Field("double", DataType.DOUBLE));
    type.addField(new Field("float", DataType.FLOAT));
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("long", DataType.LONG));
    type.addField(new Field("string", DataType.STRING));
    Document doc = new Document(type, "doc:scheme:");
    FieldValue stringVal = new StringFieldValue("69");
    FieldValue doubleVal = new DoubleFieldValue(6.9);
    FieldValue floatVal = new FloatFieldValue(6.9f);
    FieldValue intVal = new IntegerFieldValue(69);
    FieldValue longVal = new LongFieldValue(69L);
    doc.setFieldValue("string", stringVal);
    doc.setFieldValue("string", doubleVal);
    doc.setFieldValue("string", floatVal);
    doc.setFieldValue("string", intVal);
    doc.setFieldValue("string", longVal);
    doc.setFieldValue("double", stringVal);
    doc.setFieldValue("double", doubleVal);
    doc.setFieldValue("double", floatVal);
    doc.setFieldValue("double", intVal);
    doc.setFieldValue("double", longVal);
    doc.setFieldValue("float", stringVal);
    doc.setFieldValue("float", doubleVal);
    doc.setFieldValue("float", floatVal);
    doc.setFieldValue("float", intVal);
    doc.setFieldValue("float", longVal);
    doc.setFieldValue("int", stringVal);
    doc.setFieldValue("int", doubleVal);
    doc.setFieldValue("int", floatVal);
    doc.setFieldValue("int", intVal);
    doc.setFieldValue("int", longVal);
    doc.setFieldValue("long", stringVal);
    doc.setFieldValue("long", doubleVal);
    doc.setFieldValue("long", floatVal);
    doc.setFieldValue("long", intVal);
    doc.setFieldValue("long", longVal);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Test(org.junit.Test)

Example 38 with FieldValue

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

the class DocumentTestCase method testGetRecursiveValue.

@Test
public void testGetRecursiveValue() {
    Document doc = new Document(testDocType, new DocumentId("doc:ns:testdoc"));
    doc.setFieldValue("primitive1", 1);
    Struct l1s1 = new Struct(doc.getField("l1s1").getDataType());
    l1s1.setFieldValue("primitive1", 2);
    Struct l2s1 = new Struct(doc.getField("struct2").getDataType());
    l2s1.setFieldValue("primitive1", 3);
    l2s1.setFieldValue("primitive2", 4);
    Array<IntegerFieldValue> iarr1 = new Array<>(l2s1.getField("iarray").getDataType());
    iarr1.add(new IntegerFieldValue(11));
    iarr1.add(new IntegerFieldValue(12));
    iarr1.add(new IntegerFieldValue(13));
    l2s1.setFieldValue("iarray", iarr1);
    ArrayDataType dt = (ArrayDataType) l2s1.getField("sarray").getDataType();
    Array<Struct> sarr1 = new Array<>(dt);
    {
        Struct l3s1 = new Struct(dt.getNestedType());
        l3s1.setFieldValue("primitive1", 1);
        l3s1.setFieldValue("primitive2", 2);
        sarr1.add(l3s1);
    }
    {
        Struct l3s1 = new Struct(dt.getNestedType());
        l3s1.setFieldValue("primitive1", 1);
        l3s1.setFieldValue("primitive2", 2);
        sarr1.add(l3s1);
    }
    l2s1.setFieldValue("sarray", sarr1);
    MapFieldValue<StringFieldValue, StringFieldValue> smap1 = new MapFieldValue<>((MapDataType) l2s1.getField("smap").getDataType());
    smap1.put(new StringFieldValue("leonardo"), new StringFieldValue("dicaprio"));
    smap1.put(new StringFieldValue("ellen"), new StringFieldValue("page"));
    smap1.put(new StringFieldValue("joseph"), new StringFieldValue("gordon-levitt"));
    l2s1.setFieldValue("smap", smap1);
    l1s1.setFieldValue("ss", l2s1.clone());
    MapFieldValue<StringFieldValue, Struct> structmap1 = new MapFieldValue<>((MapDataType) l1s1.getField("structmap").getDataType());
    structmap1.put(new StringFieldValue("test"), l2s1.clone());
    l1s1.setFieldValue("structmap", structmap1);
    WeightedSet<StringFieldValue> wset1 = new WeightedSet<>(l1s1.getField("wset").getDataType());
    wset1.add(new StringFieldValue("foo"));
    wset1.add(new StringFieldValue("bar"));
    wset1.add(new StringFieldValue("zoo"));
    l1s1.setFieldValue("wset", wset1);
    Struct l2s2 = new Struct(doc.getField("struct2").getDataType());
    l2s2.setFieldValue("primitive1", 5);
    l2s2.setFieldValue("primitive2", 6);
    WeightedSet<Struct> wset2 = new WeightedSet<>(l1s1.getField("structwset").getDataType());
    wset2.add(l2s1.clone());
    wset2.add(l2s2.clone());
    l1s1.setFieldValue("structwset", wset2);
    doc.setFieldValue("l1s1", l1s1.clone());
    {
        FieldValue fv = doc.getRecursiveValue("l1s1");
        assertEquals(l1s1, fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.primitive1");
        assertEquals(new IntegerFieldValue(2), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss");
        assertEquals(l2s1, fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
        assertEquals(iarr1, fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray[2]");
        assertEquals(new IntegerFieldValue(13), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray[3]");
        assertNull(fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.sarray[0].primitive1");
        assertEquals(new IntegerFieldValue(1), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.smap{joseph}");
        assertEquals(new StringFieldValue("gordon-levitt"), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.ss.smap.key");
        assertEquals(3, ((Array) fv).size());
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.structmap{test}.primitive1");
        assertEquals(new IntegerFieldValue(3), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.primitive1");
        assertEquals(new IntegerFieldValue(3), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.wset{foo}");
        assertEquals(new IntegerFieldValue(1), fv);
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.wset.key");
        assertEquals(3, ((Array) fv).size());
    }
    {
        FieldValue fv = doc.getRecursiveValue("l1s1.structwset.key.primitive1");
        assertEquals(DataType.INT, (((ArrayDataType) fv.getDataType()).getNestedType()));
        assertEquals(2, ((Array) fv).size());
    }
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test)

Example 39 with FieldValue

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

the class ValueUpdateTestCase method testUpdateSimple.

public void testUpdateSimple() {
    /**
     * We cannot test much on this level anyway, most stuff in ValueUpdate is package
     * private. Better tests exist in FieldUpdateTestCase.
     */
    AssignValueUpdate upd = (AssignValueUpdate) ValueUpdate.createAssign(new StringFieldValue("newvalue"));
    assertEquals(ValueUpdate.ValueUpdateClassID.ASSIGN, upd.getValueUpdateClassID());
    FieldValue newValue = upd.getValue();
    assertEquals(new StringFieldValue("newvalue"), newValue);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 40 with FieldValue

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

the class VespaXmlFieldReaderTestCase method assertRead.

private static void assertRead(Predicate expected, String documentXml) throws Exception {
    DocumentTypeManager docManager = new DocumentTypeManager();
    DocumentType docType = new DocumentType("my_type");
    docType.addField("my_predicate", DataType.PREDICATE);
    docManager.register(docType);
    InputStream in = new ByteArrayInputStream(documentXml.getBytes(StandardCharsets.UTF_8));
    Document doc = new Document(docType, "doc:scheme:");
    new VespaXMLFieldReader(in, docManager).read(null, doc);
    FieldValue value = doc.getFieldValue("my_predicate");
    assertTrue(value instanceof PredicateFieldValue);
    assertEquals(expected, ((PredicateFieldValue) value).getPredicate());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FieldValue(com.yahoo.document.datatypes.FieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue)

Aggregations

FieldValue (com.yahoo.document.datatypes.FieldValue)106 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)69 Test (org.junit.Test)52 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)45 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)30 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)26 Array (com.yahoo.document.datatypes.Array)20 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)20 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)18 StructuredFieldValue (com.yahoo.document.datatypes.StructuredFieldValue)14 Struct (com.yahoo.document.datatypes.Struct)13 DataType (com.yahoo.document.DataType)12 Document (com.yahoo.document.Document)12 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)12 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)12 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)11 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 DocumentPut (com.yahoo.document.DocumentPut)10