Search in sources :

Example 6 with DoubleFieldValue

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

the class DocumentCalculatorTestCase method setUp.

public void setUp() {
    docMan = new DocumentTypeManager();
    testDocType = new DocumentType("testdoc");
    testDocType.addHeaderField("byteattr", DataType.BYTE);
    testDocType.addHeaderField("intattr", DataType.INT);
    testDocType.addHeaderField("longattr", DataType.LONG);
    testDocType.addHeaderField("doubleattr", DataType.DOUBLE);
    testDocType.addHeaderField("missingattr", DataType.INT);
    docMan.registerDocumentType(testDocType);
    doc = new Document(testDocType, new DocumentId("doc:testdoc:http://www.ntnu.no/"));
    doc.setFieldValue(testDocType.getField("byteattr"), new ByteFieldValue((byte) 32));
    doc.setFieldValue(testDocType.getField("intattr"), new IntegerFieldValue(468));
    doc.setFieldValue(testDocType.getField("longattr"), new LongFieldValue((long) 327));
    doc.setFieldValue(testDocType.getField("doubleattr"), new DoubleFieldValue(25.0));
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue)

Example 7 with DoubleFieldValue

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

the class DocumentSerializationTestCase method testSerializationAllVersions.

@Test
public void testSerializationAllVersions() throws IOException {
    DocumentType docInDocType = new DocumentType("docindoc");
    docInDocType.addField(new Field("stringindocfield", DataType.STRING, false));
    DocumentType docType = new DocumentType("serializetest");
    docType.addField(new Field("floatfield", DataType.FLOAT, true));
    docType.addField(new Field("stringfield", DataType.STRING, true));
    docType.addField(new Field("longfield", DataType.LONG, true));
    docType.addField(new Field("urifield", DataType.URI, true));
    docType.addField(new Field("intfield", DataType.INT, false));
    docType.addField(new Field("rawfield", DataType.RAW, false));
    docType.addField(new Field("doublefield", DataType.DOUBLE, false));
    docType.addField(new Field("bytefield", DataType.BYTE, false));
    DataType arrayOfFloatDataType = new ArrayDataType(DataType.FLOAT);
    docType.addField(new Field("arrayoffloatfield", arrayOfFloatDataType, false));
    DataType arrayOfArrayOfFloatDataType = new ArrayDataType(arrayOfFloatDataType);
    docType.addField(new Field("arrayofarrayoffloatfield", arrayOfArrayOfFloatDataType, false));
    docType.addField(new Field("docfield", DataType.DOCUMENT, false));
    DataType weightedSetDataType = DataType.getWeightedSet(DataType.STRING, false, false);
    docType.addField(new Field("wsfield", weightedSetDataType, false));
    DocumentTypeManager docMan = new DocumentTypeManager();
    docMan.register(docInDocType);
    docMan.register(docType);
    String path = "src/test/serializeddocuments/";
    {
        Document doc = new Document(docType, "doc:serializetest:http://test.doc.id/");
        doc.setFieldValue("intfield", 5);
        doc.setFieldValue("floatfield", -9.23);
        doc.setFieldValue("stringfield", "This is a string.");
        doc.setFieldValue("longfield", new LongFieldValue(398420092938472983l));
        doc.setFieldValue("doublefield", new DoubleFieldValue(98374532.398820));
        doc.setFieldValue("bytefield", new ByteFieldValue(254));
        byte[] rawData = "RAW DATA".getBytes();
        assertEquals(8, rawData.length);
        doc.setFieldValue(docType.getField("rawfield"), new Raw(ByteBuffer.wrap("RAW DATA".getBytes())));
        Document docInDoc = new Document(docInDocType, "doc:serializetest:http://doc.in.doc/");
        docInDoc.setFieldValue("stringindocfield", "Elvis is dead");
        doc.setFieldValue(docType.getField("docfield"), docInDoc);
        Array<FloatFieldValue> floatArray = new Array<>(arrayOfFloatDataType);
        floatArray.add(new FloatFieldValue(1.0f));
        floatArray.add(new FloatFieldValue(2.0f));
        doc.setFieldValue("arrayoffloatfield", floatArray);
        WeightedSet<StringFieldValue> weightedSet = new WeightedSet<>(weightedSetDataType);
        weightedSet.put(new StringFieldValue("Weighted 0"), 50);
        weightedSet.put(new StringFieldValue("Weighted 1"), 199);
        doc.setFieldValue("wsfield", weightedSet);
        CompressionConfig noncomp = new CompressionConfig();
        CompressionConfig lz4comp = new CompressionConfig(CompressionType.LZ4);
        {
            doc.getDataType().getHeaderType().setCompressionConfig(noncomp);
            doc.getDataType().getBodyType().setCompressionConfig(noncomp);
            FileOutputStream fout = new FileOutputStream(path + "document-java-currentversion-uncompressed.dat", false);
            doc.serialize(fout);
            fout.close();
        }
        {
            doc.getDataType().getHeaderType().setCompressionConfig(lz4comp);
            doc.getDataType().getBodyType().setCompressionConfig(lz4comp);
            FileOutputStream fout = new FileOutputStream(path + "document-java-currentversion-lz4-9.dat", false);
            doc.serialize(fout);
            doc.getDataType().getHeaderType().setCompressionConfig(noncomp);
            doc.getDataType().getBodyType().setCompressionConfig(noncomp);
            fout.close();
        }
    }
    class TestDoc {

        String testFile;

        int version;

        TestDoc(String testFile, int version) {
            this.testFile = testFile;
            this.version = version;
        }
    }
    String cpppath = "src/tests/data/";
    List<TestDoc> tests = new ArrayList<>();
    tests.add(new TestDoc(path + "document-java-currentversion-uncompressed.dat", Document.SERIALIZED_VERSION));
    tests.add(new TestDoc(path + "document-java-currentversion-lz4-9.dat", Document.SERIALIZED_VERSION));
    tests.add(new TestDoc(path + "document-java-v8-uncompressed.dat", 8));
    tests.add(new TestDoc(cpppath + "document-cpp-currentversion-uncompressed.dat", 7));
    tests.add(new TestDoc(cpppath + "document-cpp-currentversion-lz4-9.dat", 7));
    tests.add(new TestDoc(cpppath + "document-cpp-v8-uncompressed.dat", 7));
    tests.add(new TestDoc(cpppath + "document-cpp-v7-uncompressed.dat", 7));
    tests.add(new TestDoc(cpppath + "serializev6.dat", 6));
    for (TestDoc test : tests) {
        File f = new File(test.testFile);
        FileInputStream fin = new FileInputStream(f);
        byte[] buffer = new byte[(int) f.length()];
        int pos = 0;
        int remaining = buffer.length;
        while (remaining > 0) {
            int read = fin.read(buffer, pos, remaining);
            assertFalse(read == -1);
            pos += read;
            remaining -= read;
        }
        System.err.println("Checking doc from file " + test.testFile);
        Document doc = new Document(DocumentDeserializerFactory.create42(docMan, GrowableByteBuffer.wrap(buffer)));
        System.err.println("Id: " + doc.getId());
        assertEquals(new IntegerFieldValue(5), doc.getFieldValue("intfield"));
        assertEquals(-9.23, ((FloatFieldValue) doc.getFieldValue("floatfield")).getFloat(), 1E-6);
        assertEquals(new StringFieldValue("This is a string."), doc.getFieldValue("stringfield"));
        assertEquals(new LongFieldValue(398420092938472983l), doc.getFieldValue("longfield"));
        assertEquals(98374532.398820, ((DoubleFieldValue) doc.getFieldValue("doublefield")).getDouble(), 1E-6);
        assertEquals(new ByteFieldValue((byte) 254), doc.getFieldValue("bytefield"));
        ByteBuffer bbuffer = ((Raw) doc.getFieldValue("rawfield")).getByteBuffer();
        if (!Arrays.equals("RAW DATA".getBytes(), bbuffer.array())) {
            System.err.println("Expected 'RAW DATA' but got '" + new String(bbuffer.array()) + "'.");
            assertTrue(false);
        }
        if (test.version > 6) {
            Document docInDoc = (Document) doc.getFieldValue("docfield");
            assertTrue(docInDoc != null);
            assertEquals(new StringFieldValue("Elvis is dead"), docInDoc.getFieldValue("stringindocfield"));
        }
        Array array = (Array) doc.getFieldValue("arrayoffloatfield");
        assertTrue(array != null);
        assertEquals(1.0f, ((FloatFieldValue) array.get(0)).getFloat(), 1E-6);
        assertEquals(2.0f, ((FloatFieldValue) array.get(1)).getFloat(), 1E-6);
        WeightedSet wset = (WeightedSet) doc.getFieldValue("wsfield");
        assertTrue(wset != null);
        assertEquals(Integer.valueOf(50), wset.get(new StringFieldValue("Weighted 0")));
        assertEquals(Integer.valueOf(199), wset.get(new StringFieldValue("Weighted 1")));
    }
}
Also used : ArrayList(java.util.ArrayList) Raw(com.yahoo.document.datatypes.Raw) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FileInputStream(java.io.FileInputStream) Array(com.yahoo.document.datatypes.Array) FileOutputStream(java.io.FileOutputStream) File(java.io.File) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test) AbstractTypesTest(com.yahoo.document.annotation.AbstractTypesTest)

