use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class JsonReaderTestCase method testStructUpdate.
@Test
public final void testStructUpdate() throws IOException {
DocumentUpdate put = parseUpdate("{\"update\": \"id:unittest:mirrors:g=test:whee\"," + "\"create\": true," + " \"fields\": { " + "\"skuggsjaa\": {" + "\"assign\": { \"sandra\": \"person\"," + " \"cloud\": \"another person\"}}}}");
assertEquals(1, put.getFieldUpdates().size());
FieldUpdate fu = put.getFieldUpdate(0);
assertEquals(1, fu.getValueUpdates().size());
ValueUpdate vu = fu.getValueUpdate(0);
assertTrue(vu instanceof AssignValueUpdate);
AssignValueUpdate avu = (AssignValueUpdate) vu;
assertTrue(avu.getValue() instanceof Struct);
Struct s = (Struct) avu.getValue();
assertEquals(2, s.getFieldCount());
assertEquals(new StringFieldValue("person"), s.getFieldValue(s.getField("sandra")));
GrowableByteBuffer buf = new GrowableByteBuffer();
DocumentSerializer serializer = DocumentSerializerFactory.createHead(buf);
put.serialize(serializer);
assertEquals(107, buf.position());
}
use of com.yahoo.document.datatypes.Struct 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.Struct in project vespa by vespa-engine.
the class JsonReaderTestCase method testEmptyStructUpdate.
@Test
public final void testEmptyStructUpdate() throws IOException {
DocumentUpdate put = parseUpdate("{\"update\": \"id:unittest:mirrors:g=test:whee\"," + "\"create\": true," + " \"fields\": { " + "\"skuggsjaa\": {" + "\"assign\": { }}}}");
assertEquals(1, put.getFieldUpdates().size());
FieldUpdate fu = put.getFieldUpdate(0);
assertEquals(1, fu.getValueUpdates().size());
ValueUpdate vu = fu.getValueUpdate(0);
assertTrue(vu instanceof AssignValueUpdate);
AssignValueUpdate avu = (AssignValueUpdate) vu;
assertTrue(avu.getValue() instanceof Struct);
Struct s = (Struct) avu.getValue();
assertEquals(0, s.getFieldCount());
GrowableByteBuffer buf = new GrowableByteBuffer();
DocumentSerializer serializer = DocumentSerializerFactory.createHead(buf);
put.serialize(serializer);
assertEquals(69, buf.position());
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SystemTestCase method annotate.
private void annotate(Document document) {
AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
AnnotationType personType = registry.getType("person");
AnnotationType artistType = registry.getType("artist");
AnnotationType dateType = registry.getType("date");
AnnotationType placeType = registry.getType("place");
AnnotationType eventType = registry.getType("event");
SpanList root = new SpanList();
SpanTree tree = new SpanTree("meaningoflife", root);
SpanNode personSpan = new Span(0, 5);
SpanNode artistSpan = new Span(5, 10);
SpanNode dateSpan = new Span(10, 15);
SpanNode placeSpan = new Span(15, 20);
root.add(personSpan);
root.add(artistSpan);
root.add(dateSpan);
root.add(placeSpan);
Struct personValue = new Struct(manager.getDataType("annotation.person"));
personValue.setFieldValue("name", "george washington");
Annotation person = new Annotation(personType, personValue);
tree.annotate(personSpan, person);
Struct artistValue = new Struct(manager.getDataType("annotation.artist"));
artistValue.setFieldValue("name", "elvis presley");
artistValue.setFieldValue("instrument", 20);
Annotation artist = new Annotation(artistType, artistValue);
tree.annotate(artistSpan, artist);
Struct dateValue = new Struct(manager.getDataType("annotation.date"));
dateValue.setFieldValue("exacttime", 123456789L);
Annotation date = new Annotation(dateType, dateValue);
tree.annotate(dateSpan, date);
Struct placeValue = new Struct(manager.getDataType("annotation.place"));
placeValue.setFieldValue("lat", 1467L);
placeValue.setFieldValue("lon", 789L);
Annotation place = new Annotation(placeType, placeValue);
tree.annotate(placeSpan, place);
Struct eventValue = new Struct(manager.getDataType("annotation.event"));
eventValue.setFieldValue("description", "Big concert");
eventValue.setFieldValue("person", new AnnotationReference((AnnotationReferenceDataType) manager.getDataType("annotationreference<person>"), person));
eventValue.setFieldValue("date", new AnnotationReference((AnnotationReferenceDataType) manager.getDataType("annotationreference<date>"), date));
eventValue.setFieldValue("place", new AnnotationReference((AnnotationReferenceDataType) manager.getDataType("annotationreference<place>"), place));
Annotation event = new Annotation(eventType, eventValue);
tree.annotate(root, event);
StringFieldValue content = new StringFieldValue("This is the story of a big concert by Elvis and a special guest appearance by George Washington");
content.setSpanTree(tree);
document.setFieldValue(document.getDataType().getField("content"), content);
}
use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.
the class SystemTestCase method consume.
private void consume(Document document) {
StringFieldValue content = (StringFieldValue) document.getFieldValue(document.getDataType().getField("content"));
SpanTree tree = content.getSpanTree("meaningoflife");
SpanList root = (SpanList) tree.getRoot();
Iterator<SpanNode> childIterator = root.childIterator();
SpanNode personSpan = childIterator.next();
SpanNode artistSpan = childIterator.next();
SpanNode dateSpan = childIterator.next();
SpanNode placeSpan = childIterator.next();
Annotation person = tree.iterator(personSpan).next();
Struct personValue = (Struct) person.getFieldValue();
System.err.println("Person is " + personValue.getField("name"));
Annotation artist = tree.iterator(artistSpan).next();
Struct artistValue = (Struct) artist.getFieldValue();
System.err.println("Artist is " + artistValue.getFieldValue("name") + " who plays the " + artistValue.getFieldValue("instrument"));
Annotation date = tree.iterator(dateSpan).next();
Struct dateValue = (Struct) date.getFieldValue();
System.err.println("Date is " + dateValue.getFieldValue("exacttime"));
Annotation place = tree.iterator(placeSpan).next();
Struct placeValue = (Struct) place.getFieldValue();
System.err.println("Place is " + placeValue.getFieldValue("lat") + ";" + placeValue.getFieldValue("lon"));
Annotation event = tree.iterator(root).next();
Struct eventValue = (Struct) event.getFieldValue();
System.err.println("Event is " + eventValue.getFieldValue("description") + " with " + eventValue.getFieldValue("person") + " and " + eventValue.getFieldValue("date") + " and " + eventValue.getFieldValue("place"));
}
Aggregations