Search in sources :

Example 1 with DoubleFieldValue

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

the class ToDoubleTestCase method requireThatValueIsConverted.

@Test
public void requireThatValueIsConverted() {
    ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
    ctx.setValue(new StringFieldValue("6.9")).execute(new ToDoubleExpression());
    FieldValue val = ctx.getValue();
    assertTrue(val instanceof DoubleFieldValue);
    assertEquals(6.9, ((DoubleFieldValue) val).getDouble(), 1e-6);
}
Also used : SimpleTestAdapter(com.yahoo.vespa.indexinglanguage.SimpleTestAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) Test(org.junit.Test)

Example 2 with DoubleFieldValue

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

the class DocumentTestCase method testCppDocSplit.

@Test
public void testCppDocSplit() throws IOException {
    docMan = setUpCppDocType();
    byte[] headerData = readFile("src/test/document/serializecppsplit_header.dat");
    byte[] bodyData = readFile("src/test/document/serializecppsplit_body.dat");
    DocumentDeserializer header = DocumentDeserializerFactory.create42(docMan, GrowableByteBuffer.wrap(headerData), GrowableByteBuffer.wrap(bodyData));
    Document doc = new Document(header);
    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((byte) -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());
    WeightedSet wset = (WeightedSet) 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) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet) Test(org.junit.Test)

Example 3 with DoubleFieldValue

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

the class DocumentTestCase method testTypeChecking.

@Test
public void testTypeChecking() {
    DocumentType type = new DocumentType("test");
    type.addField(new Field("double", DataType.DOUBLE));
    type.addField(new Field("float", DataType.FLOAT));
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("long", DataType.LONG));
    type.addField(new Field("string", DataType.STRING));
    Document doc = new Document(type, "doc:scheme:");
    FieldValue stringVal = new StringFieldValue("69");
    FieldValue doubleVal = new DoubleFieldValue(6.9);
    FieldValue floatVal = new FloatFieldValue(6.9f);
    FieldValue intVal = new IntegerFieldValue(69);
    FieldValue longVal = new LongFieldValue(69L);
    doc.setFieldValue("string", stringVal);
    doc.setFieldValue("string", doubleVal);
    doc.setFieldValue("string", floatVal);
    doc.setFieldValue("string", intVal);
    doc.setFieldValue("string", longVal);
    doc.setFieldValue("double", stringVal);
    doc.setFieldValue("double", doubleVal);
    doc.setFieldValue("double", floatVal);
    doc.setFieldValue("double", intVal);
    doc.setFieldValue("double", longVal);
    doc.setFieldValue("float", stringVal);
    doc.setFieldValue("float", doubleVal);
    doc.setFieldValue("float", floatVal);
    doc.setFieldValue("float", intVal);
    doc.setFieldValue("float", longVal);
    doc.setFieldValue("int", stringVal);
    doc.setFieldValue("int", doubleVal);
    doc.setFieldValue("int", floatVal);
    doc.setFieldValue("int", intVal);
    doc.setFieldValue("int", longVal);
    doc.setFieldValue("long", stringVal);
    doc.setFieldValue("long", doubleVal);
    doc.setFieldValue("long", floatVal);
    doc.setFieldValue("long", intVal);
    doc.setFieldValue("long", longVal);
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) Test(org.junit.Test)

Example 4 with DoubleFieldValue

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

Example 5 with DoubleFieldValue

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

the class DocumentCalculatorTestCase method testFieldDivideByZero.

public void testFieldDivideByZero() throws Exception {
    try {
        DocumentCalculator calculator = new DocumentCalculator("(testdoc.byteattr + testdoc.intattr) / testdoc.doubleattr");
        doc.setFieldValue("doubleattr", new DoubleFieldValue(0.0));
        calculator.evaluate(doc, new HashMap<String, Object>());
        assertTrue(false);
    } catch (IllegalArgumentException e) {
    // ok
    }
}
Also used : DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue)

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