Search in sources :

Example 36 with IntegerFieldValue

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

the class PositionTypeTestCase method requireThatAccessorsWork.

@Test
public void requireThatAccessorsWork() {
    Struct val = PositionDataType.valueOf(6, 9);
    assertEquals(new IntegerFieldValue(6), PositionDataType.getXValue(val));
    assertEquals(new IntegerFieldValue(9), PositionDataType.getYValue(val));
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 37 with IntegerFieldValue

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

the class SpanTreeTestCase method testCopyAnnotatedString.

@Test
public void testCopyAnnotatedString() {
    StringFieldValue str = getAnnotatedString();
    StringFieldValue strCopy = str.clone();
    SpanTree balloooTree = str.getSpanTree("ballooo");
    AlternateSpanList root = (AlternateSpanList) balloooTree.getRoot();
    Span s1 = (Span) root.children(0).get(0);
    Span s2 = (Span) root.children(0).get(1);
    Span s3 = (Span) root.children(0).get(2);
    AlternateSpanList s4 = (AlternateSpanList) root.children(0).get(3);
    Span s5 = (Span) s4.children(0).get(0);
    Span s6 = (Span) s4.children(0).get(1);
    Span s7 = (Span) root.children(1).get(0);
    Span s8 = (Span) root.children(1).get(1);
    Span s9 = (Span) root.children(1).get(2);
    SpanTree balloooTreeCopy = strCopy.getSpanTree("ballooo");
    assertEquals(balloooTree, balloooTreeCopy);
    assertNotSame(balloooTree, balloooTreeCopy);
    AlternateSpanList rootCopy = (AlternateSpanList) balloooTreeCopy.getRoot();
    assertEquals(root, rootCopy);
    assertNotSame(root, rootCopy);
    Span s1Copy = (Span) rootCopy.children(0).get(0);
    assertEquals(s1, s1Copy);
    assertNotSame(s1, s1Copy);
    Span s2Copy = (Span) rootCopy.children(0).get(1);
    assertEquals(s2, s2Copy);
    assertNotSame(s2, s2Copy);
    Span s3Copy = (Span) rootCopy.children(0).get(2);
    assertEquals(s3, s3Copy);
    assertNotSame(s3, s3Copy);
    AlternateSpanList s4Copy = (AlternateSpanList) rootCopy.children(0).get(3);
    assertEquals(s4, s4Copy);
    assertNotSame(s4, s4Copy);
    Span s5Copy = (Span) s4Copy.children(0).get(0);
    assertEquals(s5, s5Copy);
    assertNotSame(s5, s5Copy);
    Span s6Copy = (Span) s4Copy.children(0).get(1);
    assertEquals(s6, s6Copy);
    assertNotSame(s6, s6Copy);
    Span s7Copy = (Span) rootCopy.children(1).get(0);
    assertEquals(s7, s7Copy);
    assertNotSame(s7, s7Copy);
    Span s8Copy = (Span) rootCopy.children(1).get(1);
    assertEquals(s8, s8Copy);
    assertNotSame(s8, s8Copy);
    Span s9Copy = (Span) rootCopy.children(1).get(2);
    assertEquals(s9, s9Copy);
    assertNotSame(s9, s9Copy);
    Iterator<Annotation> annotationsBalloooTree;
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : balloooTree) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationsBalloooTree = allAnnotationsList.iterator();
    }
    Annotation dummyAnnForS1 = annotationsBalloooTree.next();
    Annotation dummyAnnForS2 = annotationsBalloooTree.next();
    Annotation numberAnnForS2 = annotationsBalloooTree.next();
    IntegerFieldValue integerValForS2 = (IntegerFieldValue) numberAnnForS2.getFieldValue();
    Annotation motherAnnForS2 = annotationsBalloooTree.next();
    Struct motherValForS2 = (Struct) motherAnnForS2.getFieldValue();
    Annotation dummyAnnForS3 = annotationsBalloooTree.next();
    Annotation numberAnnForS3 = annotationsBalloooTree.next();
    IntegerFieldValue integerValForS3 = (IntegerFieldValue) numberAnnForS3.getFieldValue();
    Annotation dummyAnnForS5 = annotationsBalloooTree.next();
    Annotation daughterAnnForS6 = annotationsBalloooTree.next();
    Struct daughterValForS6 = (Struct) daughterAnnForS6.getFieldValue();
    AnnotationReference refFromS6ToMotherAnn = (AnnotationReference) daughterValForS6.getFieldValue("related");
    Iterator<Annotation> annotationsBalloooTreeCopy;
    {
        List<Annotation> allAnnotationsList = new ArrayList<>();
        for (Annotation an : balloooTreeCopy) {
            allAnnotationsList.add(an);
        }
        Collections.sort(allAnnotationsList);
        annotationsBalloooTreeCopy = allAnnotationsList.iterator();
    }
    Annotation dummyAnnForS1Copy = annotationsBalloooTreeCopy.next();
    assertEquals(dummyAnnForS1, dummyAnnForS1Copy);
    assertNotSame(dummyAnnForS1, dummyAnnForS1Copy);
    Annotation dummyAnnForS2Copy = annotationsBalloooTreeCopy.next();
    assertEquals(dummyAnnForS2, dummyAnnForS2Copy);
    assertNotSame(dummyAnnForS2, dummyAnnForS2Copy);
    Annotation numberAnnForS2Copy = annotationsBalloooTreeCopy.next();
    assertEquals(numberAnnForS2, numberAnnForS2Copy);
    assertNotSame(numberAnnForS2, numberAnnForS2Copy);
    IntegerFieldValue integerValForS2Copy = (IntegerFieldValue) numberAnnForS2Copy.getFieldValue();
    assertEquals(integerValForS2, integerValForS2Copy);
    assertNotSame(integerValForS2, integerValForS2Copy);
    Annotation motherAnnForS2Copy = annotationsBalloooTreeCopy.next();
    assertEquals(motherAnnForS2, motherAnnForS2Copy);
    assertNotSame(motherAnnForS2, motherAnnForS2Copy);
    Struct motherValForS2Copy = (Struct) motherAnnForS2Copy.getFieldValue();
    assertEquals(motherValForS2, motherValForS2Copy);
    assertNotSame(motherValForS2, motherValForS2Copy);
    Annotation dummyAnnForS3Copy = annotationsBalloooTreeCopy.next();
    assertEquals(dummyAnnForS3, dummyAnnForS3Copy);
    assertNotSame(dummyAnnForS3, dummyAnnForS3Copy);
    Annotation numberAnnForS3Copy = annotationsBalloooTreeCopy.next();
    assertEquals(numberAnnForS3, numberAnnForS3Copy);
    assertNotSame(numberAnnForS3, numberAnnForS3Copy);
    IntegerFieldValue integerValForS3Copy = (IntegerFieldValue) numberAnnForS3Copy.getFieldValue();
    assertEquals(integerValForS3, integerValForS3Copy);
    assertNotSame(integerValForS3, integerValForS3Copy);
    Annotation dummyAnnForS5Copy = annotationsBalloooTreeCopy.next();
    assertEquals(dummyAnnForS5, dummyAnnForS5Copy);
    assertNotSame(dummyAnnForS5, dummyAnnForS5Copy);
    Annotation daughterAnnForS6Copy = annotationsBalloooTreeCopy.next();
    assertEquals(daughterAnnForS6, daughterAnnForS6Copy);
    assertNotSame(daughterAnnForS6, daughterAnnForS6Copy);
    Struct daughterValForS6Copy = (Struct) daughterAnnForS6Copy.getFieldValue();
    assertEquals(daughterValForS6, daughterValForS6Copy);
    assertNotSame(daughterValForS6, daughterValForS6Copy);
    AnnotationReference refFromS6ToMotherAnnCopy = (AnnotationReference) daughterValForS6Copy.getFieldValue("related");
    assertEquals(refFromS6ToMotherAnn, refFromS6ToMotherAnnCopy);
    assertNotSame(refFromS6ToMotherAnn, refFromS6ToMotherAnnCopy);
    assertEquals(str, strCopy);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 38 with IntegerFieldValue

