Search in sources :

Example 11 with LongFieldValue

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

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

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

the class HexDecodeExpression method doExecute.

@Override
protected void doExecute(ExecutionContext ctx) {
    String input = String.valueOf(ctx.getValue());
    if (input.isEmpty()) {
        ctx.setValue(new LongFieldValue(Long.MIN_VALUE));
        return;
    }
    BigInteger output;
    try {
        output = new BigInteger(input, 16);
    } catch (NumberFormatException e) {
        throw new NumberFormatException("Illegal hex value '" + input + "'.");
    }
    if (output.bitLength() > 64) {
        throw new NumberFormatException("Hex value '" + input + "' is out of range.");
    }
    if (output.compareTo(BigInteger.ZERO) == 1 && output.bitLength() == 64) {
        // flip to negative
        output = output.subtract(ULONG_MAX);
    }
    ctx.setValue(new LongFieldValue(output.longValue()));
}
Also used : BigInteger(java.math.BigInteger) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue)

Example 14 with LongFieldValue

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

the class OptimizePredicateTestCase method requireThatPredicateOptionsAreSet.

@Test
public void requireThatPredicateOptionsAreSet() {
    final Predicate predicate = Mockito.mock(Predicate.class);
    final PredicateFieldValue input = new PredicateFieldValue(predicate);
    ExecutionContext ctx = new ExecutionContext().setValue(input).setVariable("arity", new IntegerFieldValue(10));
    new OptimizePredicateExpression((predicate1, options) -> {
        assertEquals(10, options.getArity());
        assertEquals(0x8000000000000000L, options.getLowerBound());
        assertEquals(0x7fffffffffffffffL, options.getUpperBound());
        return predicate1;
    }).execute(ctx);
    ctx.setVariable("upper_bound", new LongFieldValue(1000));
    ctx.setVariable("lower_bound", new LongFieldValue(0));
    new OptimizePredicateExpression((value, options) -> {
        assertEquals(10, options.getArity());
        assertEquals(0, options.getLowerBound());
        assertEquals(1000, options.getUpperBound());
        return value;
    }).execute(ctx);
}
Also used : Mockito(org.mockito.Mockito) ExpressionAssert.assertVerifyCtx(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtx) Test(org.junit.Test) DataType(com.yahoo.document.DataType) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ExpressionAssert.assertVerifyCtxThrows(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyCtxThrows) FieldValue(com.yahoo.document.datatypes.FieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) Assert(org.junit.Assert) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ExpressionAssert.assertVerifyThrows(com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows) Predicate(com.yahoo.document.predicate.Predicate) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Predicate(com.yahoo.document.predicate.Predicate) Test(org.junit.Test)

Example 15 with LongFieldValue

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

the class GuardTestCase method requireThatConstFieldsAreIncludedByDocument.

@Test
public void requireThatConstFieldsAreIncludedByDocument() throws ParseException {
    DocumentType docType = new DocumentType("my_input");
    docType.addField(new Field("my_lng", DataType.LONG));
    docType.addField(new Field("my_str", DataType.STRING));
    Document doc = new Document(docType, "doc:scheme:");
    doc.setFieldValue("my_str", new StringFieldValue("foo"));
    assertNotNull(doc = Expression.execute(Expression.fromString("guard { now | attribute my_lng }"), doc));
    assertTrue(doc.getFieldValue("my_lng") instanceof LongFieldValue);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Test(org.junit.Test)

Aggregations

LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)25 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)17 Test (org.junit.Test)17 FieldValue (com.yahoo.document.datatypes.FieldValue)9 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)8 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)7 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)6 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)6 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)6 Raw (com.yahoo.document.datatypes.Raw)4 WeightedSet (com.yahoo.document.datatypes.WeightedSet)4 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 Array (com.yahoo.document.datatypes.Array)3 ByteBuffer (java.nio.ByteBuffer)3 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)2 PredicateFieldValue (com.yahoo.document.datatypes.PredicateFieldValue)2 FileOutputStream (java.io.FileOutputStream)2 DataType (com.yahoo.document.DataType)1 AbstractTypesTest (com.yahoo.document.annotation.AbstractTypesTest)1 Struct (com.yahoo.document.datatypes.Struct)1