use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class VespaDocumentDeserializer42 method read.
public void read(AddFieldPathUpdate update) {
DataType dt = update.getFieldPath().getResultingDataType();
FieldValue fv = dt.createFieldValue();
dt.createFieldValue();
fv.deserialize(this);
if (!(fv instanceof Array)) {
throw new DeserializationException("Add only applicable to array types");
}
update.setNewValues((Array) fv);
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class DocumentTestCase method validateCppDocNotMap.
@SuppressWarnings("unchecked")
public void validateCppDocNotMap(Document doc) throws IOException {
// in practice to validate v6 serialization
assertEquals("doc:serializetest:http://test.doc.id/", doc.getId().toString());
assertEquals(new IntegerFieldValue(5), doc.getFieldValue("intfield"));
assertEquals(new FloatFieldValue((float) -9.23), doc.getFieldValue("floatfield"));
assertEquals(new StringFieldValue("This is a string."), doc.getFieldValue("stringfield"));
assertEquals(new LongFieldValue(398420092938472983L), doc.getFieldValue("longfield"));
assertEquals(new DoubleFieldValue(98374532.398820d), doc.getFieldValue("doublefield"));
assertEquals(new StringFieldValue("http://this.is.a.test/"), doc.getFieldValue("urifield"));
// NOTE: The value really is unsigned 254, which becomes signed -2:
assertEquals(new ByteFieldValue(-2), doc.getFieldValue("bytefield"));
ByteBuffer raw = ByteBuffer.wrap("RAW DATA".getBytes());
assertEquals(new Raw(raw), doc.getFieldValue("rawfield"));
Document docindoc = (Document) doc.getFieldValue("docfield");
assertEquals(docMan.getDocumentType("docindoc"), docindoc.getDataType());
assertEquals(new DocumentId("doc:docindoc:http://embedded"), docindoc.getId());
Array<FloatFieldValue> array = (Array<FloatFieldValue>) doc.getFieldValue("arrayoffloatfield");
assertEquals(new FloatFieldValue(1.0f), array.get(0));
assertEquals(new FloatFieldValue(2.0f), array.get(1));
WeightedSet<StringFieldValue> wset = (WeightedSet<StringFieldValue>) doc.getFieldValue("wsfield");
assertEquals(new Integer(50), wset.get(new StringFieldValue("Weighted 0")));
assertEquals(new Integer(199), wset.get(new StringFieldValue("Weighted 1")));
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class DocumentTestCase method testVariables.
@Test
public void testVariables() {
ArrayDataType iarr = new ArrayDataType(DataType.INT);
ArrayDataType iiarr = new ArrayDataType(iarr);
ArrayDataType iiiarr = new ArrayDataType(iiarr);
DocumentType type = new DocumentType("test");
type.addField(new Field("iiiarray", iiiarr));
Array<Array<Array<IntegerFieldValue>>> iiiaV = new Array<>(iiiarr);
for (int i = 1; i < 4; i++) {
Array<Array<IntegerFieldValue>> iiaV = new Array<>(iiarr);
for (int j = 1; j < 4; j++) {
Array<IntegerFieldValue> iaV = new Array<>(iarr);
for (int k = 1; k < 4; k++) {
iaV.add(new IntegerFieldValue(i * j * k));
}
iiaV.add(iaV);
}
iiiaV.add(iiaV);
}
Document doc = new Document(type, new DocumentId("doc:foo:testdoc"));
doc.setFieldValue("iiiarray", iiiaV);
{
VariableIteratorHandler handler = new VariableIteratorHandler();
FieldPath path = type.buildFieldPath("iiiarray[$x][$y][$z]");
doc.iterateNested(path, 0, handler);
String fasit = "x: 0,y: 0,z: 0, - 1\n" + "x: 0,y: 0,z: 1, - 2\n" + "x: 0,y: 0,z: 2, - 3\n" + "x: 0,y: 1,z: 0, - 2\n" + "x: 0,y: 1,z: 1, - 4\n" + "x: 0,y: 1,z: 2, - 6\n" + "x: 0,y: 2,z: 0, - 3\n" + "x: 0,y: 2,z: 1, - 6\n" + "x: 0,y: 2,z: 2, - 9\n" + "x: 1,y: 0,z: 0, - 2\n" + "x: 1,y: 0,z: 1, - 4\n" + "x: 1,y: 0,z: 2, - 6\n" + "x: 1,y: 1,z: 0, - 4\n" + "x: 1,y: 1,z: 1, - 8\n" + "x: 1,y: 1,z: 2, - 12\n" + "x: 1,y: 2,z: 0, - 6\n" + "x: 1,y: 2,z: 1, - 12\n" + "x: 1,y: 2,z: 2, - 18\n" + "x: 2,y: 0,z: 0, - 3\n" + "x: 2,y: 0,z: 1, - 6\n" + "x: 2,y: 0,z: 2, - 9\n" + "x: 2,y: 1,z: 0, - 6\n" + "x: 2,y: 1,z: 1, - 12\n" + "x: 2,y: 1,z: 2, - 18\n" + "x: 2,y: 2,z: 0, - 9\n" + "x: 2,y: 2,z: 1, - 18\n" + "x: 2,y: 2,z: 2, - 27\n";
assertEquals(fasit, handler.retVal);
}
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class DocumentTestCase method testModifyDocument.
@Test
public void testModifyDocument() {
Document doc = new Document(testDocType, new DocumentId("doc:ns:testdoc"));
doc.setFieldValue("primitive1", 1);
Struct l1s1 = new Struct(doc.getField("l1s1").getDataType());
l1s1.setFieldValue("primitive1", 2);
Struct l2s1 = new Struct(doc.getField("struct2").getDataType());
l2s1.setFieldValue("primitive1", 3);
l2s1.setFieldValue("primitive2", 4);
Array<IntegerFieldValue> iarr1 = new Array<>(l2s1.getField("iarray").getDataType());
iarr1.add(new IntegerFieldValue(11));
iarr1.add(new IntegerFieldValue(12));
iarr1.add(new IntegerFieldValue(13));
l2s1.setFieldValue("iarray", iarr1);
ArrayDataType dt = (ArrayDataType) l2s1.getField("sarray").getDataType();
Array<Struct> sarr1 = new Array<>(dt);
{
Struct l3s1 = new Struct(dt.getNestedType());
l3s1.setFieldValue("primitive1", 1);
l3s1.setFieldValue("primitive2", 2);
sarr1.add(l3s1);
}
{
Struct l3s1 = new Struct(dt.getNestedType());
l3s1.setFieldValue("primitive1", 1);
l3s1.setFieldValue("primitive2", 2);
sarr1.add(l3s1);
}
l2s1.setFieldValue("sarray", sarr1);
MapFieldValue<StringFieldValue, StringFieldValue> smap1 = new MapFieldValue<>((MapDataType) l2s1.getField("smap").getDataType());
smap1.put(new StringFieldValue("leonardo"), new StringFieldValue("dicaprio"));
smap1.put(new StringFieldValue("ellen"), new StringFieldValue("page"));
smap1.put(new StringFieldValue("joseph"), new StringFieldValue("gordon-levitt"));
l2s1.setFieldValue("smap", smap1);
l1s1.setFieldValue("ss", l2s1.clone());
MapFieldValue<StringFieldValue, Struct> structmap1 = new MapFieldValue<>((MapDataType) l1s1.getField("structmap").getDataType());
structmap1.put(new StringFieldValue("test"), l2s1.clone());
l1s1.setFieldValue("structmap", structmap1);
WeightedSet<StringFieldValue> wset1 = new WeightedSet<>(l1s1.getField("wset").getDataType());
wset1.add(new StringFieldValue("foo"));
wset1.add(new StringFieldValue("bar"));
wset1.add(new StringFieldValue("zoo"));
l1s1.setFieldValue("wset", wset1);
Struct l2s2 = new Struct(doc.getField("struct2").getDataType());
l2s2.setFieldValue("primitive1", 5);
l2s2.setFieldValue("primitive2", 6);
WeightedSet<Struct> wset2 = new WeightedSet<>(l1s1.getField("structwset").getDataType());
wset2.add(l2s1.clone());
wset2.add(l2s2.clone());
l1s1.setFieldValue("structwset", wset2);
doc.setFieldValue("l1s1", l1s1.clone());
{
ModifyIteratorHandler handler = new ModifyIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap{leonardo}");
assertEquals(new StringFieldValue("newvalue"), fv);
}
{
AddIteratorHandler handler = new AddIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertTrue(((Array) fv).contains(new IntegerFieldValue(32)));
assertEquals(4, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[1]");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertFalse(((Array) fv).contains(new Integer(12)));
assertEquals(3, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[$x]");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertEquals(0, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap");
assertFalse(((MapFieldValue) fv).contains(new StringFieldValue("leonardo")));
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.wset{foo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.wset");
assertFalse(((WeightedSet) fv).contains(new StringFieldValue("foo")));
}
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class Bug4261985TestCase method annotate.
public void annotate(Document document, DocumentTypeManager manager) {
AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
AnnotationType company = registry.getType("company");
AnnotationType industry = registry.getType("industry");
AnnotationType person = registry.getType("person");
AnnotationType location = registry.getType("location");
AnnotationType bigshots = registry.getType("bigshots");
if (company.inherits(industry)) {
System.out.println("Company Inherits Industry");
} else {
System.out.println("FAIL: COMPANY DOES NOT INHERIT INDUSTRY");
throw new RuntimeException("FAIL: COMPANY DOES NOT INHERIT INDUSTRY, though it does in SD file");
}
SpanTree tree = new SpanTree("testannotations");
SpanList root = (SpanList) tree.getRoot();
SpanNode span1 = new Span(0, 5);
SpanNode span2 = new Span(5, 10);
SpanNode span3 = new Span(10, 15);
SpanNode span4 = new Span(15, 20);
SpanNode span5 = new Span(6, 10);
SpanNode span6 = new Span(8, 4);
SpanNode span7 = new Span(4, 2);
SpanNode span8 = new Span(12, 6);
root.add(span1);
root.add(span2);
// root.add(span3);
root.add(span4);
root.add(span5);
root.add(span6);
// root.add(span7);
root.add(span8);
AlternateSpanList aspl = new AlternateSpanList();
aspl.add(span7);
List<SpanNode> subtree1 = new ArrayList<SpanNode>();
subtree1.add(span3);
aspl.addChildren(1, subtree1, 33.0d);
root.add(aspl);
Struct personValue = (Struct) person.getDataType().createFieldValue();
personValue.setFieldValue("name", "Richard Bair");
Annotation personAn = new Annotation(person, personValue);
tree.annotate(span1, personAn);
Struct companyValue = (Struct) company.getDataType().createFieldValue();
companyValue.setFieldValue("name", "Sun");
Struct locationVal = new Struct(manager.getDataType("annotation.location"));
locationVal.setFieldValue("lat", 37.774929);
locationVal.setFieldValue("lon", -122.419415);
Annotation locAnnotation = new Annotation(location, locationVal);
Field compLocField = ((StructDataType) company.getDataType()).getField("place");
// FieldValue compLocFieldVal = new FieldValue(compLocField.getDataType());
AnnotationReferenceDataType annType = (AnnotationReferenceDataType) compLocField.getDataType();
FieldValue compLocFieldVal = null;
// if (scenario.equals("createFieldValue")) {
// compLocFieldVal = annType.createFieldValue(new AnnotationReference(annType, locAnnotation));
// } else {
compLocFieldVal = new AnnotationReference(annType, locAnnotation);
// }
companyValue.setFieldValue(compLocField, compLocFieldVal);
companyValue.setFieldValue("vertical", "software");
Struct dirValue1 = new Struct(manager.getDataType("annotation.person"));
dirValue1.setFieldValue("name", "Jonathan Schwartz");
Annotation dirAnnotation1 = new Annotation(person, dirValue1);
Struct dirValue2 = new Struct(manager.getDataType("annotation.person"));
dirValue2.setFieldValue("name", "Scott Mcnealy");
Annotation dirAnnotation2 = new Annotation(person, dirValue2);
Field dirField = ((StructDataType) company.getDataType()).getField("directors");
Array<FieldValue> dirFieldVal = new Array<FieldValue>(dirField.getDataType());
AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) ((ArrayDataType) dirField.getDataType()).getNestedType();
dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation1));
dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation2));
companyValue.setFieldValue(dirField, dirFieldVal);
Annotation compAn = new Annotation(company, companyValue);
tree.annotate(span2, compAn);
Struct bigshotsValue = (Struct) bigshots.getDataType().createFieldValue();
Field ceosField = ((StructDataType) bigshots.getDataType()).getField("ceos");
// FieldValue compLocFieldVal = new FieldValue(compLocField.getDataType());
AnnotationReferenceDataType annType1 = (AnnotationReferenceDataType) ceosField.getDataType();
FieldValue ceosFieldVal = new AnnotationReference(annType1, compAn);
bigshotsValue.setFieldValue(ceosField, ceosFieldVal);
Annotation bigshotsAn = new Annotation(bigshots, bigshotsValue);
tree.annotate(span8, bigshotsAn);
Field selfField = ((StructDataType) bigshots.getDataType()).getField("self");
AnnotationReferenceDataType annType2 = (AnnotationReferenceDataType) selfField.getDataType();
FieldValue selfFieldVal = new AnnotationReference(annType2, bigshotsAn);
bigshotsValue.setFieldValue(selfField, selfFieldVal);
bigshotsAn = new Annotation(bigshots, bigshotsValue);
tree.annotate(span8, bigshotsAn);
tree.annotate(span3, locAnnotation);
tree.annotate(span5, dirAnnotation1);
tree.annotate(span6, dirAnnotation2);
Struct indValue = new Struct(manager.getDataType("annotation.industry"));
indValue.setFieldValue("vertical", "Manufacturing");
Annotation indAn = new Annotation(industry, indValue);
tree.annotate(span4, indAn);
StringFieldValue body = (StringFieldValue) document.getFieldValue(document.getDataType().getField("body"));
body.setSpanTree(tree);
document.setFieldValue(document.getDataType().getField("body"), body);
}
Aggregations