use of com.google.cloud.datastore.DoubleValue in project data-transfer-project by google.
the class GoogleJobStore method getProperties.
private static Map<String, Object> getProperties(Entity entity) {
if (entity == null)
return null;
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
for (String property : entity.getNames()) {
// builder.put(property, entity.getValue(property));
if (entity.getValue(property) instanceof StringValue) {
builder.put(property, (String) entity.getString(property));
} else if (entity.getValue(property) instanceof LongValue) {
// This conversion is safe because of integer to long conversion above
builder.put(property, new Long(entity.getLong(property)).intValue());
} else if (entity.getValue(property) instanceof DoubleValue) {
builder.put(property, (Double) entity.getDouble(property));
} else if (entity.getValue(property) instanceof BooleanValue) {
builder.put(property, (Boolean) entity.getBoolean(property));
} else if (entity.getValue(property) instanceof TimestampValue) {
builder.put(property, (Timestamp) entity.getTimestamp(property));
} else {
Blob blob = entity.getBlob(property);
Object obj = null;
try {
try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
try {
obj = in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
// BlobValue
builder.put(property, obj);
}
}
return builder.build();
}
use of com.google.cloud.datastore.DoubleValue in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQueryTests method compoundNameConventionTest.
@Test
public void compoundNameConventionTest() {
String gql = "SELECT * FROM " + "|org.springframework.cloud.gcp.data.datastore." + "repository.query.GqlDatastoreQueryTests$Trade|" + " WHERE price=:#{#tag6 * -1} AND price<>:#{#tag6 * -1} OR " + "price<>:#{#tag7 * -1} AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3;";
String entityResolvedGql = "SELECT * FROM trades" + " WHERE price=@SpELtag1 AND price<>@SpELtag2 OR price<>@SpELtag3 AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3";
Trade trade = new Trade();
trade.id = "tradeId1";
Object[] paramVals = new Object[] { "BUY", "abcd", // this is an array param of the non-natively supported type and will need conversion
new int[] { 1, 2 }, new double[] { 8.88, 9.99 }, // this parameter is a simple int, which is not a directly supported type and uses
3, // conversions
"blahblah", 1.11, 2.22, trade };
String[] paramNames = new String[] { "tag0", "tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8" };
buildParameters(paramVals, paramNames);
KeyFactory keyFactory = new KeyFactory("proj");
keyFactory.setKind("kind");
Key key = keyFactory.newKey("tradeid1-key");
doReturn(key).when(this.datastoreTemplate).getKey(any());
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < paramVals.length; i++) {
evaluationContext.setVariable(paramNames[i], paramVals[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, false);
doAnswer((invocation) -> {
GqlQuery statement = invocation.getArgument(0);
assertThat(statement.getQueryString()).isEqualTo(entityResolvedGql);
Map<String, Value> paramMap = statement.getNamedBindings();
assertThat(paramMap.get("tag0").get()).isEqualTo(paramVals[0]);
assertThat(paramMap.get("tag1").get()).isEqualTo(paramVals[1]);
// custom conversion is expected to have been used in this param
assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(0))).get()).isEqualTo(1L);
assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(1))).get()).isEqualTo(2L);
double actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(0))).get();
assertThat(actual).isEqualTo(((double[]) paramVals[3])[0], DELTA);
actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(1))).get();
assertThat(actual).isEqualTo(((double[]) paramVals[3])[1], DELTA);
// 3L is expected even though 3 int was the original param due to custom conversions
assertThat(paramMap.get("tag4").get()).isEqualTo(3L);
assertThat(paramMap.get("tag5").get()).isEqualTo(paramVals[5]);
assertThat(paramMap.get("tag6").get()).isEqualTo(paramVals[6]);
assertThat(paramMap.get("tag7").get()).isEqualTo(paramVals[7]);
assertThat((double) paramMap.get("SpELtag1").get()).isEqualTo(-1 * (double) paramVals[6], DELTA);
assertThat((double) paramMap.get("SpELtag2").get()).isEqualTo(-1 * (double) paramVals[6], DELTA);
assertThat((double) paramMap.get("SpELtag3").get()).isEqualTo(-1 * (double) paramVals[7], DELTA);
assertThat(((KeyValue) paramMap.get("tag8")).get()).isSameAs(key);
return null;
}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
gqlDatastoreQuery.execute(paramVals);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
use of com.google.cloud.datastore.DoubleValue in project data-transfer-project by google.
the class GoogleJobStore method getProperties.
private static Map<String, Object> getProperties(Entity entity) throws IOException, ClassNotFoundException {
if (entity == null) {
return null;
}
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
for (String property : entity.getNames()) {
// builder.put(property, entity.getValue(property));
if (entity.getValue(property) instanceof StringValue) {
builder.put(property, (String) entity.getString(property));
} else if (entity.getValue(property) instanceof LongValue) {
// This conversion is safe because of integer to long conversion above
builder.put(property, new Long(entity.getLong(property)).intValue());
} else if (entity.getValue(property) instanceof DoubleValue) {
builder.put(property, (Double) entity.getDouble(property));
} else if (entity.getValue(property) instanceof BooleanValue) {
builder.put(property, (Boolean) entity.getBoolean(property));
} else if (entity.getValue(property) instanceof TimestampValue) {
builder.put(property, (Timestamp) entity.getTimestamp(property));
} else {
Blob blob = entity.getBlob(property);
Object obj = null;
try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
obj = in.readObject();
}
// BlobValue
builder.put(property, obj);
}
}
return builder.build();
}
Aggregations