Skip to content

Commit ae5f87f

Browse files
committed
Rename Entity<K> to FullEntity<K> and provide an Entity as a shorthand for FullEntity<K> (+ a guaranty to have a key).
1 parent 3dbb485 commit ae5f87f

26 files changed

Lines changed: 547 additions & 363 deletions

src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@
1818

1919
import com.google.api.services.datastore.DatastoreV1;
2020
import com.google.common.base.Preconditions;
21+
import com.google.common.collect.Iterables;
2122
import com.google.common.collect.Lists;
2223

23-
import java.util.*;
24+
import java.util.Collections;
25+
import java.util.Iterator;
26+
import java.util.LinkedHashMap;
27+
import java.util.LinkedHashSet;
28+
import java.util.LinkedList;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Set;
2432

2533
/**
2634
* Base class for DatastoreBatchWriter.
2735
*/
2836
public abstract class BaseDatastoreBatchWriter implements DatastoreBatchWriter {
2937

3038
private final String name;
31-
private final Map<Key, Entity> toAdd = new LinkedHashMap<>();
32-
private final List<Entity<IncompleteKey>> toAddAutoId = new LinkedList<>();
33-
private final Map<Key, Entity> toUpdate = new LinkedHashMap<>();
34-
private final Map<Key, Entity> toPut = new LinkedHashMap<>();
39+
private final Map<Key, FullEntity<Key>> toAdd = new LinkedHashMap<>();
40+
private final List<FullEntity<IncompleteKey>> toAddAutoId = new LinkedList<>();
41+
private final Map<Key, FullEntity<Key>> toUpdate = new LinkedHashMap<>();
42+
private final Map<Key, FullEntity<Key>> toPut = new LinkedHashMap<>();
3543
private final Set<Key> toDelete = new LinkedHashSet<>();
3644
private boolean active = true;
3745

@@ -41,20 +49,20 @@ protected BaseDatastoreBatchWriter(String name) {
4149

4250
@SuppressWarnings("unchecked")
4351
@Override
44-
public final void addWithDeferredIdAllocation(Entity... entities) {
52+
public final void addWithDeferredIdAllocation(FullEntity<?>... entities) {
4553
validateActive();
46-
for (Entity<?> entity : entities) {
54+
for (FullEntity<?> entity : entities) {
4755
IncompleteKey key = entity.key();
4856
Preconditions.checkArgument(key != null, "Entity must have a key");
4957
if (key instanceof Key) {
50-
addInternal((Entity<Key>) entity);
58+
addInternal((FullEntity<Key>) entity);
5159
} else {
52-
toAddAutoId.add((Entity<IncompleteKey>) entity);
60+
toAddAutoId.add((FullEntity<IncompleteKey>) entity);
5361
}
5462
}
5563
}
5664

57-
private void addInternal(Entity<Key> entity) {
65+
private void addInternal(FullEntity<Key> entity) {
5866
Key key = entity.key();
5967
if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) {
6068
throw newInvalidRequest("Entity with the key %s was already added or updated in this %s",
@@ -68,38 +76,37 @@ private void addInternal(Entity<Key> entity) {
6876
}
6977

7078
@Override
71-
public final Entity<Key> add(Entity entity) {
79+
public final Entity add(FullEntity<?> entity) {
7280
return DatastoreHelper.add(this, entity);
7381
}
7482

7583
@SuppressWarnings("unchecked")
7684
@Override
77-
public final List<Entity<Key>> add(Entity... entities) {
85+
public final List<Entity> add(FullEntity<?>... entities) {
7886
validateActive();
79-
ArrayList<IncompleteKey> incompleteKeys = new ArrayList<>();
80-
for (Entity<?> entity : entities) {
87+
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
88+
for (FullEntity<?> entity : entities) {
8189
IncompleteKey key = entity.key();
8290
Preconditions.checkArgument(key != null, "Entity must have a key");
8391
if (key instanceof Key) {
84-
addInternal((Entity<Key>) entity);
92+
addInternal((FullEntity<Key>) entity);
8593
} else {
8694
incompleteKeys.add(key);
8795
}
8896
}
8997
Iterator<Key> allocated;
9098
if (!incompleteKeys.isEmpty()) {
91-
IncompleteKey[] toAllocate = incompleteKeys.toArray(new IncompleteKey[incompleteKeys.size()]);
99+
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
92100
allocated = datastore().allocateId(toAllocate).iterator();
93101
} else {
94102
allocated = Collections.emptyIterator();
95103
}
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);
104+
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
105+
for (FullEntity<?> entity : entities) {
106+
if (entity.key() instanceof Key) {
107+
answer.add(Entity.convert((FullEntity<Key>) entity));
101108
} else {
102-
Entity<Key> entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
109+
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
103110
addInternal(entityWithAllocatedId);
104111
answer.add(entityWithAllocatedId);
105112
}
@@ -109,9 +116,9 @@ public final List<Entity<Key>> add(Entity... entities) {
109116

110117
@SafeVarargs
111118
@Override
112-
public final void update(Entity<Key>... entities) {
119+
public final void update(Entity... entities) {
113120
validateActive();
114-
for (Entity<Key> entity : entities) {
121+
for (Entity entity : entities) {
115122
Key key = entity.key();
116123
if (toDelete.contains(key)) {
117124
throw newInvalidRequest("Entity with the key %s was already deleted in this %s",
@@ -127,9 +134,9 @@ public final void update(Entity<Key>... entities) {
127134

128135
@SafeVarargs
129136
@Override
130-
public final void put(Entity<Key>... entities) {
137+
public final void put(Entity... entities) {
131138
validateActive();
132-
for (Entity<Key> entity : entities) {
139+
for (Entity entity : entities) {
133140
Key key = entity.key();
134141
toAdd.remove(key);
135142
toUpdate.remove(key);
@@ -158,19 +165,19 @@ protected String name() {
158165
return name;
159166
}
160167

161-
protected Map<Key, Entity> toAdd() {
168+
protected Map<Key, FullEntity<Key>> toAdd() {
162169
return toAdd;
163170
}
164171

165-
protected List<Entity<IncompleteKey>> toAddAutoId() {
172+
protected List<FullEntity<IncompleteKey>> toAddAutoId() {
166173
return toAddAutoId;
167174
}
168175

169-
protected Map<Key, Entity> toUpdate() {
176+
protected Map<Key, FullEntity<Key>> toUpdate() {
170177
return toUpdate;
171178
}
172179

173-
protected Map<Key, Entity> toPut() {
180+
protected Map<Key, FullEntity<Key>> toPut() {
174181
return toPut;
175182
}
176183

@@ -194,16 +201,16 @@ protected DatastoreServiceException newInvalidRequest(String msg, Object... para
194201

195202
protected DatastoreV1.Mutation.Builder toMutationPb() {
196203
DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder();
197-
for (Entity<IncompleteKey> entity : toAddAutoId()) {
204+
for (FullEntity<IncompleteKey> entity : toAddAutoId()) {
198205
mutationPb.addInsertAutoId(entity.toPb());
199206
}
200-
for (Entity entity : toAdd().values()) {
207+
for (FullEntity<Key> entity : toAdd().values()) {
201208
mutationPb.addInsert(entity.toPb());
202209
}
203-
for (Entity entity : toUpdate().values()) {
210+
for (FullEntity<Key> entity : toUpdate().values()) {
204211
mutationPb.addUpdate(entity.toPb());
205212
}
206-
for (Entity entity : toPut().values()) {
213+
for (FullEntity<Key> entity : toPut().values()) {
207214
mutationPb.addUpsert(entity.toPb());
208215
}
209216
for (Key key : toDelete()) {

0 commit comments

Comments
 (0)