Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Update query-related datastore code to v1beta3#212
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c029c3716afb0dacf2c420c2f9e935ea69aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| @@ -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; | ||
| } | ||
| @@ -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 | ||
| @@ -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) { | ||
| 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! | ||
| } | ||
| } | ||
| @@ -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; | ||
| } | ||
| @@ -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) { | ||
| @@ -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); | ||
| } | ||
| @@ -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(); | ||
| } | ||
| @@ -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.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| // 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(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.