aerospike / aerospike-client-java

Aerospike Java Client Library
Other
236 stars 212 forks source link

unboxed double value comes back as Long #51

Closed daveyostcom closed 8 years ago

daveyostcom commented 9 years ago
package com.aerospike.examples;

import com.aerospike.client.*;

public class DoubleBinTest {

  public static void main(String[] args) throws Exception {
    String host = "localhost";
    int    port = 3000;
    AerospikeClient client = new AerospikeClient(null, host, port);

    String namespace = "test";
    String set       = "putget";
    Key    key       = new Key(namespace, set, "foo");

    Bin testBin1  = new Bin("Double", new Double(0.5)); // works
    Bin testBin2  = new Bin("double", 0.5);  // fails
    client.put(null, key, testBin1, testBin2);

    Record record1 = client.get(null, key);

    Object result1 = record1.getValue("Double");
    System.out.println("Result Double bin value is " + result1);

    Object result2 = record1.getValue("double");
    if (result2 != null) {
      System.out.println("Result double bin type should be java.lang.Double");
      System.out.println("Result double bin type  is " + result2.getClass()); // class java.lang.Long
      System.out.println("Result double bin value is " + result2);            // 4602678819172646912
    }

    client.delete(null, key);
  }
}

gives this result:

Result double bin value is 0.5
Result Double bin type should be java.lang.Double
Result Double bin type  is class java.lang.Long
Result Double bin value is 4602678819172646912
BrianNichols commented 9 years ago

The double is being stored as a long (for compatibility with older servers). To store double natively, set once:

Value.UseDoubleType = true;

In any case, you should always use Record.getDouble() to retrieve a double (which works when server value is stored as double or long).

bbulkow commented 9 years ago

How can I configure the client to always use the double type, instead of setting it on every value ?

BrianNichols commented 9 years ago

UseDoubleType is a static variable that only has to be set once at program startup. It does not need to be set on every value.

Double support on the server side is relatively new. When servers that support doubles have been available long enough, we will change UseDoubleType default to true on the client.

BrianNichols commented 8 years ago

The latest java client (3.2.3) now defaults UseDoubleType to true.