Skip to content

Commit ba77e75

Browse files
committed
---
yaml --- r: 49 b: refs/heads/master c: 7630e70 h: refs/heads/master i: 47: b85f58e v: v3
1 parent b5ed9a1 commit ba77e75

20 files changed

Lines changed: 545 additions & 75 deletions

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 5474d4f60576ced7a2584212a41e876e38366a1b
2+
refs/heads/master: 7630e70cd92900fa2958e4b123fce6edbb6a8ddf

trunk/src/main/java/com/google/gcloud/datastore/BatchWriter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.google.gcloud.datastore;
22

3+
/**
4+
* An interface to represent a batch of write operations.
5+
* Any write operation that is applied on a batch will only be sent
6+
* to the Datastore upon {@link #submit} and with as few RPC calls as possible.
7+
*/
38
public interface BatchWriter extends DatastoreWriter {
49

510
/**

trunk/src/main/java/com/google/gcloud/datastore/Blob.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import static com.google.common.base.Preconditions.checkArgument;
44
import static com.google.common.base.Preconditions.checkNotNull;
55

6+
import com.google.api.services.datastore.DatastoreV1;
7+
import com.google.api.services.datastore.DatastoreV1.Value;
68
import com.google.common.base.MoreObjects;
79
import com.google.common.base.MoreObjects.ToStringHelper;
810
import com.google.protobuf.ByteString;
11+
import com.google.protobuf.InvalidProtocolBufferException;
912

1013
import java.io.BufferedInputStream;
1114
import java.io.ByteArrayOutputStream;
@@ -20,12 +23,12 @@
2023
*
2124
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
2225
*/
23-
public final class Blob implements java.io.Serializable {
26+
public final class Blob extends Serializable<DatastoreV1.Value> {
2427

2528
private static final long serialVersionUID = 3835421019618247721L;
2629
private static final int MAX_LENGTH = 1_000_000;
2730

28-
private final ByteString byteString;
31+
private final transient ByteString byteString;
2932

3033
Blob(ByteString byteString, boolean enforceLimits) {
3134
this.byteString = checkNotNull(byteString);
@@ -54,6 +57,9 @@ public int hashCode() {
5457

5558
@Override
5659
public boolean equals(Object obj) {
60+
if (obj == this) {
61+
return true;
62+
}
5763
if (!(obj instanceof Blob)) {
5864
return false;
5965
}
@@ -134,4 +140,14 @@ public static Blob copyFrom(InputStream input) throws IOException {
134140
}
135141
return copyFrom(bytes.toByteArray());
136142
}
143+
144+
@Override
145+
protected Value toPb() {
146+
return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build();
147+
}
148+
149+
@Override
150+
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
151+
return new Blob(DatastoreV1.Value.parseFrom(bytesPb).getBlobValue(), false);
152+
}
137153
}

trunk/src/main/java/com/google/gcloud/datastore/Cursor.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,99 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44
import static java.nio.charset.StandardCharsets.UTF_8;
55

6+
import com.google.api.services.datastore.DatastoreV1;
7+
import com.google.api.services.datastore.DatastoreV1.Value;
68
import com.google.common.base.MoreObjects;
79
import com.google.common.base.MoreObjects.ToStringHelper;
810
import com.google.protobuf.ByteString;
11+
import com.google.protobuf.InvalidProtocolBufferException;
912

10-
import java.io.Serializable;
1113
import java.io.UnsupportedEncodingException;
1214
import java.net.URLDecoder;
1315
import java.net.URLEncoder;
14-
import java.util.Arrays;
1516

1617
/**
1718
* A Google Cloud Datastore cursor.
1819
* The cursor can be used to as a starting point or an ending point for a {@link Query}
1920
*/
20-
public final class Cursor implements Serializable {
21+
public final class Cursor extends Serializable<DatastoreV1.Value> {
2122

2223
private static final long serialVersionUID = -1423744878777486541L;
2324

24-
private final byte[] bytes;
25+
private final transient ByteString byteString;
2526

26-
public Cursor(byte[] bytes) {
27-
this.bytes = checkNotNull(bytes);
27+
Cursor(ByteString byteString) {
28+
this.byteString = byteString;
2829
}
2930

3031
@Override
3132
public int hashCode() {
32-
return Arrays.hashCode(bytes);
33+
return byteString.hashCode();
3334
}
3435

3536
@Override
3637
public boolean equals(Object obj) {
38+
if (obj == this) {
39+
return true;
40+
}
3741
if (!(obj instanceof Cursor)) {
3842
return false;
3943
}
40-
41-
return Arrays.equals(bytes, ((Cursor) obj).bytes);
44+
return byteString.equals(((Cursor) obj).byteString);
4245
}
4346

4447
@Override
4548
public String toString() {
4649
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
4750
StringBuilder stBuilder = new StringBuilder();
48-
for (byte b : bytes) {
49-
stBuilder.append(String.format("%02x", b));
51+
for (int i = 0; i < byteString.size(); i++) {
52+
stBuilder.append(String.format("%02x", byteString.byteAt(i)));
5053
}
5154
return toStringHelper.add("bytes", stBuilder.toString()).toString();
5255
}
5356

57+
ByteString byteString() {
58+
return byteString;
59+
}
60+
5461
/**
5562
* Returns the cursor in an encoded form that can be used as part of a URL.
5663
*/
5764
public String toUrlSafe() {
5865
try {
59-
return URLEncoder.encode(toString(), UTF_8.name());
66+
return URLEncoder.encode(toPb().toString(), UTF_8.name());
6067
} catch (UnsupportedEncodingException e) {
6168
throw new RuntimeException("Unxpeced encoding exception", e);
6269
}
6370
}
6471

65-
ByteString toPb() {
66-
return ByteString.copyFrom(bytes);
67-
}
68-
6972
/**
7073
* Create a {@code Cursor} given its URL safe encoded form.
7174
*/
7275
public static Cursor fromUrlSafe(String urlSafe) {
7376
try {
7477
String utf8Str = URLDecoder.decode(urlSafe, UTF_8.name());
75-
ByteString byteString = ByteString.copyFromUtf8(utf8Str);
76-
return fromPb(byteString);
77-
} catch (UnsupportedEncodingException e) {
78+
return fromPb(DatastoreV1.Value.parseFrom(utf8Str.getBytes()));
79+
} catch (UnsupportedEncodingException | InvalidProtocolBufferException e) {
7880
throw new RuntimeException("Unxpeced decoding exception", e);
7981
}
8082
}
8183

82-
private static Cursor fromPb(ByteString byteString) {
83-
return new Cursor(byteString.toByteArray());
84+
public static Cursor copyFrom(byte[] bytes) {
85+
return new Cursor(ByteString.copyFrom(checkNotNull(bytes)));
86+
}
87+
88+
@Override
89+
protected Value toPb() {
90+
return DatastoreV1.Value.newBuilder().setBlobValue(byteString).build();
91+
}
92+
93+
@Override
94+
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
95+
return fromPb(DatastoreV1.Value.parseFrom(bytesPb));
96+
}
97+
98+
static Cursor fromPb(DatastoreV1.Value valuePb) {
99+
return new Cursor(valuePb.getBlobValue());
84100
}
85101
}

trunk/src/main/java/com/google/gcloud/datastore/DatastoreReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ public interface DatastoreReader {
2727
*
2828
* @throws DatastoreServiceException upon failure.
2929
*/
30-
QueryResult<PartialEntity> runQuery(Query query);
30+
<T> QueryResult<T> runQuery(Query<T> query);
3131
}

trunk/src/main/java/com/google/gcloud/datastore/DatastoreServiceImpl.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.google.gcloud.datastore;
22

33
import com.google.api.services.datastore.DatastoreV1;
4-
import com.google.api.services.datastore.DatastoreV1.BeginTransactionResponse;
54
import com.google.api.services.datastore.client.Datastore;
65
import com.google.api.services.datastore.client.DatastoreException;
76
import com.google.common.collect.AbstractIterator;
@@ -49,11 +48,27 @@ public Transaction newTransaction(TransactionOption... transactionOption) {
4948
}
5049

5150
@Override
52-
public QueryResult<PartialEntity> runQuery(Query query) {
53-
// TODO To implement
54-
throw new RuntimeException("Not implemented yet");
51+
public <T> QueryResult<T> runQuery(Query<T> query) {
52+
DatastoreV1.RunQueryRequest.Builder requestPbBuilder = DatastoreV1.RunQueryRequest.newBuilder();
53+
DatastoreV1.PartitionId.Builder partitionId = DatastoreV1.PartitionId.newBuilder();
54+
partitionId.setDatasetId(options.dataset());
55+
String namespace = query.namespace() != null ? query.namespace() : options.namespace();
56+
if (namespace != null) {
57+
partitionId.setNamespace(namespace);
58+
}
59+
requestPbBuilder.setPartitionId(partitionId.build());
60+
query.populatePb(requestPbBuilder, 0, null);
61+
DatastoreV1.RunQueryRequest requestPb = requestPbBuilder.build();
62+
return new QueryResultImpl<>(this, query, requestPb, runQuery(requestPb).getBatch());
5563
}
5664

65+
DatastoreV1.RunQueryResponse runQuery(DatastoreV1.RunQueryRequest requestPb) {
66+
try {
67+
return datastore.runQuery(requestPb);
68+
} catch (DatastoreException e) {
69+
throw DatastoreServiceException.translateAndThrow(e);
70+
}
71+
}
5772

5873
@Override
5974
public Key allocateId(PartialKey key) {
@@ -211,7 +226,8 @@ void comitMutation(DatastoreV1.CommitRequest.Builder requestPb) {
211226

212227
ByteString requestTransactionId(DatastoreV1.BeginTransactionRequest.Builder requestPb) {
213228
try {
214-
BeginTransactionResponse responsePb = datastore.beginTransaction(requestPb.build());
229+
DatastoreV1.BeginTransactionResponse responsePb =
230+
datastore.beginTransaction(requestPb.build());
215231
return responsePb.getTransaction();
216232
} catch (DatastoreException e) {
217233
throw DatastoreServiceException.translateAndThrow(e);

trunk/src/main/java/com/google/gcloud/datastore/DateTime.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
import com.google.api.services.datastore.DatastoreV1;
6+
import com.google.api.services.datastore.DatastoreV1.Value;
7+
import com.google.protobuf.InvalidProtocolBufferException;
8+
59
import org.joda.time.format.ISODateTimeFormat;
610

711
import java.util.Calendar;
@@ -13,11 +17,11 @@
1317
*
1418
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
1519
*/
16-
public final class DateTime implements java.io.Serializable {
20+
public final class DateTime extends Serializable<DatastoreV1.Value> {
1721

1822
private static final long serialVersionUID = 7343324797621228378L;
1923

20-
private final long timestampMicroseconds;
24+
private final transient long timestampMicroseconds;
2125

2226
DateTime(long timestampMicroseconds) {
2327
this.timestampMicroseconds = timestampMicroseconds;
@@ -35,6 +39,9 @@ public int hashCode() {
3539

3640
@Override
3741
public boolean equals(Object obj) {
42+
if (obj == this) {
43+
return true;
44+
}
3845
if (!(obj instanceof DateTime)) {
3946
return false;
4047
}
@@ -70,4 +77,14 @@ public static DateTime copyFrom(Date date) {
7077
public static DateTime copyFrom(Calendar calendar) {
7178
return copyFrom(calendar.getTime());
7279
}
80+
81+
@Override
82+
protected Value toPb() {
83+
return DatastoreV1.Value.newBuilder().setIntegerValue(timestampMicroseconds).build();
84+
}
85+
86+
@Override
87+
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
88+
return new DateTime(DatastoreV1.Value.parseFrom(bytesPb).getIntegerValue());
89+
}
7390
}

trunk/src/main/java/com/google/gcloud/datastore/EntityValue.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private Builder() {
4242

4343
@Override
4444
public Builder indexed(boolean indexed) {
45+
// see b/8730533
4546
Preconditions.checkArgument(!indexed, "EntityValue can't be indexed");
4647
return super.indexed(indexed);
4748
}

0 commit comments

Comments
 (0)