Example 8 with DoubleFieldValue

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

the class SerializeAnnotationsTestCase method testSerializeAdvancedTree.

public void testSerializeAdvancedTree() throws IOException {
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("html", root);
    DataType positionType = docMan.getDataType("myposition");
    StructDataType cityDataType = (StructDataType) docMan.getDataType("annotation.city");
    AnnotationTypeRegistry registry = docMan.getAnnotationTypeRegistry();
    AnnotationType textType = registry.getType("text");
    AnnotationType beginTag = registry.getType("begintag");
    AnnotationType endTag = registry.getType("endtag");
    AnnotationType bodyType = registry.getType("body");
    AnnotationType paragraphType = registry.getType("paragraph");
    AnnotationType cityType = registry.getType("city");
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) docMan.getDataType("annotationreference<text>");
    Struct position = new Struct(positionType);
    position.setFieldValue("latitude", new DoubleFieldValue(37.774929));
    position.setFieldValue("longitude", new DoubleFieldValue(-122.419415));
    Annotation sanAnnotation = new Annotation(textType);
    Annotation franciscoAnnotation = new Annotation(textType);
    Struct positionWithRef = cityDataType.createFieldValue();
    positionWithRef.setFieldValue("position", position);
    Field referencesField = cityDataType.getField("references");
    Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
    refList.add(new AnnotationReference(annRefType, sanAnnotation));
    refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
    positionWithRef.setFieldValue(referencesField, refList);
    Annotation city = new Annotation(cityType, positionWithRef);
    AlternateSpanList paragraph = new AlternateSpanList();
    paragraph.addChildren(new ArrayList<SpanNode>(), 0);
    paragraph.setProbability(0, 0.9);
    paragraph.setProbability(1, 0.1);
    {
        Span span1 = new Span(6, 3);
        Span span2 = new Span(9, 10);
        Span span3 = new Span(19, 4);
        Span span4 = new Span(23, 4);
        paragraph.add(0, span1).add(0, span2).add(0, span3).add(0, span4);
        Span alt_span1 = new Span(6, 13);
        Span alt_span2 = new Span(19, 8);
        paragraph.add(1, alt_span1).add(1, alt_span2);
        tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(alt_span1, textType).annotate(alt_span2, bodyType).annotate(paragraph, paragraphType);
    }
    {
        Span span1 = new Span(0, 6);
        Span span2 = new Span(27, 9);
        Span span3 = new Span(36, 8);
        root.add(span1).add(paragraph).add(span2).add(span3);
        tree.annotate(span1, beginTag).annotate(span2, franciscoAnnotation).annotate(span3, endTag).annotate(root, bodyType).annotate(city);
    }
    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! call readFile() before writeFile()!
    ByteBuffer serializedFromFile = readFile("test_data_serialized_advanced");
    ByteBuffer serialized = writeFile(value, "test_data_serialized_advanced");
    assertEquals(serialized.limit(), serializedFromFile.limit());
    StringFieldValue valueFromFile = new StringFieldValue();
    DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
    deserializer.read(null, valueFromFile);
    assertEquals(value, valueFromFile);
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) DataType(com.yahoo.document.DataType) StructDataType(com.yahoo.document.StructDataType) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 9 with DoubleFieldValue

use of com.yahoo.document.datatypes.DoubleFieldValue 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")));
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Raw(com.yahoo.document.datatypes.Raw) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Aggregations

DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)9 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)7 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)6 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)6 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)6 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)5 Test (org.junit.Test)5 Array (com.yahoo.document.datatypes.Array)4 Raw (com.yahoo.document.datatypes.Raw)4 WeightedSet (com.yahoo.document.datatypes.WeightedSet)4 ByteBuffer (java.nio.ByteBuffer)4 FieldValue (com.yahoo.document.datatypes.FieldValue)3 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)2 FileOutputStream (java.io.FileOutputStream)2 DataType (com.yahoo.document.DataType)1 Field (com.yahoo.document.Field)1 StructDataType (com.yahoo.document.StructDataType)1 AbstractTypesTest (com.yahoo.document.annotation.AbstractTypesTest)1 Struct (com.yahoo.document.datatypes.Struct)1