Search in sources :

Example 11 with MapDataType

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

the class MapTestCase method testStringMap.

public void testStringMap() {
    MapDataType mapType = new MapDataType(DataType.STRING, DataType.STRING);
    MapFieldValue<StringFieldValue, StringFieldValue> map = mapType.createFieldValue();
    StringFieldValue sfvk1 = new StringFieldValue("k1");
    StringFieldValue sfvk2 = new StringFieldValue("k2");
    StringFieldValue sfvk3 = new StringFieldValue("k3");
    StringFieldValue sfvv1 = new StringFieldValue("v1");
    StringFieldValue sfvv2 = new StringFieldValue("v2");
    StringFieldValue sfvv3 = new StringFieldValue("v3");
    map.put(sfvk1, sfvv1);
    map.put(sfvk2, sfvv2);
    map.put(sfvk3, sfvv3);
    assertEquals(map.get(sfvk1), sfvv1);
    assertEquals(map.get(sfvk2), sfvv2);
    assertEquals(map.get(sfvk3), sfvv3);
    assertEquals(map.get(new StringFieldValue("k1")).getWrappedValue(), "v1");
    assertEquals(map.get(new StringFieldValue("k2")).getWrappedValue(), "v2");
    assertEquals(map.get(new StringFieldValue("k3")).getWrappedValue(), "v3");
}
Also used : MapDataType(com.yahoo.document.MapDataType)

Example 12 with MapDataType

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

the class VespaDocumentDeserializer42 method read.

public <K extends FieldValue, V extends FieldValue> void read(FieldBase field, MapFieldValue<K, V> map) {
    int numElements = getNumCollectionElems();
    Map<K, V> hash = new HashMap<>();
    MapDataType type = map.getDataType();
    for (int i = 0; i < numElements; i++) {
        if (version < 7) {
            // We don't need size for anything
            getInt(null);
        }
        K key = (K) type.getKeyType().createFieldValue();
        V val = (V) type.getValueType().createFieldValue();
        key.deserialize(null, this);
        val.deserialize(null, this);
        hash.put(key, val);
    }
    map.clear();
    map.putAll(hash);
}
Also used : HashMap(java.util.HashMap) MapDataType(com.yahoo.document.MapDataType)

Example 13 with MapDataType

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

the class VespaXMLFieldReader method read.

public <K extends FieldValue, V extends FieldValue> void read(FieldBase field, MapFieldValue<K, V> map) {
    try {
        MapDataType dt = map.getDataType();
        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                if ("item".equals(reader.getName().toString())) {
                    KeyAndValue kv = new KeyAndValue();
                    readKeyAndValue(field, kv, dt);
                    if (kv.key == null || kv.value == null) {
                        throw newDeserializeException(field, "Map items must specify both key and value");
                    }
                    // noinspection unchecked
                    map.put((K) kv.key, (V) kv.value);
                    skipToEnd("item");
                } else {
                    throw newDeserializeException(field, "Illegal tag " + reader.getName() + " expected 'item'");
                }
            } else if (type == XMLStreamReader.END_ELEMENT) {
                return;
            }
        }
    } catch (XMLStreamException e) {
        throw newException(field, e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) MapDataType(com.yahoo.document.MapDataType)

Aggregations

MapDataType (com.yahoo.document.MapDataType)13 StructDataType (com.yahoo.document.StructDataType)7 ArrayDataType (com.yahoo.document.ArrayDataType)5 DataType (com.yahoo.document.DataType)5 Field (com.yahoo.document.Field)5 DocumentType (com.yahoo.document.DocumentType)4 ReferenceDataType (com.yahoo.document.ReferenceDataType)4 CollectionDataType (com.yahoo.document.CollectionDataType)3 PositionDataType (com.yahoo.document.PositionDataType)3 TensorDataType (com.yahoo.document.TensorDataType)3 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)3 TemporaryAnnotationReferenceDataType (com.yahoo.searchdefinition.document.annotation.TemporaryAnnotationReferenceDataType)3 StructuredDataType (com.yahoo.document.StructuredDataType)2 TemporaryStructuredDataType (com.yahoo.document.TemporaryStructuredDataType)2 AnnotationReferenceDataType (com.yahoo.document.annotation.AnnotationReferenceDataType)2 NewDocumentType (com.yahoo.documentmodel.NewDocumentType)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)1 AnnotationType (com.yahoo.document.annotation.AnnotationType)1 VespaDocumentType (com.yahoo.documentmodel.VespaDocumentType)1