use of com.yahoo.document.datatypes.IntegerFieldValue 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 39 with IntegerFieldValue

use of com.yahoo.document.datatypes.IntegerFieldValue 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 40 with IntegerFieldValue

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

the class MapValueUpdate method applyTo.

@Override
public FieldValue applyTo(FieldValue fval) {
    if (fval instanceof Array) {
        Array array = (Array) fval;
        FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
        element = update.applyTo(element);
        array.set(((IntegerFieldValue) value).getInteger(), element);
    } else if (fval instanceof WeightedSet) {
        WeightedSet wset = (WeightedSet) fval;
        WeightedSetDataType wtype = wset.getDataType();
        Integer weight = wset.get(value);
        if (weight == null) {
            if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
                weight = 0;
            } else {
                return fval;
            }
        }
        weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
        wset.put(value, weight);
        if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
            wset.remove(value);
        }
    }
    return fval;
}
Also used : Array(com.yahoo.document.datatypes.Array) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Aggregations

IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)69 Test (org.junit.Test)56 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)39 FieldValue (com.yahoo.document.datatypes.FieldValue)23 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)14 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)13 Array (com.yahoo.document.datatypes.Array)12 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)10 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)10 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)10 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)10 Struct (com.yahoo.document.datatypes.Struct)10 Field (com.yahoo.document.Field)8 DocumentType (com.yahoo.document.DocumentType)7 WeightedSet (com.yahoo.document.datatypes.WeightedSet)7 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)7 Document (com.yahoo.document.Document)5 DocumentUpdate (com.yahoo.document.DocumentUpdate)5 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)4 PredicateFieldValue (com.yahoo.document.datatypes.PredicateFieldValue)4