Search in sources :

Example 11 with MapFieldValue

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

the class MapReader method fillMapFromObject.

@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) {
    JsonToken token = buffer.currentToken();
    int initNesting = buffer.nesting();
    expectObjectStart(token);
    token = buffer.next();
    DataType keyType = parent.getDataType().getKeyType();
    DataType valueType = parent.getDataType().getValueType();
    while (buffer.nesting() >= initNesting) {
        FieldValue key = readAtomic(buffer.currentName(), keyType);
        FieldValue value = readSingleValue(buffer, valueType);
        Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
        parent.put(key, value);
        token = buffer.next();
    }
    expectObjectEnd(token);
}
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)

Example 12 with MapFieldValue

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

the class JsonReaderTestCase method testOldMap.

@Test
public final void testOldMap() throws IOException {
    InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testmap::whee\"," + " \"fields\": { \"actualmap\": [" + " { \"key\": \"nalle\", \"value\": \"kalle\"}," + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}}"));
    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("actualmap"));
    assertSame(MapFieldValue.class, f.getClass());
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f;
    assertEquals(2, m.size());
    assertEquals(new StringFieldValue("kalle"), m.get(new StringFieldValue("nalle")));
    assertEquals(new StringFieldValue("skalle"), m.get(new StringFieldValue("tralle")));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) ByteArrayInputStream(java.io.ByteArrayInputStream) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) 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) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) Test(org.junit.Test)

Example 13 with MapFieldValue

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

the class JsonReaderTestCase method testOldMapStringToArrayOfInt.

@Test
public final void testOldMapStringToArrayOfInt() throws IOException {
    InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": [" + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}}"));
    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("actualMapStringToArrayOfInt");
    assertSame(MapFieldValue.class, f.getClass());
    MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f;
    Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
    assertEquals(3, a.size());
    assertEquals(new IntegerFieldValue(1), a.get(0));
    assertEquals(new IntegerFieldValue(2), a.get(1));
    assertEquals(new IntegerFieldValue(3), a.get(2));
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) Array(com.yahoo.document.datatypes.Array) 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) Test(org.junit.Test)

Example 14 with MapFieldValue

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

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

the class DocumentTestCase method getSertestDocument.

private Document getSertestDocument() {
    Document doc = new Document(docMan.getDocumentType("sertest"), new DocumentId("doc:sertest:foobar"));
    doc.setFieldValue("mailid", "emailfromalicetobob");
    // 03/13/06 11:00:00
    doc.setFieldValue("date", -2013512400);
    doc.setFieldValue("attachmentcount", 2);
    byte[] rawBytes = new byte[100];
    for (int i = 0; i < rawBytes.length; i++) {
        rawBytes[i] = (byte) i;
    }
    doc.setFieldValue("rawfield", new Raw(ByteBuffer.wrap(rawBytes)));
    Document docInDoc = new Document(docMan.getDocumentType("docindoc"), new DocumentId("doc:sertest:inserted"));
    docInDoc.setFieldValue("tull", "ball");
    doc.setFieldValue("docindoc", docInDoc);
    WeightedSet<StringFieldValue> wset = new WeightedSet<>(DataType.getWeightedSet(DataType.STRING));
    wset.put(new StringFieldValue("this is a test"), 5);
    wset.put(new StringFieldValue("this is another test, blah blah"), 10);
    doc.setFieldValue("weightedfield", wset);
    MapFieldValue<StringFieldValue, StringFieldValue> map = new MapFieldValue<>(new MapDataType(DataType.STRING, DataType.STRING));
    map.put(new StringFieldValue("foo1"), new StringFieldValue("bar1"));
    map.put(new StringFieldValue("foo2"), new StringFieldValue("bar2"));
    doc.setFieldValue("mapfield", map);
    return doc;
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Raw(com.yahoo.document.datatypes.Raw) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

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