Search in sources :

Example 21 with Struct

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());
}
Also used : AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentSerializer(com.yahoo.document.serialization.DocumentSerializer) FieldUpdate(com.yahoo.document.update.FieldUpdate) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 22 with Struct

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 23 with Struct

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());
}
Also used : AddValueUpdate(com.yahoo.document.update.AddValueUpdate) MapValueUpdate(com.yahoo.document.update.MapValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) ArithmeticValueUpdate(com.yahoo.document.update.ArithmeticValueUpdate) ClearValueUpdate(com.yahoo.document.update.ClearValueUpdate) DocumentUpdate(com.yahoo.document.DocumentUpdate) DocumentSerializer(com.yahoo.document.serialization.DocumentSerializer) FieldUpdate(com.yahoo.document.update.FieldUpdate) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) AssignValueUpdate(com.yahoo.document.update.AssignValueUpdate) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 24 with Struct

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);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Struct(com.yahoo.document.datatypes.Struct)

Example 25 with Struct

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"));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Struct(com.yahoo.document.datatypes.Struct)

Aggregations

Struct (com.yahoo.document.datatypes.Struct)38 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)25 Test (org.junit.Test)21 FieldValue (com.yahoo.document.datatypes.FieldValue)13 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 StructDataType (com.yahoo.document.StructDataType)9 Array (com.yahoo.document.datatypes.Array)9 Field (com.yahoo.document.Field)7 Document (com.yahoo.document.Document)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 DocumentType (com.yahoo.document.DocumentType)3 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)3 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)3 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)3 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)3 FieldUpdate (com.yahoo.document.update.FieldUpdate)3 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)3 ValueUpdate (com.yahoo.document.update.ValueUpdate)3