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");
}
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);
}
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);
}
}
Aggregations