Skip to content

Commit fbe0845

Browse files
committed
---
yaml --- r: 5895 b: refs/heads/tswast-patch-1 c: 0f252cf h: refs/heads/master i: 5893: 4699e3a 5891: 8034d11 5887: be8ff24
1 parent 3a6e8d9 commit fbe0845

23 files changed

Lines changed: 128 additions & 92 deletions

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: a0fd66c4e77330f71a3ff0721d3cddc4c4d11d49
60+
refs/heads/tswast-patch-1: 0f252cf76c7de0a98e518c7d952db426d2281bfa
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/Blob.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.gcloud.datastore;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120

2221
import com.google.api.services.datastore.DatastoreV1;
@@ -34,23 +33,18 @@
3433

3534
/**
3635
* A Google Cloud Datastore Blob.
37-
* A Datastore blob is limited to {@value #MAX_LENGTH} bytes.
3836
* This class is immutable.
3937
*
4038
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
4139
*/
4240
public final class Blob extends Serializable<DatastoreV1.Value> {
4341

4442
private static final long serialVersionUID = 3835421019618247721L;
45-
public static final int MAX_LENGTH = 1_000_000;
4643

4744
private final transient ByteString byteString;
4845

49-
Blob(ByteString byteString, boolean enforceLimits) {
46+
Blob(ByteString byteString) {
5047
this.byteString = checkNotNull(byteString);
51-
if (enforceLimits) {
52-
checkArgument(byteString.size() <= MAX_LENGTH, "May be a maximum of %,d bytes", MAX_LENGTH);
53-
}
5448
}
5549

5650
@Override
@@ -134,11 +128,11 @@ ByteString byteString() {
134128
}
135129

136130
public static Blob copyFrom(byte[] bytes) {
137-
return new Blob(ByteString.copyFrom(bytes), true);
131+
return new Blob(ByteString.copyFrom(bytes));
138132
}
139133

140134
public static Blob copyFrom(ByteBuffer bytes) {
141-
return new Blob(ByteString.copyFrom(bytes), true);
135+
return new Blob(ByteString.copyFrom(bytes));
142136
}
143137

144138
public static Blob copyFrom(InputStream input) throws IOException {
@@ -158,6 +152,6 @@ protected Value toPb() {
158152

159153
@Override
160154
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
161-
return new Blob(DatastoreV1.Value.parseFrom(bytesPb).getBlobValue(), false);
155+
return new Blob(DatastoreV1.Value.parseFrom(bytesPb).getBlobValue());
162156
}
163157
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BlobValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Builder newBuilder(Blob value) {
4141

4242
@Override
4343
protected Blob getValue(DatastoreV1.Value from) {
44-
return new Blob(from.getBlobValue(), false);
44+
return new Blob(from.getBlobValue());
4545
}
4646

4747
@Override

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ static List<Entity> fetch(DatastoreReader reader, Key... keys) {
5151
return list;
5252
}
5353

54-
static void runInTransaction(DatastoreService datastoreService,
55-
DatastoreService.RunInTransaction runFor, TransactionOption... options) {
54+
static <T> T runInTransaction(DatastoreService datastoreService,
55+
DatastoreService.TransactionCallable<T> callable, TransactionOption... options) {
5656
Transaction transaction = datastoreService.newTransaction(options);
5757
try {
58-
runFor.run(transaction);
58+
T value = callable.run(transaction);
5959
transaction.commit();
60+
return value;
61+
} catch (Exception ex) {
62+
transaction.rollback();
63+
throw DatastoreServiceException.propagateUserException(ex);
6064
} finally {
6165
if (transaction.active()) {
6266
transaction.rollback();

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ public interface DatastoreReader {
5555
*
5656
* @throws DatastoreServiceException upon failure.
5757
*/
58-
<T> QueryResult<T> run(Query<T> query);
58+
<T> QueryResults<T> run(Query<T> query);
5959
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreService.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,32 @@ public interface DatastoreService extends Service<DatastoreServiceOptions>, Data
3232
*/
3333
Transaction newTransaction(TransactionOption... options);
3434

35-
interface RunInTransaction {
36-
void run(DatastoreReaderWriter readerWriter);
35+
36+
/**
37+
* An Callback for running with a Transactional
38+
* {@link com.google.gcloud.datastore.DatastoreReaderWriter}.
39+
* The associated transaction will be committed after a successful return from the {@code run}
40+
* method. Any propagated exception will cause the transaction to be rolled-back.
41+
*
42+
* @param <T> the type of the return value
43+
*/
44+
interface TransactionCallable<T> {
45+
T run(DatastoreReaderWriter readerWriter) throws Exception;
3746
}
3847

3948

4049
/**
41-
* Invokes the callback's {@link RunInTransaction#run} method with a
50+
* Invokes the callback's {@link DatastoreService.TransactionCallable#run} method with a
4251
* {@link DatastoreReaderWriter} that is associated with a new transaction.
43-
* The transaction will be committed upon successful invocation or rollback
44-
* otherwise.
52+
* The transaction will be committed upon successful invocation.
53+
* Any thrown exception will cause the transaction to rollback and will be propagated
54+
* as a {@link DatastoreServiceException} with the original exception as its root cause.
4555
*
46-
* @param runFor the functor to call with the transactional readerWriter
56+
* @param callable the callback to call with a newly created transactional readerWriter
4757
* @param options the options for the created transaction
58+
* @throws DatastoreServiceException upon failure
4859
*/
49-
void runInTransaction(RunInTransaction runFor, TransactionOption... options);
60+
<T> T runInTransaction(TransactionCallable<T> callable, TransactionOption... options);
5061

5162
/**
5263
* Returns a new Batch for processing multiple write operations in one request.

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreServiceException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,8 @@ static DatastoreServiceException translateAndThrow(DatastoreRpcException excepti
148148
static DatastoreServiceException throwInvalidRequest(String massage, Object... params) {
149149
throw new DatastoreServiceException(Code.FAILED_PRECONDITION, String.format(massage, params));
150150
}
151+
152+
static DatastoreServiceException propagateUserException(Exception ex) {
153+
throw new DatastoreServiceException(Code.UNKNOWN, ex.getMessage(), ex);
154+
}
151155
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@ public Transaction newTransaction(TransactionOption... options) {
9090
return new TransactionImpl(this, options);
9191
}
9292

93-
public void runInTransaction(RunInTransaction runFor, TransactionOption... options) {
94-
DatastoreHelper.runInTransaction(this, runFor, options);
93+
@Override
94+
public <T> T runInTransaction(TransactionCallable<T> callable, TransactionOption... options) {
95+
return DatastoreHelper.runInTransaction(this, callable, options);
9596
}
9697

9798
@Override
98-
public <T> QueryResult<T> run(Query<T> query) {
99+
public <T> QueryResults<T> run(Query<T> query) {
99100
return run(null, query);
100101
}
101102

102-
<T> QueryResult<T> run(DatastoreV1.ReadOptions readOptionsPb, Query<T> query) {
103-
return new QueryResultImpl<>(this, readOptionsPb, query);
103+
<T> QueryResults<T> run(DatastoreV1.ReadOptions readOptionsPb, Query<T> query) {
104+
return new QueryResultsImpl<>(this, readOptionsPb, query);
104105
}
105106

106107
DatastoreV1.RunQueryResponse runQuery(final DatastoreV1.RunQueryRequest requestPb) {
@@ -129,10 +130,9 @@ public List<Key> allocateId(PartialKey... keys) {
129130
for (PartialKey key : keys) {
130131
requestPb.addKey(trimNameOrId(key).toPb());
131132
}
132-
// TODO(ozarov): will need to populate "force" after b/18594027 is fixed.
133133
DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build());
134134
Iterator<DatastoreV1.Key> keyIterator = responsePb.getKeyList().iterator();
135-
ImmutableList.Builder builder = ImmutableList.builder().addAll(
135+
ImmutableList.Builder<Key> builder = ImmutableList.<Key>builder().addAll(
136136
Iterators.transform(keyIterator, new Function<DatastoreV1.Key, Key>() {
137137
@Override
138138
public Key apply(DatastoreV1.Key keyPb) {

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/EntityValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private Builder() {
5959

6060
@Override
6161
public Builder indexed(boolean indexed) {
62-
// see b/8730533
62+
// see issue #25
6363
Preconditions.checkArgument(!indexed, "EntityValue can't be indexed");
6464
return super.indexed(indexed);
6565
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/GqlQuery.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* <p>When the type of the results is known the preferred usage would be:
4646
* <pre>{@code
4747
* Query&lt;Entity&gt; query = GqlQuery.builder(Query.Type.FULL, "select * from kind").build();
48-
* QueryResult&lt;Entity&gt; results = datastore.run(query);
48+
* QueryResults&lt;Entity&gt; results = datastore.run(query);
4949
* while (results.hasNext()) {
5050
* Entity entity = results.next();
5151
* ...
@@ -55,9 +55,9 @@
5555
* <p>When the type of the results is unknown you can use this approach:
5656
* <pre>{@code
5757
* Query&lt;?&gt; query = GqlQuery.builder("select __key__ from kind").build();
58-
* QueryResult&lt;?&gt; results = datastore.run(query);
58+
* QueryResults&lt;?&gt; results = datastore.run(query);
5959
* if (Key.class.isAssignableFrom(results.resultClass())) {
60-
* QueryResult&lt;Key&gt; keys = (QueryResult&lt;Key&gt;) results;
60+
* QueryResults&lt;Key&gt; keys = (QueryResults&lt;Key&gt;) results;
6161
* while (keys.hasNext()) {
6262
* Key key = keys.next();
6363
* ...
@@ -389,7 +389,7 @@ protected void populatePb(DatastoreV1.RunQueryRequest.Builder requestPb) {
389389

390390
@Override
391391
protected GqlQuery<V> nextQuery(DatastoreV1.QueryResultBatch responsePb) {
392-
// See b/18705483
392+
// See issue #17
393393
throw new UnsupportedOperationException("paging for this query is not implemented yet");
394394
}
395395

0 commit comments

Comments
 (0)