33import static com .google .gcloud .datastore .DatastoreServiceException .throwInvalidRequest ;
44
55import com .google .api .services .datastore .DatastoreV1 ;
6- import com .google .gcloud .datastore .BatchWriteOption .ForceWrites ;
6+ import com .google .common .base .Function ;
7+ import com .google .common .collect .Lists ;
8+ import com .google .gcloud .datastore .BatchOption .ForceWrites ;
79
810import java .util .LinkedHashMap ;
911import java .util .LinkedHashSet ;
1214import java .util .Map ;
1315import java .util .Set ;
1416
15- class BatchWriterImpl implements BatchWriter {
17+ class BatchImpl implements Batch {
1618
1719 private final Map <Key , Entity > toAdd = new LinkedHashMap <>();
1820 private final List <PartialEntity > toAddAutoId = new LinkedList <>();
@@ -24,10 +26,30 @@ class BatchWriterImpl implements BatchWriter {
2426
2527 private boolean active = true ;
2628
27- BatchWriterImpl (DatastoreServiceImpl datastore , BatchWriteOption ... options ) {
29+
30+ class ResponseImpl implements Response {
31+
32+ private final DatastoreV1 .CommitResponse response ;
33+
34+ public ResponseImpl (DatastoreV1 .CommitResponse response ) {
35+ this .response = response ;
36+ }
37+
38+ @ Override
39+ public List <Key > generatedKeys () {
40+ return Lists .transform (response .getMutationResult ().getInsertAutoIdKeyList (),
41+ new Function <DatastoreV1 .Key , Key >() {
42+ @ Override public Key apply (DatastoreV1 .Key keyPb ) {
43+ return Key .fromPb (keyPb );
44+ }
45+ });
46+ }
47+ }
48+
49+ BatchImpl (DatastoreServiceImpl datastore , BatchOption ... options ) {
2850 this .datastore = datastore ;
29- Map <Class <? extends BatchWriteOption >, BatchWriteOption > optionsMap =
30- BatchWriteOption .asImmutableMap (options );
51+ Map <Class <? extends BatchOption >, BatchOption > optionsMap =
52+ BatchOption .asImmutableMap (options );
3153 if (optionsMap .containsKey (ForceWrites .class )) {
3254 force = ((ForceWrites ) optionsMap .get (ForceWrites .class )).force ();
3355 } else {
@@ -115,7 +137,11 @@ public void delete(Key... keys) {
115137 }
116138
117139 @ Override
118- public void submit () {
140+ public Response submit () {
141+ return new ResponseImpl (commitRequest ());
142+ }
143+
144+ DatastoreV1 .CommitResponse commitRequest () {
119145 validateActive ();
120146 DatastoreV1 .Mutation .Builder mutationPb = DatastoreV1 .Mutation .newBuilder ();
121147 for (PartialEntity entity : toAddAutoId ) {
@@ -138,8 +164,9 @@ public void submit() {
138164 }
139165 DatastoreV1 .CommitRequest .Builder requestPb = newCommitRequest ();
140166 requestPb .setMutation (mutationPb );
141- datastore .commit (requestPb .build ());
167+ DatastoreV1 . CommitResponse response = datastore .commit (requestPb .build ());
142168 active = false ;
169+ return response ;
143170 }
144171
145172 @ Override
0 commit comments