@@ -87,11 +87,12 @@ com.google.datastore.v1beta3.RunQueryResponse runQuery(
8787 try {
8888 return RetryHelper .runWithRetries (
8989 new Callable <com .google .datastore .v1beta3 .RunQueryResponse >() {
90- @ Override public com .google .datastore .v1beta3 .RunQueryResponse call ()
91- throws DatastoreException {
92- return datastoreRpc .runQuery (requestPb );
93- }
94- }, retryParams , EXCEPTION_HANDLER , options ().clock ());
90+ @ Override
91+ public com .google .datastore .v1beta3 .RunQueryResponse call ()
92+ throws DatastoreException {
93+ return datastoreRpc .runQuery (requestPb );
94+ }
95+ }, retryParams , EXCEPTION_HANDLER , options ().clock ());
9596 } catch (RetryHelperException e ) {
9697 throw DatastoreException .translateAndThrow (e );
9798 }
@@ -125,11 +126,12 @@ com.google.datastore.v1beta3.AllocateIdsResponse allocateIds(
125126 try {
126127 return RetryHelper .runWithRetries (
127128 new Callable <com .google .datastore .v1beta3 .AllocateIdsResponse >() {
128- @ Override public com .google .datastore .v1beta3 .AllocateIdsResponse call ()
129- throws DatastoreException {
130- return datastoreRpc .allocateIds (requestPb );
131- }
132- }, retryParams , EXCEPTION_HANDLER , options ().clock ());
129+ @ Override
130+ public com .google .datastore .v1beta3 .AllocateIdsResponse call ()
131+ throws DatastoreException {
132+ return datastoreRpc .allocateIds (requestPb );
133+ }
134+ }, retryParams , EXCEPTION_HANDLER , options ().clock ());
133135 } catch (RetryHelperException e ) {
134136 throw DatastoreException .translateAndThrow (e );
135137 }
@@ -166,7 +168,7 @@ public List<Entity> add(FullEntity<?>... entities) {
166168 "Duplicate entity with the key %s" , entity .key ());
167169 }
168170 } else {
169- Preconditions .checkArgument (entity .hasKey (), "entity %s is missing a key" , entity );
171+ Preconditions .checkArgument (entity .hasKey (), "Entity %s is missing a key" , entity );
170172 }
171173 mutationsPb .add (com .google .datastore .v1beta3 .Mutation .newBuilder ()
172174 .setInsert (entity .toPb ()).build ());
@@ -281,19 +283,19 @@ com.google.datastore.v1beta3.LookupResponse lookup(
281283 try {
282284 return RetryHelper .runWithRetries (
283285 new Callable <com .google .datastore .v1beta3 .LookupResponse >() {
284- @ Override public com .google .datastore .v1beta3 .LookupResponse call ()
285- throws DatastoreException {
286- return datastoreRpc .lookup (requestPb );
287- }
288- }, retryParams , EXCEPTION_HANDLER , options ().clock ());
286+ @ Override
287+ public com .google .datastore .v1beta3 .LookupResponse call ()
288+ throws DatastoreException {
289+ return datastoreRpc .lookup (requestPb );
290+ }
291+ }, retryParams , EXCEPTION_HANDLER , options ().clock ());
289292 } catch (RetryHelperException e ) {
290293 throw DatastoreException .translateAndThrow (e );
291294 }
292295 }
293296
294- @ SafeVarargs
295297 @ Override
296- public final void update (Entity ... entities ) {
298+ public void update (Entity ... entities ) {
297299 if (entities .length > 0 ) {
298300 List <com .google .datastore .v1beta3 .Mutation > mutationsPb =
299301 new ArrayList <>();
@@ -309,22 +311,47 @@ public final void update(Entity... entities) {
309311 }
310312 }
311313
312- @ SafeVarargs
313314 @ Override
314- public final void put (Entity ... entities ) {
315- if (entities .length > 0 ) {
316- List <com .google .datastore .v1beta3 .Mutation > mutationsPb =
317- new ArrayList <>();
318- Map <Key , Entity > dedupEntities = new LinkedHashMap <>();
319- for (Entity entity : entities ) {
320- dedupEntities .put (entity .key (), entity );
321- }
322- for (Entity e : dedupEntities .values ()) {
315+ public Entity put (FullEntity <?> entity ) {
316+ return DatastoreHelper .put (this , entity );
317+ }
318+
319+ @ SuppressWarnings ("unchecked" )
320+ @ Override
321+ public List <Entity > put (FullEntity <?>... entities ) {
322+ if (entities .length == 0 ) {
323+ return Collections .emptyList ();
324+ }
325+ List <com .google .datastore .v1beta3 .Mutation > mutationsPb = new ArrayList <>();
326+ Map <Key , Entity > dedupEntities = new LinkedHashMap <>();
327+ for (FullEntity <?> entity : entities ) {
328+ Preconditions .checkArgument (entity .hasKey (), "Entity %s is missing a key" , entity );
329+ if (entity .key () instanceof Key ) {
330+ Entity completeEntity = Entity .convert ((FullEntity <Key >) entity );
331+ dedupEntities .put (completeEntity .key (), completeEntity );
332+ } else {
323333 mutationsPb .add (
324- com .google .datastore .v1beta3 .Mutation .newBuilder ().setUpsert (e .toPb ()).build ());
334+ com .google .datastore .v1beta3 .Mutation .newBuilder ().setUpsert (entity .toPb ()).build ());
335+ }
336+ }
337+ for (Entity entity : dedupEntities .values ()) {
338+ mutationsPb .add (
339+ com .google .datastore .v1beta3 .Mutation .newBuilder ().setUpsert (entity .toPb ()).build ());
340+ }
341+ com .google .datastore .v1beta3 .CommitResponse commitResponse = commitMutation (mutationsPb );
342+ Iterator <com .google .datastore .v1beta3 .MutationResult > mutationResults =
343+ commitResponse .getMutationResultsList ().iterator ();
344+ ImmutableList .Builder <Entity > responseBuilder = ImmutableList .builder ();
345+ for (FullEntity <?> entity : entities ) {
346+ Entity completeEntity = dedupEntities .get (entity .key ());
347+ if (completeEntity != null ) {
348+ responseBuilder .add (completeEntity );
349+ } else {
350+ responseBuilder .add (
351+ Entity .builder (Key .fromPb (mutationResults .next ().getKey ()), entity ).build ());
325352 }
326- commitMutation (mutationsPb );
327353 }
354+ return responseBuilder .build ();
328355 }
329356
330357 @ Override
0 commit comments