Search in sources :

Example 1 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class DocumentToPathUpdateTestCase method requireThatStructAssignIsConverted.

@Test
public void requireThatStructAssignIsConverted() {
    DocumentType docType = new DocumentType("my_type");
    StructDataType structType = new StructDataType("my_struct");
    structType.addField(new Field("b", DataType.INT));
    docType.addField(new Field("a", structType));
    Struct struct = structType.createFieldValue();
    struct.setFieldValue("b", new IntegerFieldValue(69));
    FieldPathUpdate upd = new AssignFieldPathUpdate(docType, "a", "", struct);
    Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
    assertNotNull(doc);
    FieldValue obj = doc.getFieldValue("a");
    assertTrue(obj instanceof Struct);
    struct = (Struct) obj;
    struct.setFieldValue("b", new IntegerFieldValue(96));
    DocumentUpdate docUpd = new FieldPathUpdateAdapter(new SimpleDocumentAdapter(null, doc), upd).getOutput();
    assertNotNull(docUpd);
    assertEquals(1, docUpd.getFieldPathUpdates().size());
    assertNotNull(upd = docUpd.getFieldPathUpdates().get(0));
    assertTrue(upd instanceof AssignFieldPathUpdate);
    assertEquals("a", upd.getOriginalFieldPath());
    assertEquals(struct, ((AssignFieldPathUpdate) upd).getNewValue());
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 2 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class DocumentToPathUpdateTestCase method requireThatStructElementAssignIsConverted.

@Test
public void requireThatStructElementAssignIsConverted() {
    DocumentType docType = new DocumentType("my_type");
    StructDataType structType = new StructDataType("my_struct");
    structType.addField(new Field("b", DataType.INT));
    docType.addField(new Field("a", structType));
    FieldPathUpdate upd = new AssignFieldPathUpdate(docType, "a.b", "", new IntegerFieldValue(69));
    Document doc = FieldPathUpdateHelper.newPartialDocument(null, upd);
    assertNotNull(doc);
    FieldValue obj = doc.getFieldValue("a");
    assertTrue(obj instanceof Struct);
    Struct struct = (Struct) obj;
    struct.setFieldValue("b", new IntegerFieldValue(96));
    DocumentUpdate docUpd = new FieldPathUpdateAdapter(new SimpleDocumentAdapter(null, doc), upd).getOutput();
    assertNotNull(docUpd);
    assertEquals(1, docUpd.getFieldPathUpdates().size());
    assertNotNull(upd = docUpd.getFieldPathUpdates().get(0));
    assertTrue(upd instanceof AssignFieldPathUpdate);
    assertEquals("a.b", upd.getOriginalFieldPath());
    obj = ((AssignFieldPathUpdate) upd).getNewValue();
    assertTrue(obj instanceof IntegerFieldValue);
    assertEquals(96, ((IntegerFieldValue) obj).getInteger());
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) AssignFieldPathUpdate(com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate) FieldPathUpdate(com.yahoo.document.fieldpathupdate.FieldPathUpdate) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 3 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class DocumentUpdateTestCase method requireThatArrayOfStructElementAddIsProcessedCorrectly.

@Test
public void requireThatArrayOfStructElementAddIsProcessedCorrectly() throws ParseException {
    DocumentType docType = new DocumentType("my_input");
    docType.addField(new Field("my_str", DataType.getArray(DataType.STRING)));
    docType.addField(new Field("my_pos", DataType.getArray(PositionDataType.INSTANCE)));
    DocumentUpdate docUpdate = new DocumentUpdate(docType, "doc:scheme:");
    docUpdate.addFieldUpdate(FieldUpdate.createAdd(docType.getField("my_str"), new StringFieldValue("6;9")));
    docUpdate = Expression.execute(Expression.fromString("input my_str | for_each { to_pos } | index my_pos"), docUpdate);
    assertNotNull(docUpdate);
    assertEquals(0, docUpdate.getFieldPathUpdates().size());
    assertEquals(1, docUpdate.getFieldUpdates().size());
    FieldUpdate fieldUpd = docUpdate.getFieldUpdate(0);
    assertNotNull(fieldUpd);
    assertEquals(docType.getField("my_pos"), fieldUpd.getField());
    assertEquals(1, fieldUpd.getValueUpdates().size());
    ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
    assertNotNull(valueUpd);
    assertTrue(valueUpd instanceof AddValueUpdate);
    Object val = valueUpd.getValue();
    assertNotNull(val);
    assertTrue(val instanceof Struct);
    Struct pos = (Struct) val;
    assertEquals(PositionDataType.INSTANCE, pos.getDataType());
    assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(pos));
    assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(pos));
}
Also used : AddValueUpdate(com.yahoo.document.update.AddValueUpdate) ValueUpdate(com.yahoo.document.update.ValueUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) AddValueUpdate(com.yahoo.document.update.AddValueUpdate) FieldUpdate(com.yahoo.document.update.FieldUpdate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 4 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class SimpleDocumentAdapterTestCase method requireThatStructFieldsCanBeAccessed.

@Test
public void requireThatStructFieldsCanBeAccessed() {
    DataType barType = DataType.STRING;
    FieldValue bar = barType.createFieldValue("bar");
    StructDataType fooType = new StructDataType("my_struct");
    fooType.addField(new Field("bar", barType));
    Struct foo = new Struct(fooType);
    foo.setFieldValue("bar", bar);
    DocumentType docType = new DocumentType("my_doc");
    docType.addField("foo", fooType);
    Document doc = new Document(docType, "doc:scheme:");
    doc.setFieldValue("foo", foo);
    DocumentAdapter adapter = new SimpleDocumentAdapter(doc);
    assertEquals(fooType, adapter.getInputType(null, "foo"));
    assertEquals(foo, adapter.getInputValue("foo"));
    assertEquals(barType, adapter.getInputType(null, "foo.bar"));
    assertEquals(bar, adapter.getInputValue("foo.bar"));
}
Also used : FieldValue(com.yahoo.document.datatypes.FieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 5 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method getDoc.

private Document getDoc() {
    DocumentType type = new DocumentType("album");
    AnnotationType personType = new AnnotationType("person");
    Annotation person = new Annotation(personType);
    type.addField("title", DataType.STRING);
    type.addField("artist", DataType.STRING);
    type.addField("guitarist", DataType.STRING);
    type.addField("year", DataType.INT);
    type.addField("labels", DataType.getArray(DataType.STRING));
    Document doc = new Document(type, new DocumentId("doc:map:test:1"));
    doc.setFieldValue("title", new StringFieldValue("Black Rock"));
    StringFieldValue joe = new StringFieldValue("Joe Bonamassa");
    joe.setSpanTree(new SpanTree("mytree").annotate(person));
    doc.setFieldValue("artist", joe);
    doc.setFieldValue("year", new IntegerFieldValue(2010));
    Array<StringFieldValue> labels = new Array<>(type.getField("labels").getDataType());
    labels.add(new StringFieldValue("audun"));
    labels.add(new StringFieldValue("tylden"));
    doc.setFieldValue("labels", labels);
    StructDataType personStructType = new StructDataType("artist");
    personStructType.addField(new com.yahoo.document.Field("firstname", DataType.STRING));
    personStructType.addField(new com.yahoo.document.Field("lastname", DataType.STRING));
    type.addField("listeners", DataType.getArray(personStructType));
    Array<Struct> listeners = new Array<>(type.getField("listeners").getDataType());
    Struct listenerOne = new Struct(personStructType);
    listenerOne.setFieldValue("firstname", new StringFieldValue("per"));
    listenerOne.setFieldValue("lastname", new StringFieldValue("olsen"));
    Struct listenerTwo = new Struct(personStructType);
    listenerTwo.setFieldValue("firstname", new StringFieldValue("anders"));
    listenerTwo.setFieldValue("lastname", new StringFieldValue("and"));
    listeners.add(listenerOne);
    listeners.add(listenerTwo);
    doc.setFieldValue("listeners", listeners);
    return doc;
}
Also used : DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Document(com.yahoo.document.Document) AnnotationType(com.yahoo.document.annotation.AnnotationType) Annotation(com.yahoo.document.annotation.Annotation) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) SpanTree(com.yahoo.document.annotation.SpanTree)

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