Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update query-related datastore code to v1beta3 #212

Merged
merged 5 commits into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.services.datastore.DatastoreV1;
import com.google.api.services.datastore.DatastoreV1.Value;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.protobuf.ByteString;
Expand All @@ -37,7 +35,7 @@
*
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
*/
public final class Blob extends Serializable<DatastoreV1.Value> {
public final class Blob extends Serializable<com.google.datastore.v1beta3.Value> {

private static final long serialVersionUID = 3835421019618247721L;

Expand Down Expand Up @@ -146,12 +144,12 @@ public static Blob copyFrom(InputStream input) throws IOException {
}

@Override
protected Value toPb() {
return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build();
protected com.google.datastore.v1beta3.Value toPb() {
return com.google.datastore.v1beta3.Value.newBuilder().setBlobValue(byteString).build();
}

@Override
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
return new Blob(DatastoreV1.Value.parseFrom(bytesPb).getBlobValue());
return new Blob(com.google.datastore.v1beta3.Value.parseFrom(bytesPb).getBlobValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.services.datastore.DatastoreV1;
import com.google.api.services.datastore.DatastoreV1.Value;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Preconditions;
Expand All @@ -37,7 +35,7 @@
* A Google Cloud Datastore cursor.
* The cursor can be used to as a starting point or an ending point for a {@link Query}
*/
public final class Cursor extends Serializable<DatastoreV1.Value> {
public final class Cursor extends Serializable<com.google.datastore.v1beta3.Value> {

private static final long serialVersionUID = -1423744878777486541L;

Expand Down Expand Up @@ -89,7 +87,8 @@ public String toUrlSafe() {
public static Cursor fromUrlSafe(String urlSafe) {
try {
String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name());
DatastoreV1.Value.Builder builder = DatastoreV1.Value.newBuilder();
com.google.datastore.v1beta3.Value.Builder builder =
com.google.datastore.v1beta3.Value.newBuilder();
TextFormat.merge(utf8Str, builder);
return fromPb(builder.build());
} catch (UnsupportedEncodingException | ParseException e) {
Expand All @@ -102,16 +101,16 @@ public static Cursor copyFrom(byte[] bytes) {
}

@Override
protected Value toPb() {
return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build();
protected com.google.datastore.v1beta3.Value toPb() {
return com.google.datastore.v1beta3.Value.newBuilder().setBlobValue(byteString).build();
}

@Override
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
return fromPb(DatastoreV1.Value.parseFrom(bytesPb));
return fromPb(com.google.datastore.v1beta3.Value.parseFrom(bytesPb));
}

static Cursor fromPb(DatastoreV1.Value valuePb) {
static Cursor fromPb(com.google.datastore.v1beta3.Value valuePb) {
return new Cursor(valuePb.getBlobValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public <T> QueryResults<T> run(Query<T> query) {
}

<T> QueryResults<T> run(com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query<T> query) {
// TODO(ajaykannan): fix me!
//return new QueryResultsImpl<>(this, readOptionsPb, query);
return null; // TODO(ajaykannan): fix me!
return new QueryResultsImpl<>(this, readOptionsPb, query);
}

com.google.datastore.v1beta3.RunQueryResponse runQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gcloud.datastore.Validator.validateNamespace;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -75,25 +74,22 @@ public final class GqlQuery<V> extends Query<V> {

private final transient String queryString;
private final transient boolean allowLiteral;
private final transient ImmutableList<Binding> namedBindings;
private final transient ImmutableMap<String, Binding> namedBindings;
private final transient ImmutableList<Binding> positionalBindings;

static final class Binding extends Serializable<DatastoreV1.GqlQueryArg> {
static final class Binding extends Serializable<com.google.datastore.v1beta3.GqlQueryParameter> {

private static final long serialVersionUID = 1976895435257636275L;

private final transient String name;
private final transient Cursor cursor;
private final transient Value<?> value;

Binding(String name, Cursor cursor) {
this.name = name;
Binding(Cursor cursor) {
this.cursor = checkNotNull(cursor);
value = null;
}

Binding(String name, Value<?> value) {
this.name = name;
Binding(Value<?> value) {
this.value = checkNotNull(value);
cursor = null;
}
Expand All @@ -102,13 +98,9 @@ Object cursorOrValue() {
return MoreObjects.firstNonNull(cursor, value);
}

String name() {
return name;
}

@Override
public int hashCode() {
return Objects.hash(name, cursor, value);
return Objects.hash(cursor, value);
}

@Override
Expand All @@ -120,40 +112,36 @@ public boolean equals(Object obj) {
return false;
}
Binding other = (Binding) obj;
return Objects.equals(name, other.name)
&& Objects.equals(cursor, other.cursor)
&& Objects.equals(value, other.value);
return Objects.equals(cursor, other.cursor) && Objects.equals(value, other.value);
}

@Override
protected DatastoreV1.GqlQueryArg toPb() {
DatastoreV1.GqlQueryArg.Builder argPb = DatastoreV1.GqlQueryArg.newBuilder();
if (name != null) {
argPb.setName(name);
}
protected com.google.datastore.v1beta3.GqlQueryParameter toPb() {
com.google.datastore.v1beta3.GqlQueryParameter.Builder argPb =
com.google.datastore.v1beta3.GqlQueryParameter.newBuilder();
if (cursor != null) {
argPb.setCursor(cursor.byteString());
}
if (value != null) {
// TODO(ajaykannan): fix me!
//argPb.setValue(value.toPb());
argPb.setValue(value.toPb());
}
return argPb.build();
}

@Override
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
return fromPb(DatastoreV1.GqlQueryArg.parseFrom(bytesPb));
return fromPb(com.google.datastore.v1beta3.GqlQueryParameter.parseFrom(bytesPb));
}

static Binding fromPb(DatastoreV1.GqlQueryArg argPb) {
String name = argPb.hasName() ? argPb.getName() : null;
if (argPb.hasCursor()) {
return new Binding(name, new Cursor(argPb.getCursor()));
static Binding fromPb(com.google.datastore.v1beta3.GqlQueryParameter argPb) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

switch (argPb.getParameterTypeCase()) {
case CURSOR:
return new Binding(new Cursor(argPb.getCursor()));
case VALUE:
return new Binding(Value.fromPb(argPb.getValue()));
default:
throw new AssertionError("Unexpected enum value " + argPb.getParameterTypeCase());
}
// TODO(ajaykannan): fix me!
//return new Binding(name, Value.fromPb(argPb.getValue()));
return new Binding(name, new Cursor(null)); // TODO(ajaykannan): fix me!
}
}

Expand Down Expand Up @@ -196,52 +184,52 @@ public Builder<V> clearBindings() {
}

public Builder<V> setBinding(String name, Cursor cursor) {
namedBindings.put(name, new Binding(name, cursor));
namedBindings.put(name, new Binding(cursor));
return this;
}

public Builder<V> setBinding(String name, String... value) {
namedBindings.put(name, toBinding(name, StringValue.MARSHALLER, Arrays.asList(value)));
namedBindings.put(name, toBinding(StringValue.MARSHALLER, Arrays.asList(value)));
return this;
}

public Builder<V> setBinding(String name, long... value) {
namedBindings.put(name, toBinding(name, LongValue.MARSHALLER, Longs.asList(value)));
namedBindings.put(name, toBinding(LongValue.MARSHALLER, Longs.asList(value)));
return this;
}

public Builder<V> setBinding(String name, double... value) {
namedBindings.put(name, toBinding(name, DoubleValue.MARSHALLER, Doubles.asList(value)));
namedBindings.put(name, toBinding(DoubleValue.MARSHALLER, Doubles.asList(value)));
return this;
}

public Builder<V> setBinding(String name, boolean... value) {
namedBindings.put(name, toBinding(name, BooleanValue.MARSHALLER, Booleans.asList(value)));
namedBindings.put(name, toBinding(BooleanValue.MARSHALLER, Booleans.asList(value)));
return this;
}

public Builder<V> setBinding(String name, DateTime... value) {
namedBindings.put(name, toBinding(name, DateTimeValue.MARSHALLER, Arrays.asList(value)));
namedBindings.put(name, toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value)));
return this;
}

public Builder<V> setBinding(String name, Key... value) {
namedBindings.put(name, toBinding(name, KeyValue.MARSHALLER, Arrays.asList(value)));
namedBindings.put(name, toBinding(KeyValue.MARSHALLER, Arrays.asList(value)));
return this;
}

public Builder<V> setBinding(String name, FullEntity<?>... value) {
namedBindings.put(name, toBinding(name, EntityValue.MARSHALLER, Arrays.asList(value)));
namedBindings.put(name, toBinding(EntityValue.MARSHALLER, Arrays.asList(value)));
return this;
}

public Builder<V> setBinding(String name, Blob... value) {
namedBindings.put(name, toBinding(name, BlobValue.MARSHALLER, Arrays.asList(value)));
namedBindings.put(name, toBinding(BlobValue.MARSHALLER, Arrays.asList(value)));
return this;
}

public Builder<V> addBinding(Cursor cursor) {
positionalBindings.add(new Binding(null, cursor));
positionalBindings.add(new Binding(cursor));
return this;
}

Expand Down Expand Up @@ -289,11 +277,7 @@ public GqlQuery<V> build() {
return new GqlQuery<>(this);
}

private static Binding toBinding(Value.BuilderFactory<?, ?, ?> builderFactory, List<?> values) {
return toBinding(null, builderFactory, values);
}

private static <V> Binding toBinding(String name, Value.BuilderFactory<V, ?, ?> builderFactory,
private static <V> Binding toBinding(Value.BuilderFactory<V, ?, ?> builderFactory,
List<?> values) {
List<Value<V>> list = new ArrayList<>(values.size());
for (Object object : values) {
Expand All @@ -309,15 +293,15 @@ private static <V> Binding toBinding(String name, Value.BuilderFactory<V, ?, ?>
} else {
value = new ListValue(list);
}
return new Binding(name, value);
return new Binding(value);
}
}

private GqlQuery(Builder<V> builder) {
super(builder.resultType, builder.namespace);
queryString = builder.queryString;
allowLiteral = builder.allowLiteral;
namedBindings = ImmutableList.copyOf(builder.namedBindings.values());
namedBindings = ImmutableMap.copyOf(builder.namedBindings);
positionalBindings = ImmutableList.copyOf(builder.positionalBindings);
}

Expand All @@ -334,8 +318,8 @@ public boolean allowLiteral() {
*/
public Map<String, Object> namedBindings() {
ImmutableMap.Builder<String, Object> builder = ImmutableSortedMap.naturalOrder();
for (Binding binding : namedBindings) {
builder.put(binding.name(), binding.cursorOrValue());
for (Map.Entry<String, Binding> binding : namedBindings.entrySet()) {
builder.put(binding.getKey(), binding.getValue().cursorOrValue());
}
return builder.build();
}
Expand Down Expand Up @@ -373,50 +357,53 @@ public boolean equals(Object obj) {
}

@Override
protected DatastoreV1.GqlQuery toPb() {
DatastoreV1.GqlQuery.Builder queryPb = DatastoreV1.GqlQuery.newBuilder();
protected com.google.datastore.v1beta3.GqlQuery toPb() {
com.google.datastore.v1beta3.GqlQuery.Builder queryPb =
com.google.datastore.v1beta3.GqlQuery.newBuilder();
queryPb.setQueryString(queryString);
queryPb.setAllowLiteral(allowLiteral);
for (Binding argument : namedBindings) {
queryPb.addNameArg(argument.toPb());
queryPb.setAllowLiterals(allowLiteral);
Map<String, com.google.datastore.v1beta3.GqlQueryParameter> namedBindingsPb =
queryPb.getMutableNamedBindings();
for (Map.Entry<String, Binding> entry : namedBindings.entrySet()) {
namedBindingsPb.put(entry.getKey(), entry.getValue().toPb());
}
for (Binding argument : positionalBindings) {
queryPb.addNumberArg(argument.toPb());
queryPb.addPositionalBindings(argument.toPb());
}
return queryPb.build();
}

@Override
protected void populatePb(DatastoreV1.RunQueryRequest.Builder requestPb) {
protected void populatePb(com.google.datastore.v1beta3.RunQueryRequest.Builder requestPb) {
requestPb.setGqlQuery(toPb());
}

@Override
protected GqlQuery<V> nextQuery(DatastoreV1.QueryResultBatch responsePb) {
protected GqlQuery<V> nextQuery(com.google.datastore.v1beta3.QueryResultBatch responsePb) {

This comment was marked as spam.

This comment was marked as spam.

// See issue #17
throw new UnsupportedOperationException("paging for this query is not implemented yet");
}

@Override
protected Object fromPb(ResultType<V> resultType, String namespace, byte[] bytesPb)
throws InvalidProtocolBufferException {
return fromPb(resultType, namespace, DatastoreV1.GqlQuery.parseFrom(bytesPb));
return fromPb(resultType, namespace, com.google.datastore.v1beta3.GqlQuery.parseFrom(bytesPb));
}

private static <V> GqlQuery<V> fromPb(
ResultType<V> resultType, String ns, DatastoreV1.GqlQuery queryPb) {
ResultType<V> resultType, String ns, com.google.datastore.v1beta3.GqlQuery queryPb) {
Builder<V> builder = new Builder<>(resultType, queryPb.getQueryString());
builder.namespace(ns);
if (queryPb.hasAllowLiteral()) {
builder.allowLiteral = queryPb.getAllowLiteral();
}
for (DatastoreV1.GqlQueryArg nameArg : queryPb.getNameArgList()) {
Binding argument = Binding.fromPb(nameArg);
builder.namedBindings.put(argument.name(), argument);
}
for (DatastoreV1.GqlQueryArg numberArg : queryPb.getNumberArgList()) {
Binding argument = Binding.fromPb(numberArg);
builder.positionalBindings.add(argument);
builder.allowLiteral = queryPb.getAllowLiterals();
for (Map.Entry<String, com.google.datastore.v1beta3.GqlQueryParameter> nameArg
: queryPb.getNamedBindings().entrySet()) {
Binding currBinding = Binding.fromPb(nameArg.getValue());
builder.namedBindings.put(nameArg.getKey(), currBinding);
}
for (com.google.datastore.v1beta3.GqlQueryParameter numberArg
: queryPb.getPositionalBindingsList()) {
Binding currBinding = Binding.fromPb(numberArg);
builder.positionalBindings.add(currBinding);
}
return builder.build();
}
Expand Down
Loading