Skip to content

Commit

Permalink
Default projectId and namespace are empty strings, NullValue and Date…
Browse files Browse the repository at this point in the history
…Time are fixed, along with other minor edits
  • Loading branch information
Ajay Kannan committed Sep 18, 2015
1 parent a77fd34 commit 7a51c15
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected DatastoreV1.Mutation.Builder toMutationPb() {
mutationPb.addUpsert(entity.toPb());
}
for (Key key : toDelete()) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//mutationPb.addDelete(key.toPb());
}
return mutationPb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ private B self() {
protected B fill(DatastoreV1.Entity entityPb) {
Map<String, Value<?>> copiedProperties = Maps.newHashMap();
for (DatastoreV1.Property property : entityPb.getPropertyList()) {
// TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//copiedProperties.put(property.getName(), Value.fromPb(property.getValue()));
}
properties(copiedProperties);
if (entityPb.hasKey()) {
// TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//key((K) IncompleteKey.fromPb(entityPb.getKey()));
}
return self();
Expand Down Expand Up @@ -391,12 +391,12 @@ protected final DatastoreV1.Entity toPb() {
for (Map.Entry<String, Value<?>> entry : properties.entrySet()) {
DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder();
propertyPb.setName(entry.getKey());
// TODO(ajaykannan): Uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//propertyPb.setValue(entry.getValue().toPb());
entityPb.addProperty(propertyPb.build());
}
if (key != null) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//entityPb.setKey(key.toPb());
}
return entityPb.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.gcloud.datastore.Validator.validateNamespace;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

import java.util.LinkedList;
Expand All @@ -41,8 +40,8 @@ abstract class BaseKey extends Serializable<com.google.datastore.v1beta3.Key> {

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 @@ -176,15 +175,9 @@ protected 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();
if (!Strings.isNullOrEmpty(projectId)) {
partitionIdPb.setProjectId(projectId);
}
if (!Strings.isNullOrEmpty(namespace)) {
partitionIdPb.setNamespaceId(namespace);
}
if (!partitionIdPb.getProjectId().isEmpty() || !partitionIdPb.getNamespaceId().isEmpty()) {
keyPb.setPartitionId(partitionIdPb.build());
}
partitionIdPb.setProjectId(projectId);
partitionIdPb.setNamespaceId(namespace);
keyPb.setPartitionId(partitionIdPb.build());
for (PathElement pathEntry : path) {
keyPb.addPath(pathEntry.toPb());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public List<Key> generatedKeys() {
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
new Function<DatastoreV1.Key, Key>() {
@Override public Key apply(DatastoreV1.Key keyPb) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//return Key.fromPb(keyPb);
return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible
return Key.builder(null).build(); // TODO(ajaykannan): fix me!
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public List<Key> allocateId(IncompleteKey... keys) {
}
DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder();
for (IncompleteKey key : keys) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//requestPb.addKey(trimNameOrId(key).toPb());
}
DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build());
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
for (DatastoreV1.Key keyPb : responsePb.getKeyList()) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
// keyList.add(Key.fromPb(keyPb));
}
return keyList.build();
Expand Down Expand Up @@ -195,7 +195,7 @@ public List<Entity> add(FullEntity<?>... entities) {
if (completeEntity != null) {
responseBuilder.add(completeEntity);
} else {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//responseBuilder.add(Entity.builder(Key.fromPb(allocatedKeys.next()), entity).build());
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ Iterator<Entity> get(DatastoreV1.ReadOptions readOptionsPb, final Key... keys) {
requestPb.setReadOptions(readOptionsPb);
}
for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//requestPb.addKey(k.toPb());
}
return new ResultsIterator(requestPb);
Expand Down Expand Up @@ -314,7 +314,7 @@ public void delete(Key... keys) {
DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder();
Set<Key> dedupKeys = new LinkedHashSet<>(Arrays.asList(keys));
for (Key key : dedupKeys) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//mutationPb.addDelete(key.toPb());
}
commitMutation(mutationPb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
* Entities, Properties, and Keys</a>
*/
public final class DateTime extends Serializable<com.google.datastore.v1beta3.Value>
public final class DateTime extends Serializable<com.google.protobuf.Timestamp>
implements Comparable<DateTime> {

private static final long serialVersionUID = 7343324797621228378L;
Expand Down Expand Up @@ -96,24 +96,23 @@ public static DateTime copyFrom(Calendar calendar) {
}

@Override
protected com.google.datastore.v1beta3.Value toPb() {
return com.google.datastore.v1beta3.Value.newBuilder()
.setTimestampValue(microsecondsToTimestampPb(timestampMicroseconds)).build();
protected com.google.protobuf.Timestamp toPb() {
return microsecondsToTimestampPb(timestampMicroseconds);
}

@Override
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
return new DateTime(timestampPbToMicroseconds(com.google.datastore.v1beta3.Value
.parseFrom(bytesPb).getTimestampValue()));
return new DateTime(timestampPbToMicroseconds(
com.google.protobuf.Timestamp.parseFrom(bytesPb)));
}

protected static long timestampPbToMicroseconds(com.google.protobuf.Timestamp timestampPb) {
return timestampPb.getSeconds() * 10^6 + timestampPb.getNanos() / 10^3;
return timestampPb.getSeconds() * 1000000 + timestampPb.getNanos() / 1000;
}

protected static com.google.protobuf.Timestamp microsecondsToTimestampPb(long microseconds) {
long seconds = microseconds / 10^6;
int nanos = (int) (microseconds % 10^6) * 10^3;
long seconds = microseconds / 1000000;
int nanos = (int) (microseconds % 1000000) * 1000;
return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import static com.google.datastore.v1beta3.Value.DOUBLE_VALUE_FIELD_NUMBER;

import com.google.api.services.datastore.DatastoreV1;

public final class DoubleValue extends Value<Double> {

private static final long serialVersionUID = -5096238337676649540L;
Expand All @@ -43,7 +41,7 @@ public Builder newBuilder(Double value) {
protected Double getValue(com.google.datastore.v1beta3.Value from) {
return from.getDoubleValue();
}

@Override
protected void setValue(DoubleValue from, com.google.datastore.v1beta3.Value.Builder to) {
to.setDoubleValue(from.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public Builder newBuilder(FullEntity<?> value) {

@Override
protected FullEntity<?> getValue(com.google.datastore.v1beta3.Value from) {
// TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//return FullEntity.fromPb(from.getEntityValue());
return null; // TODO(ajaykannan): remove this line when possible
return null; // TODO(ajaykannan): fix me!
}

@Override
protected void setValue(EntityValue from, com.google.datastore.v1beta3.Value.Builder to) {
// TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//to.setEntityValue(from.get().toPb());
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected DatastoreV1.GqlQueryArg toPb() {
argPb.setCursor(cursor.byteString());
}
if (value != null) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//argPb.setValue(value.toPb());
}
return argPb.build();
Expand All @@ -151,9 +151,9 @@ static Binding fromPb(DatastoreV1.GqlQueryArg argPb) {
if (argPb.hasCursor()) {
return new Binding(name, new Cursor(argPb.getCursor()));
}
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//return new Binding(name, Value.fromPb(argPb.getValue()));
return new Binding(name, new Cursor(null)); // TODO(ajaykannan): remove this line when possible
return new Binding(name, new Cursor(null)); // TODO(ajaykannan): fix me!
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,12 @@ protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
}

static IncompleteKey fromPb(com.google.datastore.v1beta3.Key keyPb) {
String projectId = null;
String namespace = null;
String projectId = "";
String namespace = "";
if (keyPb.hasPartitionId()) {
com.google.datastore.v1beta3.PartitionId partitionIdPb = keyPb.getPartitionId();
projectId = partitionIdPb.getProjectId();
if (projectId.isEmpty()) {
projectId = null;
}
namespace = partitionIdPb.getNamespaceId();
if (namespace.isEmpty()) {
namespace = null;
}
}
List<com.google.datastore.v1beta3.Key.PathElement> pathElementsPb = keyPb.getPathList();
Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public KeyFactory(String projectId, String namespace) {
super(projectId);
namespace(namespace);
this.pi = projectId;
this.ns = namespace;
this.ns = (namespace == null) ? "" : namespace;
}

public IncompleteKey newKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import static com.google.datastore.v1beta3.Value.NULL_VALUE_FIELD_NUMBER;

public final class NullValue extends Value<Void> {

private static final long serialVersionUID = 8497300779013002270L;
Expand All @@ -34,7 +36,7 @@ public Builder newBuilder(Void value) {

@Override
public int getProtoFieldId() {
return 0;
return NULL_VALUE_FIELD_NUMBER;
}

@Override
Expand All @@ -44,11 +46,12 @@ protected Void getValue(com.google.datastore.v1beta3.Value from) {

@Override
protected void setValue(NullValue from, com.google.datastore.v1beta3.Value.Builder to) {
// nothing to set
to.setNullValue(com.google.protobuf.NullValue.NULL_VALUE);
}
};

public static final class Builder extends Value.BaseBuilder<Void, NullValue, Builder> {
public static final class Builder
extends Value.BaseBuilder<Void, NullValue, Builder> {

private Builder() {
super(ValueType.NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public boolean equals(Object obj) {

@Override
protected com.google.datastore.v1beta3.Key.PathElement toPb() {
com.google.datastore.v1beta3.Key.PathElement.Builder pathElementPb = com.google.datastore.v1beta3.Key.PathElement.newBuilder();
com.google.datastore.v1beta3.Key.PathElement.Builder pathElementPb =
com.google.datastore.v1beta3.Key.PathElement.newBuilder();
pathElementPb.setKind(kind);
if (id != null) {
pathElementPb.setId(id);
Expand All @@ -103,15 +104,14 @@ protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {

static PathElement fromPb(com.google.datastore.v1beta3.Key.PathElement pathElementPb) {
String kind = pathElementPb.getKind();
if (pathElementPb.getIdTypeCase() ==
com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.ID) {
return of(kind, pathElementPb.getId());
switch (pathElementPb.getIdTypeCase()) {
case ID:
return of(kind, pathElementPb.getId());
case NAME:
return of(kind, pathElementPb.getName());
default:
return of(kind);
}
if (pathElementPb.getIdTypeCase() ==
com.google.datastore.v1beta3.Key.PathElement.IdTypeCase.NAME) {
return of(kind, pathElementPb.getName());
}
return of(kind);
}

static PathElement of(String kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public abstract static class ResultType<V> implements java.io.Serializable {
if (!entityPb.hasKey()) {
return null;
}
//TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
//TODO(ajaykannan): fix me!
//return Key.fromPb(entityPb.getKey());
}
return ProjectionEntity.fromPb(entityPb);
Expand All @@ -89,9 +89,9 @@ public abstract static class ResultType<V> implements java.io.Serializable {
private static final long serialVersionUID = -8514289244104446252L;

@Override protected Key convert(DatastoreV1.Entity entityPb) {
//TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
//TODO(ajaykannan): fix me!
//return Key.fromPb(entityPb.getKey());
return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible
return Key.builder(null).build(); // TODO(ajaykannan): fix me!
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public int getProtoFieldId() {
}

@Override
protected com.google.datastore.v1beta3.Value getValue(com.google.datastore.v1beta3.Value from) {
protected com.google.datastore.v1beta3.Value getValue(
com.google.datastore.v1beta3.Value from) {
return from;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ private PropertyFilter(String property, Operator operator, Value<?> value) {
public static PropertyFilter fromPb(DatastoreV1.PropertyFilter propertyFilterPb) {
String property = propertyFilterPb.getProperty().getName();
Operator operator = Operator.fromPb(propertyFilterPb.getOperator());
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//Value<?> value = Value.fromPb(propertyFilterPb.getValue());
//return new PropertyFilter(property, operator, value);
return new PropertyFilter(property, operator, null); // TODO(ajaykannan): remove this line when possible
return new PropertyFilter(property, operator, null); // TODO(ajaykannan): fix me!
}

@Override
Expand Down Expand Up @@ -437,7 +437,7 @@ protected DatastoreV1.Filter toPb() {
propertyFilterPb.getPropertyBuilder().setName(property);
propertyFilterPb.setOperator(operator.toPb());
if (value != null) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//propertyFilterPb.setValue(value.toPb());
}
return filterPb.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public List<Key> generatedKeys() {
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
new Function<DatastoreV1.Key, Key>() {
@Override public Key apply(DatastoreV1.Key keyPb) {
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
// TODO(ajaykannan): fix me!
//return Key.fromPb(keyPb);
return Key.builder(null).build(); //: TODO(ajaykannan) remove this placeholder line
return Key.builder(null).build(); // TODO(ajaykannan): fix me!
}
});
}
Expand Down
Loading

0 comments on commit 7a51c15

Please sign in to comment.