use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class JsonReaderTestCase method testStruct.
@Test
public final void testStruct() throws IOException {
InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:mirrors::whee\"," + " \"fields\": { " + "\"skuggsjaa\": {" + "\"sandra\": \"person\"," + " \"cloud\": \"another person\"}}}"));
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("skuggsjaa"));
assertSame(Struct.class, f.getClass());
Struct s = (Struct) f;
assertEquals("person", ((StringFieldValue) s.getFieldValue("sandra")).getString());
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class JsonReaderTestCase method testPositionNegative.
@Test
public final void testPositionNegative() throws IOException {
InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testsinglepos::bamf\"," + " \"fields\": { \"singlepos\": \"W46.63;S23.55\" }}"));
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("singlepos"));
assertSame(Struct.class, f.getClass());
assertEquals(-46630000, PositionDataType.getXValue(f).getInteger());
assertEquals(-23550000, PositionDataType.getYValue(f).getInteger());
}
use of com.yahoo.document.datatypes.FieldValue 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.FieldValue 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.FieldValue in project vespa by vespa-engine.
the class SerializeAnnotationsTestCase method testSerializeAdvancedTree.
public void testSerializeAdvancedTree() throws IOException {
SpanList root = new SpanList();
SpanTree tree = new SpanTree("html", root);
DataType positionType = docMan.getDataType("myposition");
StructDataType cityDataType = (StructDataType) docMan.getDataType("annotation.city");
AnnotationTypeRegistry registry = docMan.getAnnotationTypeRegistry();
AnnotationType textType = registry.getType("text");
AnnotationType beginTag = registry.getType("begintag");
AnnotationType endTag = registry.getType("endtag");
AnnotationType bodyType = registry.getType("body");
AnnotationType paragraphType = registry.getType("paragraph");
AnnotationType cityType = registry.getType("city");
AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) docMan.getDataType("annotationreference<text>");
Struct position = new Struct(positionType);
position.setFieldValue("latitude", new DoubleFieldValue(37.774929));
position.setFieldValue("longitude", new DoubleFieldValue(-122.419415));
Annotation sanAnnotation = new Annotation(textType);
Annotation franciscoAnnotation = new Annotation(textType);
Struct positionWithRef = cityDataType.createFieldValue();
positionWithRef.setFieldValue("position", position);
Field referencesField = cityDataType.getField("references");
Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
refList.add(new AnnotationReference(annRefType, sanAnnotation));
refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
positionWithRef.setFieldValue(referencesField, refList);
Annotation city = new Annotation(cityType, positionWithRef);
AlternateSpanList paragraph = new AlternateSpanList();
paragraph.addChildren(new ArrayList<SpanNode>(), 0);
paragraph.setProbability(0, 0.9);
paragraph.setProbability(1, 0.1);
{
Span span1 = new Span(6, 3);
Span span2 = new Span(9, 10);
Span span3 = new Span(19, 4);
Span span4 = new Span(23, 4);
paragraph.add(0, span1).add(0, span2).add(0, span3).add(0, span4);
Span alt_span1 = new Span(6, 13);
Span alt_span2 = new Span(19, 8);
paragraph.add(1, alt_span1).add(1, alt_span2);
tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(alt_span1, textType).annotate(alt_span2, bodyType).annotate(paragraph, paragraphType);
}
{
Span span1 = new Span(0, 6);
Span span2 = new Span(27, 9);
Span span3 = new Span(36, 8);
root.add(span1).add(paragraph).add(span2).add(span3);
tree.annotate(span1, beginTag).annotate(span2, franciscoAnnotation).annotate(span3, endTag).annotate(root, bodyType).annotate(city);
}
StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj " + "lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj l jlkj lkj lkj " + "lkjoijoij oij oij oij oij oij oijoijoij oij oij oij oij oij " + "oijoijoijoijoijoijoijoijoijoijoijoijoij oij oij oij oij " + "oijaosdifjoai fdoais jdoasi jai os oafoai ai dfojsfoa dfoi dsf" + "aosifjasofija sodfij oasdifj aosdiosifjsi ooai oais osi");
value.setSpanTree(tree);
// important! call readFile() before writeFile()!
ByteBuffer serializedFromFile = readFile("test_data_serialized_advanced");
ByteBuffer serialized = writeFile(value, "test_data_serialized_advanced");
assertEquals(serialized.limit(), serializedFromFile.limit());
StringFieldValue valueFromFile = new StringFieldValue();
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
deserializer.read(null, valueFromFile);
assertEquals(value, valueFromFile);
}
Aggregations