Skip to content

Commit

Permalink
Merge pull request #833 from ajkannan/merge-v1beta3
Browse files Browse the repository at this point in the history
Merge v1beta3
  • Loading branch information
aozarov committed Apr 1, 2016
2 parents 5fea95f + bf918e5 commit c70d9d7
Show file tree
Hide file tree
Showing 79 changed files with 1,711 additions and 1,484 deletions.
18 changes: 8 additions & 10 deletions gcloud-java-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-datastore-protobuf</artifactId>
<version>v1beta2-rev1-4.0.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
</exclusion>
</exclusions>
<groupId>com.google.cloud.datastore</groupId>
<artifactId>datastore-v1beta3-protos</artifactId>
<version>1.0.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.cloud.datastore</groupId>
<artifactId>datastore-v1beta3-proto-client</artifactId>
<version>1.0.0-beta</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.google.gcloud.datastore;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -88,9 +88,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
for (FullEntity<?> entity : entities) {
IncompleteKey key = entity.key();
Preconditions.checkArgument(key != null, "Entity must have a key");
if (key instanceof Key) {
addInternal((FullEntity<Key>) entity);
} else {
if (!(key instanceof Key)) {
incompleteKeys.add(key);
}
}
Expand All @@ -104,6 +102,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
for (FullEntity<?> entity : entities) {
if (entity.key() instanceof Key) {
addInternal((FullEntity<Key>) entity);
answer.add(Entity.convert((FullEntity<Key>) entity));
} else {
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
Expand Down Expand Up @@ -199,24 +198,30 @@ protected DatastoreException newInvalidRequest(String msg, Object... params) {
return DatastoreException.throwInvalidRequest(String.format(msg, params));
}

DatastoreV1.Mutation.Builder toMutationPb() {
DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder();
protected List<com.google.datastore.v1beta3.Mutation> toMutationPbList() {
List<com.google.datastore.v1beta3.Mutation> mutationsPb =
new ArrayList<>();
for (FullEntity<IncompleteKey> entity : toAddAutoId()) {
mutationPb.addInsertAutoId(entity.toPb());
mutationsPb.add(
com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build());
}
for (FullEntity<Key> entity : toAdd().values()) {
mutationPb.addInsert(entity.toPb());
mutationsPb.add(
com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build());
}
for (FullEntity<Key> entity : toUpdate().values()) {
mutationPb.addUpdate(entity.toPb());
mutationsPb.add(
com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(entity.toPb()).build());
}
for (FullEntity<Key> entity : toPut().values()) {
mutationPb.addUpsert(entity.toPb());
mutationsPb.add(
com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build());
}
for (Key key : toDelete()) {
mutationPb.addDelete(key.toPb());
mutationsPb.add(
com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(key.toPb()).build());
}
return mutationPb;
return mutationsPb;
}

protected abstract Datastore datastore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import static com.google.gcloud.datastore.DoubleValue.of;
import static com.google.gcloud.datastore.EntityValue.of;
import static com.google.gcloud.datastore.KeyValue.of;
import static com.google.gcloud.datastore.LatLngValue.of;
import static com.google.gcloud.datastore.ListValue.of;
import static com.google.gcloud.datastore.LongValue.of;
import static com.google.gcloud.datastore.NullValue.of;
import static com.google.gcloud.datastore.StringValue.of;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
import com.google.protobuf.InvalidProtocolBufferException;
Expand All @@ -49,7 +49,8 @@
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
* Entities, Properties, and Keys</a>
*/
public abstract class BaseEntity<K extends IncompleteKey> extends Serializable<DatastoreV1.Entity> {
public abstract class BaseEntity<K extends IncompleteKey>
extends Serializable<com.google.datastore.v1beta3.Entity> {

private static final long serialVersionUID = 8175618724683792766L;

Expand Down Expand Up @@ -91,10 +92,11 @@ private B self() {
}

@SuppressWarnings("unchecked")
B fill(DatastoreV1.Entity entityPb) {
B fill(com.google.datastore.v1beta3.Entity entityPb) {
Map<String, Value<?>> copiedProperties = Maps.newHashMap();
for (DatastoreV1.Property property : entityPb.getPropertyList()) {
copiedProperties.put(property.getName(), Value.fromPb(property.getValue()));
for (Map.Entry<String, com.google.datastore.v1beta3.Value> entry :
entityPb.getProperties().entrySet()) {
copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue()));
}
properties(copiedProperties);
if (entityPb.hasKey()) {
Expand Down Expand Up @@ -290,6 +292,36 @@ public B set(String name, DateTime first, DateTime second, DateTime... others) {
return self();
}

/**
* Sets a property of type {@link LatLng}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, LatLng value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link LatLng}.
*
* @param name name of the property
* @param first the first {@link LatLng} in the list
* @param second the second {@link LatLng} in the list
* @param others other {@link LatLng}s in the list
*/
public B set(String name, LatLng first, LatLng second, LatLng... others) {
List<LatLngValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (LatLng other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link KeyValue}.
*
Expand Down Expand Up @@ -545,6 +577,17 @@ public DateTime getDateTime(String name) {
return ((Value<DateTime>) getValue(name)).get();
}

/**
* Returns the property value as a LatLng.
*
* @throws DatastoreException if not such property.
* @throws ClassCastException if value is not a LatLng.
*/
@SuppressWarnings("unchecked")
public LatLng getLatLng(String name) {
return ((Value<LatLng>) getValue(name)).get();
}

/**
* Returns the property value as a Key.
*
Expand Down Expand Up @@ -603,20 +646,19 @@ ImmutableSortedMap<String, Value<?>> properties() {
@Override
Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
Builder<?, ?> builder = emptyBuilder();
builder.fill(DatastoreV1.Entity.parseFrom(bytesPb));
builder.fill(com.google.datastore.v1beta3.Entity.parseFrom(bytesPb));
return builder.build();
}

protected abstract Builder<?, ?> emptyBuilder();

@Override
final DatastoreV1.Entity toPb() {
DatastoreV1.Entity.Builder entityPb = DatastoreV1.Entity.newBuilder();
final com.google.datastore.v1beta3.Entity toPb() {
com.google.datastore.v1beta3.Entity.Builder entityPb =
com.google.datastore.v1beta3.Entity.newBuilder();
Map<String, com.google.datastore.v1beta3.Value> propertiesPb = entityPb.getMutableProperties();
for (Map.Entry<String, Value<?>> entry : properties.entrySet()) {
DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder();
propertyPb.setName(entry.getKey());
propertyPb.setValue(entry.getValue().toPb());
entityPb.addProperty(propertyPb.build());
propertiesPb.put(entry.getKey(), entry.getValue().toPb());
}
if (key != null) {
entityPb.setKey(key.toPb());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.gcloud.datastore.Validator.validateKind;
import static com.google.gcloud.datastore.Validator.validateNamespace;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

Expand All @@ -31,7 +30,7 @@
/**
* Base class for keys.
*/
public abstract class BaseKey extends Serializable<DatastoreV1.Key> {
public abstract class BaseKey extends Serializable<com.google.datastore.v1beta3.Key> {

private static final long serialVersionUID = -4671243265877410635L;

Expand All @@ -46,8 +45,8 @@ public abstract class BaseKey extends Serializable<DatastoreV1.Key> {
*/
protected abstract static class Builder<B extends Builder<B>> {

String projectId;
String namespace;
String projectId = "";
String namespace = "";
String kind;
final List<PathElement> ancestors;

Expand Down Expand Up @@ -179,20 +178,15 @@ public boolean equals(Object obj) {
}

@Override
DatastoreV1.Key toPb() {
DatastoreV1.Key.Builder keyPb = DatastoreV1.Key.newBuilder();
DatastoreV1.PartitionId.Builder partitionIdPb = DatastoreV1.PartitionId.newBuilder();
if (projectId != null) {
partitionIdPb.setDatasetId(projectId);
}
if (namespace != null) {
partitionIdPb.setNamespace(namespace);
}
if (partitionIdPb.hasDatasetId() || partitionIdPb.hasNamespace()) {
keyPb.setPartitionId(partitionIdPb.build());
}
com.google.datastore.v1beta3.Key toPb() {
com.google.datastore.v1beta3.Key.Builder keyPb = com.google.datastore.v1beta3.Key.newBuilder();
com.google.datastore.v1beta3.PartitionId.Builder partitionIdPb =
com.google.datastore.v1beta3.PartitionId.newBuilder();
partitionIdPb.setProjectId(projectId);
partitionIdPb.setNamespaceId(namespace);
keyPb.setPartitionId(partitionIdPb.build());
for (PathElement pathEntry : path) {
keyPb.addPathElement(pathEntry.toPb());
keyPb.addPath(pathEntry.toPb());
}
return keyPb.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,53 @@

package com.google.gcloud.datastore;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.gcloud.datastore.BatchOption.ForceWrites;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


class BatchImpl extends BaseDatastoreBatchWriter implements Batch {

private final DatastoreImpl datastore;
private final boolean force;

static class ResponseImpl implements Batch.Response {

private final DatastoreV1.CommitResponse response;
private final com.google.datastore.v1beta3.CommitResponse response;
private final int numAutoAllocatedIds;

ResponseImpl(DatastoreV1.CommitResponse response) {
ResponseImpl(com.google.datastore.v1beta3.CommitResponse response, int numAutoAllocatedIds) {
this.response = response;
this.numAutoAllocatedIds = numAutoAllocatedIds;
}

@Override
public List<Key> generatedKeys() {
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
new Function<DatastoreV1.Key, Key>() {
@Override public Key apply(DatastoreV1.Key keyPb) {
return Key.fromPb(keyPb);
}
});
Iterator<com.google.datastore.v1beta3.MutationResult> results =
response.getMutationResultsList().iterator();
List<Key> generated = new ArrayList<>(numAutoAllocatedIds);
for (int i = 0; i < numAutoAllocatedIds; i++) {
generated.add(Key.fromPb(results.next().getKey()));
}
return generated;
}
}

BatchImpl(DatastoreImpl datastore, BatchOption... options) {
BatchImpl(DatastoreImpl datastore) {
super("batch");
this.datastore = datastore;
Map<Class<? extends BatchOption>, BatchOption> optionsMap = BatchOption.asImmutableMap(options);
if (optionsMap.containsKey(ForceWrites.class)) {
force = ((ForceWrites) optionsMap.get(ForceWrites.class)).force();
} else {
force = datastore.options().force();
}
}

@Override
public Batch.Response submit() {
validateActive();
DatastoreV1.Mutation.Builder mutationPb = toMutationPb();
if (force) {
mutationPb.setForce(force);
}
DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder();
requestPb.setMode(DatastoreV1.CommitRequest.Mode.NON_TRANSACTIONAL);
requestPb.setMutation(mutationPb);
DatastoreV1.CommitResponse responsePb = datastore.commit(requestPb.build());
List<com.google.datastore.v1beta3.Mutation> mutationsPb = toMutationPbList();
com.google.datastore.v1beta3.CommitRequest.Builder requestPb =
com.google.datastore.v1beta3.CommitRequest.newBuilder();
requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.NON_TRANSACTIONAL);
requestPb.addAllMutations(mutationsPb);
com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build());
deactivate();
return new ResponseImpl(responsePb);
return new ResponseImpl(responsePb, toAddAutoId().size());
}

@Override
Expand Down
Loading

0 comments on commit c70d9d7

Please sign in to comment.