use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class DocumentTestCase method testDocumentDataType.
@Test
public void testDocumentDataType() {
// use documenttypemanagerconfigurer to read config
docMan = new DocumentTypeManager();
DocumentTypeManagerConfigurer.configure(docMan, "file:src/test/document/docindoc.cfg");
// get a document type
docMan.getDocumentType("outerdoc");
// create document and necessary structures
Document outerdoc = new Document(docMan.getDocumentType("outerdoc"), new DocumentId("doc:recursion:outerdoc"));
Document innerdoc = new Document(docMan.getDocumentType("innerdoc"), new DocumentId("doc:recursion:innerdoc"));
innerdoc.setFieldValue("intfield", 55);
outerdoc.setFieldValue("stringfield", "boooo");
outerdoc.setFieldValue("docfield", innerdoc);
// serialize document
int size = outerdoc.getSerializedSize();
GrowableByteBuffer buf = new GrowableByteBuffer(size, 2.0f);
outerdoc.serialize(buf);
assertEquals(size, buf.position());
// deserialize document
buf.position(0);
Document outerdoc2 = docMan.createDocument(buf);
// compare values
assertEquals(outerdoc, outerdoc2);
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class DocumentTestCase method testGenerateSerializedFile.
@Test
public void testGenerateSerializedFile() throws IOException {
docMan = setUpCppDocType();
Document doc = new Document(docMan.getDocumentType("serializetest"), new DocumentId("doc:serializetest:http://test.doc.id/"));
Document docindoc = new Document(docMan.getDocumentType("docindoc"), new DocumentId("doc:serializetest:http://doc.in.doc/"));
docindoc.setFieldValue("stringindocfield", "Elvis is dead");
doc.setFieldValue("docfield", docindoc);
Array<FloatFieldValue> l = new Array<>(doc.getField("arrayoffloatfield").getDataType());
l.add(new FloatFieldValue((float) 1.0));
l.add(new FloatFieldValue((float) 2.0));
doc.setFieldValue("arrayoffloatfield", l);
WeightedSet<StringFieldValue> wset = new WeightedSet<>(doc.getDataType().getField("wsfield").getDataType());
wset.put(new StringFieldValue("Weighted 0"), 50);
wset.put(new StringFieldValue("Weighted 1"), 199);
doc.setFieldValue("wsfield", wset);
MapFieldValue<StringFieldValue, StringFieldValue> map = new MapFieldValue<>((MapDataType) doc.getDataType().getField("mapfield").getDataType());
map.put(new StringFieldValue("foo1"), new StringFieldValue("bar1"));
map.put(new StringFieldValue("foo2"), new StringFieldValue("bar2"));
doc.setFieldValue("mapfield", map);
doc.setFieldValue("bytefield", new ByteFieldValue((byte) 254));
doc.setFieldValue("rawfield", new Raw(ByteBuffer.wrap("RAW DATA".getBytes())));
doc.setFieldValue("intfield", new IntegerFieldValue(5));
doc.setFieldValue("floatfield", new FloatFieldValue(-9.23f));
doc.setFieldValue("stringfield", new StringFieldValue("This is a string."));
doc.setFieldValue("longfield", new LongFieldValue(398420092938472983L));
doc.setFieldValue("doublefield", new DoubleFieldValue(98374532.398820d));
doc.setFieldValue("urifield", new StringFieldValue("http://this.is.a.test/"));
int size = doc.getSerializedSize();
GrowableByteBuffer buf = new GrowableByteBuffer(size, 2.0f);
doc.serialize(buf);
assertEquals(size, buf.position());
buf.position(0);
FileOutputStream fos = new FileOutputStream("src/tests/data/serializejava.dat");
fos.write(buf.array(), 0, size);
fos.close();
CompressionConfig noncomp = new CompressionConfig();
CompressionConfig lz4comp = new CompressionConfig(CompressionType.LZ4);
doc.getDataType().getHeaderType().setCompressionConfig(lz4comp);
doc.getDataType().getBodyType().setCompressionConfig(lz4comp);
buf = new GrowableByteBuffer(size, 2.0f);
doc.serialize(buf);
doc.getDataType().getHeaderType().setCompressionConfig(noncomp);
doc.getDataType().getBodyType().setCompressionConfig(noncomp);
fos = new FileOutputStream("src/tests/data/serializejava-compressed.dat");
fos.write(buf.array(), 0, buf.position());
fos.close();
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class DocumentTestCase method testTimestamp.
@Test
public void testTimestamp() {
Document doc = new Document(docMan.getDocumentType("testdoc"), new DocumentId("doc:testdoc:timetest"));
assertNull(doc.getLastModified());
doc.setLastModified(4350129845023985L);
assertEquals(new Long(4350129845023985L), doc.getLastModified());
doc.setLastModified(null);
assertNull(doc.getLastModified());
Long timestamp = System.currentTimeMillis();
doc.setLastModified(timestamp);
assertEquals(timestamp, doc.getLastModified());
GrowableByteBuffer buf = new GrowableByteBuffer();
doc.getSerializedSize();
doc.serialize(buf);
buf.position(0);
Document doc2 = docMan.createDocument(buf);
assertNull(doc2.getLastModified());
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class DocumentTestCase method testUnknownFieldsDeserialization.
@Test
public void testUnknownFieldsDeserialization() {
DocumentTypeManager docTypeManasjer = new DocumentTypeManager();
GrowableByteBuffer buf = new GrowableByteBuffer();
{
DocumentType typeWithDinner = new DocumentType("elvis");
typeWithDinner.addField("breakfast", DataType.STRING);
typeWithDinner.addField("lunch", DataType.INT);
typeWithDinner.addField("dinner", DataType.DOUBLE);
docTypeManasjer.registerDocumentType(typeWithDinner);
Document docWithDinner = new Document(typeWithDinner, "doc:elvis:has:left:the:building");
docWithDinner.setFieldValue("breakfast", "peanut butter");
docWithDinner.setFieldValue("lunch", 14);
docWithDinner.setFieldValue("dinner", 5.43d);
docWithDinner.serialize(buf);
buf.flip();
docTypeManasjer.clear();
}
{
DocumentType typeWithoutDinner = new DocumentType("elvis");
typeWithoutDinner.addField("breakfast", DataType.STRING);
typeWithoutDinner.addField("lunch", DataType.INT);
// no dinner
docTypeManasjer.registerDocumentType(typeWithoutDinner);
Document docWithoutDinner = docTypeManasjer.createDocument(buf);
assertEquals(new StringFieldValue("peanut butter"), docWithoutDinner.getFieldValue("breakfast"));
assertEquals(new IntegerFieldValue(14), docWithoutDinner.getFieldValue("lunch"));
assertNull(docWithoutDinner.getFieldValue("dinner"));
}
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class Bug6425939TestCase method canDeserializeAnnotationsOnZeroLengthStrings.
@Test
public void canDeserializeAnnotationsOnZeroLengthStrings() {
StringFieldValue emptyString = new StringFieldValue("");
emptyString.setSpanTree(createSpanTree());
GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
Field strField = new Field("flarn", DataType.STRING);
serializer.write(strField, emptyString);
buffer.flip();
// Should not throw exception if bug 6425939 is fixed:
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(man, buffer);
StringFieldValue deserializedString = new StringFieldValue();
deserializer.read(strField, deserializedString);
assertEquals("", deserializedString.getString());
SpanTree readTree = deserializedString.getSpanTree("SpanTree1");
assertNotNull(readTree);
}
Aggregations