1717package com .google .gcloud .datastore ;
1818
1919import com .google .api .services .datastore .DatastoreV1 ;
20+ import com .google .common .base .Preconditions ;
21+ import com .google .common .collect .Lists ;
2022
21- import java .util .LinkedHashMap ;
22- import java .util .LinkedHashSet ;
23- import java .util .LinkedList ;
24- import java .util .List ;
25- import java .util .Map ;
26- import java .util .Set ;
23+ import java .util .*;
2724
2825/**
2926 * Base class for DatastoreBatchWriter.
@@ -32,7 +29,7 @@ public abstract class BaseDatastoreBatchWriter implements DatastoreBatchWriter {
3229
3330 private final String name ;
3431 private final Map <Key , Entity > toAdd = new LinkedHashMap <>();
35- private final List <PartialEntity > toAddAutoId = new LinkedList <>();
32+ private final List <Entity < IncompleteKey > > toAddAutoId = new LinkedList <>();
3633 private final Map <Key , Entity > toUpdate = new LinkedHashMap <>();
3734 private final Map <Key , Entity > toPut = new LinkedHashMap <>();
3835 private final Set <Key > toDelete = new LinkedHashSet <>();
@@ -42,39 +39,79 @@ protected BaseDatastoreBatchWriter(String name) {
4239 this .name = name ;
4340 }
4441
42+ @ SuppressWarnings ("unchecked" )
4543 @ Override
46- public void add (Entity ... entities ) {
44+ public final void addWithDeferredIdAllocation (Entity ... entities ) {
4745 validateActive ();
48- for (Entity entity : entities ) {
49- Key key = entity .key ();
50- if (toAdd .containsKey (key ) || toUpdate .containsKey (key ) || toPut .containsKey (key )) {
51- throw newInvalidRequest ("Entity with the key %s was already added or updated in this %s" ,
52- entity .key (), name );
53- }
54- if (toDelete .remove (key )) {
55- toPut .put (key , entity );
46+ for (Entity <?> entity : entities ) {
47+ IncompleteKey key = entity .key ();
48+ Preconditions .checkArgument (key != null , "Entity must have a key" );
49+ if (key instanceof Key ) {
50+ addInternal ((Entity <Key >) entity );
5651 } else {
57- toAdd . put ( key , entity );
52+ toAddAutoId . add (( Entity < IncompleteKey >) entity );
5853 }
5954 }
6055 }
6156
57+ private void addInternal (Entity <Key > entity ) {
58+ Key key = entity .key ();
59+ if (toAdd .containsKey (key ) || toUpdate .containsKey (key ) || toPut .containsKey (key )) {
60+ throw newInvalidRequest ("Entity with the key %s was already added or updated in this %s" ,
61+ entity .key (), name );
62+ }
63+ if (toDelete .remove (key )) {
64+ toPut .put (key , entity );
65+ } else {
66+ toAdd .put (key , entity );
67+ }
68+ }
69+
6270 @ Override
63- public void add (PartialEntity ... entities ) {
71+ public final Entity <Key > add (Entity entity ) {
72+ return DatastoreHelper .add (this , entity );
73+ }
74+
75+ @ SuppressWarnings ("unchecked" )
76+ @ Override
77+ public final List <Entity <Key >> add (Entity ... entities ) {
6478 validateActive ();
65- for (PartialEntity entity : entities ) {
66- if (entity instanceof Entity ) {
67- add ((Entity ) entity );
79+ ArrayList <IncompleteKey > incompleteKeys = new ArrayList <>();
80+ for (Entity <?> entity : entities ) {
81+ IncompleteKey key = entity .key ();
82+ Preconditions .checkArgument (key != null , "Entity must have a key" );
83+ if (key instanceof Key ) {
84+ addInternal ((Entity <Key >) entity );
85+ } else {
86+ incompleteKeys .add (key );
87+ }
88+ }
89+ Iterator <Key > allocated ;
90+ if (!incompleteKeys .isEmpty ()) {
91+ IncompleteKey [] toAllocate = incompleteKeys .toArray (new IncompleteKey [incompleteKeys .size ()]);
92+ allocated = datastore ().allocateId (toAllocate ).iterator ();
93+ } else {
94+ allocated = Collections .emptyIterator ();
95+ }
96+ List <Entity <Key >> answer = Lists .newArrayListWithExpectedSize (entities .length );
97+ for (Entity <?> entity : entities ) {
98+ IncompleteKey key = entity .key ();
99+ if (key instanceof Key ) {
100+ answer .add ((Entity <Key >) entity );
68101 } else {
69- toAddAutoId .add (entity );
102+ Entity <Key > entityWithAllocatedId = Entity .builder (allocated .next (), entity ).build ();
103+ addInternal (entityWithAllocatedId );
104+ answer .add (entityWithAllocatedId );
70105 }
71106 }
107+ return answer ;
72108 }
73109
110+ @ SafeVarargs
74111 @ Override
75- public void update (Entity ... entities ) {
112+ public final void update (Entity < Key > ... entities ) {
76113 validateActive ();
77- for (Entity entity : entities ) {
114+ for (Entity < Key > entity : entities ) {
78115 Key key = entity .key ();
79116 if (toDelete .contains (key )) {
80117 throw newInvalidRequest ("Entity with the key %s was already deleted in this %s" ,
@@ -88,10 +125,11 @@ public void update(Entity... entities) {
88125 }
89126 }
90127
128+ @ SafeVarargs
91129 @ Override
92- public void put (Entity ... entities ) {
130+ public final void put (Entity < Key > ... entities ) {
93131 validateActive ();
94- for (Entity entity : entities ) {
132+ for (Entity < Key > entity : entities ) {
95133 Key key = entity .key ();
96134 toAdd .remove (key );
97135 toUpdate .remove (key );
@@ -101,7 +139,7 @@ public void put(Entity... entities) {
101139 }
102140
103141 @ Override
104- public void delete (Key ... keys ) {
142+ public final void delete (Key ... keys ) {
105143 validateActive ();
106144 for (Key key : keys ) {
107145 toAdd .remove (key );
@@ -124,7 +162,7 @@ protected Map<Key, Entity> toAdd() {
124162 return toAdd ;
125163 }
126164
127- protected List <PartialEntity > toAddAutoId () {
165+ protected List <Entity < IncompleteKey > > toAddAutoId () {
128166 return toAddAutoId ;
129167 }
130168
@@ -156,7 +194,7 @@ protected DatastoreServiceException newInvalidRequest(String msg, Object... para
156194
157195 protected DatastoreV1 .Mutation .Builder toMutationPb () {
158196 DatastoreV1 .Mutation .Builder mutationPb = DatastoreV1 .Mutation .newBuilder ();
159- for (PartialEntity entity : toAddAutoId ()) {
197+ for (Entity < IncompleteKey > entity : toAddAutoId ()) {
160198 mutationPb .addInsertAutoId (entity .toPb ());
161199 }
162200 for (Entity entity : toAdd ().values ()) {
@@ -173,4 +211,6 @@ protected DatastoreV1.Mutation.Builder toMutationPb() {
173211 }
174212 return mutationPb ;
175213 }
214+
215+ protected abstract DatastoreService datastore ();
176216}
0 commit comments