Search in sources :

Example 6 with MapFieldValue

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

the class VespaDocumentSerializerTestCase method incompressable_structs_are_serialized_without_buffer_size_overhead_bug.

@Test
public void incompressable_structs_are_serialized_without_buffer_size_overhead_bug() {
    CompressionFixture fixture = new CompressionFixture();
    Document doc = new Document(fixture.docType, "id:foo:map_of_structs::flarn");
    Struct nested = new Struct(fixture.nestedType);
    nested.setFieldValue("str", new StringFieldValue(CompressionFixture.COMPRESSABLE_STRING));
    MapFieldValue<StringFieldValue, Struct> map = new MapFieldValue<StringFieldValue, Struct>(fixture.mapType);
    // Only 1 struct added. Not enough redundant information that header struct containing map itself
    // can be compressed.
    map.put(new StringFieldValue("foo"), nested);
    doc.setFieldValue("map", map);
    GrowableByteBuffer buf = CompressionFixture.asSerialized(doc);
    // Explanation of arbitrary value: buffer copy bug meant that incompressable structs were all serialized
    // rounded up to 4096 bytes.
    assertTrue(buf.remaining() < 4096);
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 7 with MapFieldValue

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

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

the class DocumentTestCase method testGenerateSerializedFile.

@Test
public void testGenerateSerializedFile() throws IOException {
    docMan = setUpCppDocType();
    Document doc = new Document(docMan.getDocumentType("serializetest"), new DocumentId("doc:serializetest:http://test.doc.id/"));
    Document docindoc = new Document(docMan.getDocumentType("docindoc"), new DocumentId("doc:serializetest:http://doc.in.doc/"));
    docindoc.setFieldValue("stringindocfield", "Elvis is dead");
    doc.setFieldValue("docfield", docindoc);
    Array<FloatFieldValue> l = new Array<>(doc.getField("arrayoffloatfield").getDataType());
    l.add(new FloatFieldValue((float) 1.0));
    l.add(new FloatFieldValue((float) 2.0));
    doc.setFieldValue("arrayoffloatfield", l);
    WeightedSet<StringFieldValue> wset = new WeightedSet<>(doc.getDataType().getField("wsfield").getDataType());
    wset.put(new StringFieldValue("Weighted 0"), 50);
    wset.put(new StringFieldValue("Weighted 1"), 199);
    doc.setFieldValue("wsfield", wset);
    MapFieldValue<StringFieldValue, StringFieldValue> map = new MapFieldValue<>((MapDataType) doc.getDataType().getField("mapfield").getDataType());
    map.put(new StringFieldValue("foo1"), new StringFieldValue("bar1"));
    map.put(new StringFieldValue("foo2"), new StringFieldValue("bar2"));
    doc.setFieldValue("mapfield", map);
    doc.setFieldValue("bytefield", new ByteFieldValue((byte) 254));
    doc.setFieldValue("rawfield", new Raw(ByteBuffer.wrap("RAW DATA".getBytes())));
    doc.setFieldValue("intfield", new IntegerFieldValue(5));
    doc.setFieldValue("floatfield", new FloatFieldValue(-9.23f));
    doc.setFieldValue("stringfield", new StringFieldValue("This is a string."));
    doc.setFieldValue("longfield", new LongFieldValue(398420092938472983L));
    doc.setFieldValue("doublefield", new DoubleFieldValue(98374532.398820d));
    doc.setFieldValue("urifield", new StringFieldValue("http://this.is.a.test/"));
    int size = doc.getSerializedSize();
    GrowableByteBuffer buf = new GrowableByteBuffer(size, 2.0f);
    doc.serialize(buf);
    assertEquals(size, buf.position());
    buf.position(0);
    FileOutputStream fos = new FileOutputStream("src/tests/data/serializejava.dat");
    fos.write(buf.array(), 0, size);
    fos.close();
    CompressionConfig noncomp = new CompressionConfig();
    CompressionConfig lz4comp = new CompressionConfig(CompressionType.LZ4);
    doc.getDataType().getHeaderType().setCompressionConfig(lz4comp);
    doc.getDataType().getBodyType().setCompressionConfig(lz4comp);
    buf = new GrowableByteBuffer(size, 2.0f);
    doc.serialize(buf);
    doc.getDataType().getHeaderType().setCompressionConfig(noncomp);
    doc.getDataType().getBodyType().setCompressionConfig(noncomp);
    fos = new FileOutputStream("src/tests/data/serializejava-compressed.dat");
    fos.write(buf.array(), 0, buf.position());
    fos.close();
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) Raw(com.yahoo.document.datatypes.Raw) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FileOutputStream(java.io.FileOutputStream) WeightedSet(com.yahoo.document.datatypes.WeightedSet) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Test(org.junit.Test)

Example 9 with MapFieldValue

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

the class DocumentTestCase method validateCppDoc.

public void validateCppDoc(Document doc) throws IOException {
    validateCppDocNotMap(doc);
    MapFieldValue map = (MapFieldValue) doc.getFieldValue("mapfield");
    assertEquals(map.get(new StringFieldValue("foo1")), new StringFieldValue("bar1"));
    assertEquals(map.get(new StringFieldValue("foo2")), new StringFieldValue("bar2"));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue)

Example 10 with MapFieldValue

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

the class MapReader method fillMapFromArray.

@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent) {
    JsonToken token = buffer.currentToken();
    int initNesting = buffer.nesting();
    expectArrayStart(token);
    token = buffer.next();
    DataType keyType = parent.getDataType().getKeyType();
    DataType valueType = parent.getDataType().getValueType();
    while (buffer.nesting() >= initNesting) {
        FieldValue key = null;
        FieldValue value = null;
        expectObjectStart(token);
        token = buffer.next();
        for (int i = 0; i < 2; ++i) {
            if (MAP_KEY.equals(buffer.currentName())) {
                key = readSingleValue(buffer, keyType);
            } else if (MAP_VALUE.equals(buffer.currentName())) {
                value = readSingleValue(buffer, valueType);
            }
            token = buffer.next();
        }
        Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
        parent.put(key, value);
        expectObjectEnd(token);
        // array end or next entry
        token = buffer.next();
    }
}
Also used : DataType(com.yahoo.document.DataType) ArrayDataType(com.yahoo.document.ArrayDataType) CollectionDataType(com.yahoo.document.CollectionDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) MapDataType(com.yahoo.document.MapDataType) JsonToken(com.fasterxml.jackson.core.JsonToken) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue)

Aggregations

MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)16 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)13 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 Test (org.junit.Test)11 FieldValue (com.yahoo.document.datatypes.FieldValue)8 Array (com.yahoo.document.datatypes.Array)7 Document (com.yahoo.document.Document)6 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)5 WeightedSet (com.yahoo.document.datatypes.WeightedSet)5 DocumentPut (com.yahoo.document.DocumentPut)4 DocumentType (com.yahoo.document.DocumentType)4 Struct (com.yahoo.document.datatypes.Struct)4 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)4 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 JsonToken (com.fasterxml.jackson.core.JsonToken)3 DataType (com.yahoo.document.DataType)3 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)3 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)3