Search in sources :

Example 1 with DoubleValue

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();
}
Also used : Blob(com.google.cloud.datastore.Blob) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) DoubleValue(com.google.cloud.datastore.DoubleValue) BooleanValue(com.google.cloud.datastore.BooleanValue) LongValue(com.google.cloud.datastore.LongValue) TimestampValue(com.google.cloud.datastore.TimestampValue) StringValue(com.google.cloud.datastore.StringValue) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with DoubleValue

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));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) KeyValue(com.google.cloud.datastore.KeyValue) GqlQuery(com.google.cloud.datastore.GqlQuery) DoubleValue(com.google.cloud.datastore.DoubleValue) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 3 with DoubleValue

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();
}
Also used : Blob(com.google.cloud.datastore.Blob) ImmutableMap(com.google.common.collect.ImmutableMap) DoubleValue(com.google.cloud.datastore.DoubleValue) BooleanValue(com.google.cloud.datastore.BooleanValue) LongValue(com.google.cloud.datastore.LongValue) TimestampValue(com.google.cloud.datastore.TimestampValue) StringValue(com.google.cloud.datastore.StringValue) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

DoubleValue (com.google.cloud.datastore.DoubleValue)3 LongValue (com.google.cloud.datastore.LongValue)3 Blob (com.google.cloud.datastore.Blob)2 BooleanValue (com.google.cloud.datastore.BooleanValue)2 StringValue (com.google.cloud.datastore.StringValue)2 TimestampValue (com.google.cloud.datastore.TimestampValue)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ObjectInputStream (java.io.ObjectInputStream)2 GqlQuery (com.google.cloud.datastore.GqlQuery)1 Key (com.google.cloud.datastore.Key)1 KeyFactory (com.google.cloud.datastore.KeyFactory)1 KeyValue (com.google.cloud.datastore.KeyValue)1 Value (com.google.cloud.datastore.Value)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1