use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SpanTestCase method serializeAndAssert.
private void serializeAndAssert(Span span) {
GrowableByteBuffer buffer;
{
buffer = new GrowableByteBuffer(1024);
DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lk");
SpanTree tree = new SpanTree("bababa", span);
value.setSpanTree(tree);
serializer.write(null, value);
buffer.flip();
}
Span span2;
{
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(man, buffer);
StringFieldValue value = new StringFieldValue();
deserializer.read(null, value);
span2 = (Span) value.getSpanTree("bababa").getRoot();
}
assertEquals(span, span2);
assertNotSame(span, span2);
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SpanTreeTestCase method testCyclicReferences.
@Test
public void testCyclicReferences() {
DocumentTypeManager docMan = new DocumentTypeManager();
AnnotationTypeRegistry reg = docMan.getAnnotationTypeRegistry();
StringFieldValue strfval = new StringFieldValue("hohohoho");
SpanList root = new SpanList();
SpanTree tree = new SpanTree("habahaba", root);
strfval.setSpanTree(tree);
// set up types:
AnnotationType endTagType = new AnnotationType("endtag");
AnnotationType beginTagType = new AnnotationType("begintag");
AnnotationReferenceDataType refToBeginTagDataType = new AnnotationReferenceDataType(beginTagType);
AnnotationReferenceDataType refToEndTagDataType = new AnnotationReferenceDataType(endTagType);
endTagType.setDataType(refToBeginTagDataType);
beginTagType.setDataType(refToEndTagDataType);
// register types:
reg.register(endTagType);
reg.register(beginTagType);
docMan.register(refToBeginTagDataType);
docMan.register(refToEndTagDataType);
// set up annotations and their references to each other:
AnnotationReference refToBeginTag = new AnnotationReference(refToBeginTagDataType);
AnnotationReference refToEndTag = new AnnotationReference(refToEndTagDataType);
Annotation beginTag = new Annotation(beginTagType, refToEndTag);
Annotation endTag = new Annotation(endTagType, refToBeginTag);
refToBeginTag.setReference(beginTag);
refToEndTag.setReference(endTag);
// create spans:
Span beginTagSpan = new Span(0, 1);
Span endTagSpan = new Span(1, 1);
root.add(beginTagSpan);
root.add(endTagSpan);
// annotate spans:
tree.annotate(beginTagSpan, beginTag);
tree.annotate(endTagSpan, endTag);
// none of the below statements should lead to a StackOverflowError:
assertFalse(endTag.toString().equals(beginTag.toString()));
assertTrue(endTag.hashCode() != beginTag.hashCode());
assertFalse(endTag.equals(beginTag));
assertFalse(beginTag.equals(endTag));
StringFieldValue copyString = strfval.clone();
assertEquals(strfval, copyString);
GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
serializer.write(new Field("stringfield", DataType.STRING), strfval);
buffer.flip();
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, buffer);
StringFieldValue stringFieldValue2 = new StringFieldValue();
deserializer.read(new Field("stringfield", DataType.STRING), stringFieldValue2);
assertEquals(strfval, stringFieldValue2);
assertNotSame(strfval, stringFieldValue2);
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SerializeAnnotationsTestCase method testSerializeSimpleTree.
public void testSerializeSimpleTree() throws IOException {
SpanList root = new SpanList();
root.add(new Span(0, 19)).add(new Span(19, 5)).add(new Span(24, 21)).add(new Span(45, 23)).add(new Span(68, 14));
SpanTree tree = new SpanTree("html", root);
StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj " + "lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj l jlkj lkj lkj " + "lkjoijoij oij oij oij oij oij oijoijoij oij oij oij oij oij " + "oijoijoijoijoijoijoijoijoijoijoijoijoij oij oij oij oij " + "oijaosdifjoai fdoais jdoasi jai os oafoai ai dfojsfoa dfoi dsf" + "aosifjasofija sodfij oasdifj aosdiosifjsi ooai oais osi");
value.setSpanTree(tree);
/*
Important note! The iteration order of annotations in SpanTree.iterator() is non-deterministic, meaning
that the order which annotations are serialized will differ between test runs. Thus, we cannot assert
that a serialized buffer is equal to a buffer written earlier. We can, however, assert that the size stays
the same, and the deserialized values from the buffers should be equal.
*/
// important! call readFile() before writeFile()!
ByteBuffer serializedFromFile = readFile("test_data_serialized_simple");
ByteBuffer serialized = writeFile(value, "test_data_serialized_simple");
assertEquals(serialized.limit(), serializedFromFile.limit());
StringFieldValue valueFromFile = new StringFieldValue();
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
deserializer.read(null, valueFromFile);
assertEquals(value, valueFromFile);
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SpanListTestCase method serializeAndAssert.
private void serializeAndAssert(SpanList spanList) {
GrowableByteBuffer buffer;
{
buffer = new GrowableByteBuffer(1024);
DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lk");
SpanTree tree = new SpanTree("bababa", spanList);
value.setSpanTree(tree);
serializer.write(null, value);
buffer.flip();
}
SpanList spanList2;
{
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(man, buffer);
StringFieldValue value = new StringFieldValue();
deserializer.read(null, value);
spanList2 = (SpanList) value.getSpanTree("bababa").getRoot();
}
assertEquals(spanList, spanList2);
assertNotSame(spanList, spanList2);
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class Bug6394548TestCase method testSerializeAndDeserializeMultipleAdjacentStructAnnotations.
@Test
public void testSerializeAndDeserializeMultipleAdjacentStructAnnotations() {
DocumentTypeManager manager = new DocumentTypeManager();
DocumentTypeManagerConfigurer.configure(manager, "file:src/test/java/com/yahoo/document/annotation/documentmanager.6394548.cfg");
AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
AnnotationType featureSetType = registry.getType("morty.RICK_FEATURESET");
assertNotNull(featureSetType);
Document doc = new Document(manager.getDocumentType("article"), "doc:article:test");
StringFieldValue sfv = new StringFieldValue("badger waltz");
SpanList root = new SpanList();
SpanNode node = new Span(0, sfv.getString().length());
root.add(node);
SpanTree tree = new SpanTree("rick_features", root);
for (int i = 0; i < 2; ++i) {
tree.annotate(createBigFeatureSetAnnotation(featureSetType));
}
sfv.setSpanTree(tree);
doc.setFieldValue("title", sfv);
System.out.println(doc.toXml());
String annotationsBefore = dumpAllAnnotations(tree);
GrowableByteBuffer buffer = new GrowableByteBuffer();
DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
serializer.write(doc);
buffer.flip();
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(manager, buffer);
Document doc2 = new Document(deserializer);
System.out.println(doc2.toXml());
StringFieldValue readString = (StringFieldValue) doc2.getFieldValue("title");
SpanTree readTree = readString.getSpanTree("rick_features");
assertNotNull(readTree);
String annotationsAfter = dumpAllAnnotations(readTree);
System.out.println("before:\n" + annotationsBefore);
System.out.println("after:\n" + annotationsAfter);
assertEquals(annotationsBefore, annotationsAfter);
}
Aggregations