Git Product home page Git Product logo

Comments (3)

BrianNichols avatar BrianNichols commented on June 17, 2024

Java supports automatic int/long conversion to double, but this is not true for their object counterparts (Integer,Long to Double).

Double d1 = (Integer)5;  // does not compile
Double d2 = (Double)(Integer)5;  // does not compile
double d3 = (Integer)5; // works by converting Integer to native int which automatically converts to native double.

Since the record contains objects (not native types), you will need to check the type of the object and convert yourself.

Map<String, Object> vals = new HashMap<>();
vals.put("value", 5);
Record record = new Record(vals, 1, 1);
Object obj = record.getValue("value");
double dbl = 0.0;

if (obj instanceof Long) {
    dbl = (Long)obj;
}
else if (obj instanceof Integer) {
    dbl = (Integer)obj;
}
else {
    dbl = (Double)obj;
}
System.out.println(dbl);

Another alternative is to always write the bin as double:

int val = 5;
client.put(null, key, new Bin("bin", (double)val));

from aerospike-client-java.

SnoopInf avatar SnoopInf commented on June 17, 2024

Right, that's exactly what i have to do now.
Solution with writing always double values is something i initially requested from DMP team, but they don't use java driver and for some reason solution which they use inserts integers even if you cast them to double.

@Test
    public void testGetDoubleWhenIntInserted() {
        Map<String, Object> vals = new HashMap<>();
        vals.put("value", 5);
        Record record = new Record(vals, 1, 1);
        Object o = record.getValue("value");
        double dbl = 0.0;
        if (o instanceof Integer) {
            dbl = (Integer)o;
        } else if (o instanceof Long) {
            dbl = (Long)o;
        } else {
            dbl = (Double)o;
        }
        System.out.println(dbl);
    }
}

We could also use getDouble() after integer check, since double values are anyways cast to long inside the driver

public double getDouble(String name) {
        Object result = this.getValue(name);
        return result instanceof Double?((Double)result).doubleValue():(result != null?Double.longBitsToDouble(((Long)result).longValue()):0.0D);
    }

I just wonder if it's possible to do type check for Integer here right near type check for double? Something like this:

public double getDouble(String name) {
        Object result = this.getValue(name);
        return result instanceof Double ? (Double) result : 
                result instanceof Integer ? ((Integer)(result)).doubleValue() : 
                        (result != null?Double.longBitsToDouble((Long) result):0.0D);
        
    }

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on June 17, 2024

The server only stores/returns 8 byte integers for integer bins, so it's not necessary to check for Integer. Only need to check for Double/Long.

The problem is that the java client used to store doubles in a long type (which happens to also need 8 bytes of storage) when the server did not support a double type. The longBitsToDouble() conversions works for this case, but this conversion does not work when the bits are a real long value. The function for that case is:

public double getDouble(String name) {
    Object result = this.getValue(name);
    return (result instanceof Double)? (Double)result : (result != null)? (Long)result : 0.0;      
}

We are concerned that removing longBitsToDouble() would break code for databases that still have double bits stored as long bits. The change to store doubles as new double type was only made 9 months ago, and we would like to keep this functionality intact for at least a year.

In the meantime, you can add this function to another class and then call that.

from aerospike-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.