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