Comments (3)
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.
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.
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)
- Get namespaces/sets by AerospikeClient.
- Streaming support for batch operations HOT 2
- rename Record to AerospikeRecord HOT 1
- View screenshot link is broken HOT 1
- Type Erasure in 5.1.11 HOT 7
- Fully support surrogates symbol in Strings HOT 5
- Wrong condition for aeropsike client connectivity HOT 6
- Add RetryListener HOT 7
- Security contact? HOT 2
- Aerospike Cluster Peer Invalid Node Docker HOT 3
- Node name is null HOT 1
- Error 20: Partition map empty ( Issue for java client ) HOT 8
- Add support for findBySinceUpdate using since_update in metadata HOT 2
- What is the difference between `BatchRecordArrayListener` and `RecordArrayListener`? HOT 2
- How to make async queries faster? HOT 3
- java.lang.NullPointerException in AerospikeClient.put() HOT 4
- Setters for Spring HOT 2
- Enable releases in github HOT 2
- Problem reading Date type with client version 7.X HOT 2
- When compiling lua, the client discards the exception which has the lua compilation error details. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aerospike-client-java.