use of com.yahoo.document.StructDataType in project vespa by vespa-engine.
the class SpanTreeTestCase method testCleanup.
@Test
public void testCleanup() {
SpanList a = new SpanList();
SpanTree tree = new SpanTree("ballooO", a);
Span b = new Span(0, 1);
Span c = new Span(1, 1);
a.add(b).add(c);
AnnotationReferenceDataType refTypeToDummy = new AnnotationReferenceDataType(dummy);
AnnotationType annotationTypeWithRefToDummy = new AnnotationType("reftodummy", refTypeToDummy);
AnnotationReferenceDataType refTypeToRefTypeToDummy = new AnnotationReferenceDataType(annotationTypeWithRefToDummy);
AnnotationType annotationTypeWithRefToRefToDummy = new AnnotationType("reftoreftodummy", refTypeToRefTypeToDummy);
StructDataType structType = new StructDataType("str");
Field refToDummyField = new Field("refToDummy", refTypeToDummy);
structType.addField(refToDummyField);
AnnotationType annotationTypeWithStruct = new AnnotationType("structwithref", structType);
Annotation dummyAnnotationForB = new Annotation(dummy);
tree.annotate(b, dummyAnnotationForB);
AnnotationReference referenceToDummyB = new AnnotationReference(refTypeToDummy, dummyAnnotationForB);
Annotation annotationWithRefToDummyB = new Annotation(annotationTypeWithRefToDummy, referenceToDummyB);
tree.annotate(annotationWithRefToDummyB);
AnnotationReference referenceToRefToDummyB = new AnnotationReference(refTypeToRefTypeToDummy, annotationWithRefToDummyB);
Annotation annotationWithRefToRefToDummyB = new Annotation(annotationTypeWithRefToRefToDummy, referenceToRefToDummyB);
tree.annotate(annotationWithRefToRefToDummyB);
Struct struct = new Struct(structType);
struct.setFieldValue(refToDummyField, new AnnotationReference(refTypeToDummy, dummyAnnotationForB));
Annotation annotationWithStruct = new Annotation(annotationTypeWithStruct, struct);
tree.annotate(annotationWithStruct);
Iterator<Annotation> annotationIt;
assertEquals(4, tree.numAnnotations());
assertTrue(a.isValid());
assertTrue(b.isValid());
assertTrue(c.isValid());
assertEquals(2, a.numChildren());
{
List<Annotation> allAnnotationsList = new ArrayList<>();
for (Annotation an : tree) {
allAnnotationsList.add(an);
}
Collections.sort(allAnnotationsList);
annotationIt = allAnnotationsList.iterator();
}
// NOTE: ordering of annotations is derived from their name
assertSame(annotationWithRefToDummyB, annotationIt.next());
assertFalse(annotationWithRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
assertTrue(annotationWithRefToDummyB.hasFieldValue());
assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
assertSame(annotationWithStruct, annotationIt.next());
assertFalse(annotationWithStruct.hasSpanNode());
assertFalse(annotationWithStruct.isSpanNodeValid());
assertTrue(annotationWithStruct.hasFieldValue());
assertSame(struct, annotationWithStruct.getFieldValue());
assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
assertSame(dummyAnnotationForB, annotationIt.next());
assertTrue(dummyAnnotationForB.hasSpanNode());
assertTrue(dummyAnnotationForB.isSpanNodeValid());
assertFalse(dummyAnnotationForB.hasFieldValue());
assertFalse(annotationIt.hasNext());
// removing b!!
a.remove(b);
assertEquals(4, tree.numAnnotations());
assertTrue(a.isValid());
assertFalse(b.isValid());
assertTrue(c.isValid());
assertEquals(1, a.numChildren());
{
List<Annotation> allAnnotationsList = new ArrayList<>();
for (Annotation an : tree) {
allAnnotationsList.add(an);
}
Collections.sort(allAnnotationsList);
annotationIt = allAnnotationsList.iterator();
}
assertSame(annotationWithRefToDummyB, annotationIt.next());
assertFalse(annotationWithRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
assertTrue(annotationWithRefToDummyB.hasFieldValue());
assertSame(dummyAnnotationForB, ((AnnotationReference) annotationWithRefToDummyB.getFieldValue()).getReference());
assertSame(annotationWithStruct, annotationIt.next());
assertFalse(annotationWithStruct.hasSpanNode());
assertFalse(annotationWithStruct.isSpanNodeValid());
assertTrue(annotationWithStruct.hasFieldValue());
assertSame(struct, annotationWithStruct.getFieldValue());
assertSame(dummyAnnotationForB, ((AnnotationReference) struct.getFieldValue(refToDummyField)).getReference());
assertSame(annotationWithRefToRefToDummyB, annotationIt.next());
assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
assertTrue(annotationWithRefToRefToDummyB.hasFieldValue());
assertSame(annotationWithRefToDummyB, ((AnnotationReference) annotationWithRefToRefToDummyB.getFieldValue()).getReference());
assertSame(dummyAnnotationForB, annotationIt.next());
assertTrue(dummyAnnotationForB.hasSpanNode());
assertFalse(dummyAnnotationForB.isSpanNodeValid());
assertFalse(dummyAnnotationForB.hasFieldValue());
assertFalse(annotationIt.hasNext());
// cleaning up tree!!
tree.cleanup();
assertEquals(1, tree.numAnnotations());
assertTrue(a.isValid());
assertFalse(b.isValid());
assertTrue(c.isValid());
assertEquals(1, a.numChildren());
assertFalse(dummyAnnotationForB.hasSpanNode());
assertFalse(dummyAnnotationForB.isSpanNodeValid());
assertFalse(dummyAnnotationForB.hasFieldValue());
assertFalse(annotationWithRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToDummyB.isSpanNodeValid());
assertFalse(annotationWithRefToDummyB.hasFieldValue());
assertFalse(annotationWithRefToRefToDummyB.hasSpanNode());
assertFalse(annotationWithRefToRefToDummyB.isSpanNodeValid());
assertFalse(annotationWithRefToRefToDummyB.hasFieldValue());
annotationIt = tree.iterator();
assertSame(annotationWithStruct, annotationIt.next());
assertFalse(annotationWithStruct.hasSpanNode());
assertFalse(annotationWithStruct.isSpanNodeValid());
assertTrue(annotationWithStruct.hasFieldValue());
assertSame(struct, annotationWithStruct.getFieldValue());
assertNull(struct.getFieldValue(refToDummyField));
assertFalse(annotationIt.hasNext());
}
use of com.yahoo.document.StructDataType in project vespa by vespa-engine.
the class StructTestCase method testSetGetPrimitiveTypes.
@Test
public void testSetGetPrimitiveTypes() throws Exception {
StructDataType type = new StructDataType("teststr");
type.addField(new Field("int", DataType.INT));
type.addField(new Field("flt", DataType.FLOAT));
type.addField(new Field("str", DataType.STRING));
type.addField(new Field("raw", DataType.RAW));
type.addField(new Field("lng", DataType.LONG));
type.addField(new Field("dbl", DataType.DOUBLE));
type.addField(new Field("uri", DataType.URI));
type.addField(new Field("byt", DataType.BYTE));
Struct struct = new Struct(type);
{
IntegerFieldValue nt = new IntegerFieldValue(544);
Object o = struct.setFieldValue("int", nt);
assertNull(o);
assertEquals(new IntegerFieldValue(544), struct.getFieldValue("int"));
o = struct.setFieldValue("int", 500);
assertEquals(nt, o);
assertFalse(nt.equals(struct.getFieldValue("int")));
}
{
FloatFieldValue flt = new FloatFieldValue(5.44f);
Object o = struct.setFieldValue("flt", flt);
assertNull(o);
assertEquals(flt, struct.getFieldValue("flt"));
o = struct.setFieldValue("flt", new FloatFieldValue(5.00f));
assertEquals(flt, o);
assertFalse(flt.equals(struct.getFieldValue("flt")));
}
{
StringFieldValue string = new StringFieldValue("this is a string");
Object o = struct.setFieldValue("str", string);
assertNull(o);
assertEquals(string, struct.getFieldValue("str"));
o = struct.setFieldValue("str", "another string");
assertEquals(string, o);
assertSame(string, o);
assertFalse(string.equals(struct.getFieldValue("str")));
}
{
Raw buf = new Raw(ByteBuffer.wrap(new byte[100]));
Object o = struct.setFieldValue("raw", buf);
assertNull(o);
assertEquals(buf, struct.getFieldValue("raw"));
o = struct.setFieldValue("raw", new Raw(ByteBuffer.wrap(new byte[50])));
assertEquals(buf, o);
assertSame(buf, o);
assertFalse(buf.equals(struct.getFieldValue("raw")));
}
{
LongFieldValue lng = new LongFieldValue(59879879879079L);
Object o = struct.setFieldValue("lng", lng);
assertEquals(lng, struct.getFieldValue("lng"));
o = struct.setFieldValue("lng", new LongFieldValue(23418798734243L));
assertEquals(lng, o);
assertFalse(lng.equals(struct.getFieldValue("lng")));
}
{
DoubleFieldValue dbl = new DoubleFieldValue(5.44d);
Object o = struct.setFieldValue("dbl", dbl);
assertNull(o);
assertEquals(dbl, struct.getFieldValue("dbl"));
o = struct.setFieldValue("dbl", new DoubleFieldValue(5.00d));
assertEquals(dbl, o);
assertFalse(dbl.equals(struct.getFieldValue("dbl")));
}
{
UriFieldValue uri = new UriFieldValue("this is a uri");
Object o = struct.setFieldValue("uri", uri);
assertNull(o);
assertEquals(uri, struct.getFieldValue("uri"));
o = struct.setFieldValue("uri", "another uri");
assertEquals(uri, o);
assertSame(uri, o);
assertFalse(uri.equals(struct.getFieldValue("uri")));
}
{
ByteFieldValue byt = new ByteFieldValue((byte) 123);
Object o = struct.setFieldValue("byt", byt);
assertNull(o);
assertEquals(byt, struct.getFieldValue("byt"));
o = struct.setFieldValue("byt", (byte) 100);
assertEquals(byt, o);
assertFalse(byt.equals(struct.getFieldValue("byt")));
}
}
use of com.yahoo.document.StructDataType in project vespa by vespa-engine.
the class StructTestCase method testBasicStuff.
@Test
public void testBasicStuff() throws Exception {
StructDataType type = new StructDataType("teststr");
type.addField(new Field("int", 0, DataType.INT, true));
type.addField(new Field("flt", 1, DataType.FLOAT, true));
type.addField(new Field("str", 2, DataType.STRING, true));
type.addField(new Field("raw", 3, DataType.RAW, true));
type.addField(new Field("lng", 4, DataType.LONG, true));
type.addField(new Field("dbl", 5, DataType.DOUBLE, true));
type.addField(new Field("uri", 6, DataType.URI, true));
type.addField(new Field("byt", 8, DataType.BYTE, true));
Struct struct = new Struct(type);
{
// add and remove again:
assertEquals(0, struct.getFields().size());
IntegerFieldValue ifv = new IntegerFieldValue(5);
struct.setFieldValue("int", ifv);
assertEquals(1, struct.getFields().size());
struct.removeFieldValue("int");
assertEquals(0, struct.getFields().size());
}
{
// add three elements and remove one of them, and replace the last one:
assertEquals(0, struct.getFields().size());
IntegerFieldValue ifv = new IntegerFieldValue(5);
struct.setFieldValue("int", ifv);
assertEquals(1, struct.getFields().size());
FloatFieldValue ffv = new FloatFieldValue(5.0f);
struct.setFieldValue("flt", ffv);
assertEquals(2, struct.getFields().size());
DoubleFieldValue dfv = new DoubleFieldValue(6.0d);
struct.setFieldValue("dbl", dfv);
assertEquals(3, struct.getFields().size());
Iterator<Map.Entry<Field, FieldValue>> it = struct.iterator();
assertSame(ifv, it.next().getValue());
assertSame(ffv, it.next().getValue());
assertSame(dfv, it.next().getValue());
assertFalse(it.hasNext());
struct.removeFieldValue("flt");
assertEquals(2, struct.getFields().size());
it = struct.iterator();
assertSame(ifv, it.next().getValue());
assertSame(dfv, it.next().getValue());
assertFalse(it.hasNext());
DoubleFieldValue dfv2 = new DoubleFieldValue(9.0d);
struct.setFieldValue("dbl", dfv2);
assertEquals(2, struct.getFields().size());
it = struct.iterator();
assertSame(ifv, it.next().getValue());
assertSame(dfv2, it.next().getValue());
assertFalse(it.hasNext());
}
}
use of com.yahoo.document.StructDataType in project vespa by vespa-engine.
the class StructTestCase method sortingOrderIndependentOfValueInsertionOrder.
@Test
public void sortingOrderIndependentOfValueInsertionOrder() {
StructDataType type = new StructDataType("test");
type.addField(new Field("int", DataType.INT));
type.addField(new Field("flt", DataType.FLOAT));
type.addField(new Field("str", DataType.STRING));
Struct a = new Struct(type);
a.setFieldValue("int", new IntegerFieldValue(123));
a.setFieldValue("flt", new DoubleFieldValue(45.6));
a.setFieldValue("str", new StringFieldValue("hello world"));
Struct b = new Struct(type);
b.setFieldValue("str", new StringFieldValue("hello world"));
b.setFieldValue("flt", new DoubleFieldValue(45.6));
b.setFieldValue("int", new IntegerFieldValue(123));
assertEquals(0, a.compareTo(b));
assertEquals(0, b.compareTo(a));
assertTrue(a.equals(b));
assertTrue(b.equals(a));
b.setFieldValue("int", new IntegerFieldValue(122));
assertTrue(a.compareTo(b) > 0);
assertTrue(b.compareTo(a) < 0);
assertFalse(a.equals(b));
assertFalse(b.equals(a));
}
use of com.yahoo.document.StructDataType in project vespa by vespa-engine.
the class DocumentModelBuilder method handleStruct.
private static StructDataType handleStruct(NewDocumentType dt, SDDocumentType type) {
StructDataType s = new StructDataType(type.getName());
for (Field f : type.getDocumentType().getHeaderType().getFieldsThisTypeOnly()) {
specialHandleAnnotationReference(dt, f);
s.addField(f);
}
for (StructDataType inherited : type.getDocumentType().getHeaderType().getInheritedTypes()) {
s.inherit(inherited);
}
extractNestedTypes(dt, s);
addType(dt, s);
return s;
}
Aggregations