Skip to content

Commit 12bf706

Browse files
committed
datastore entity change (remove partialentity)
1 parent 73978c0 commit 12bf706

40 files changed

Lines changed: 468 additions & 511 deletions

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<source>1.7</source>
197197
<target>1.7</target>
198198
<encoding>UTF-8</encoding>
199+
<compilerArgument>-Xlint:unchecked</compilerArgument>
199200
</configuration>
200201
</plugin>
201202
<plugin>

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

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
package com.google.gcloud.datastore;
1818

1919
import 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
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public B set(String name, Key value) {
112112
return self();
113113
}
114114

115-
public B set(String name, PartialEntity value) {
115+
public B set(String name, Entity<? extends IncompleteKey> value) {
116116
properties.put(name, of(value));
117117
return self();
118118
}
@@ -200,8 +200,8 @@ public Key getKey(String name) {
200200
}
201201

202202
@SuppressWarnings("unchecked")
203-
public <T extends PartialEntity> T getEntity(String name) {
204-
return (T) ((Value<PartialEntity>) getValue(name)).get();
203+
public <K extends IncompleteKey> Entity<K> getEntity(String name) {
204+
return ((Value<Entity<K>>) getValue(name)).get();
205205
}
206206

207207
@SuppressWarnings("unchecked")

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,36 @@
1616

1717
package com.google.gcloud.datastore;
1818

19+
import java.util.List;
20+
1921
/**
2022
* An interface to represent a batch of write operations.
2123
* All write operation for a batch writer will be applied to the Datastore in one RPC call.
2224
*/
2325
interface DatastoreBatchWriter extends DatastoreWriter {
2426

25-
/**
26-
* {@inheritDoc}
27-
* This operation will be converted to {@link #put} operation for entities that were already
28-
* marked for deletion in this writer.
29-
* @throws com.google.gcloud.datastore.DatastoreServiceException if a given entity already added
30-
* to this writer or if not active
31-
*/
32-
@Override
33-
void add(Entity... entity);
34-
3527
/**
3628
* Datastore add operation.
37-
* This method will automatically allocate id for any entity with an incomplete key.
29+
* This method will also allocate id for any entity with an incomplete key.
30+
* As oppose to {@link #add(Entity)}, this method will defer any necessary id allocation
31+
* to submit time.
3832
*
3933
* @throws IllegalArgumentException if any of the given entities is missing a key
4034
* @throws com.google.gcloud.datastore.DatastoreServiceException if a given entity with a
4135
* complete key was already added to this writer or if not active
4236
*/
43-
void add(PartialEntity... entity);
37+
void addWithDeferredIdAllocation(Entity... entity);
38+
39+
/**
40+
* {@inheritDoc}
41+
* For entities with complete keys that were marked for deletion in this writer the operation
42+
* will be changed to {@link #put}.
43+
* @throws com.google.gcloud.datastore.DatastoreServiceException if a given entity with the
44+
* same complete key was already added to this writer, if writer is not active or
45+
* if id allocation for an entity with an incomplete key failed.
46+
*/
47+
@Override
48+
List<Entity<Key>> add(Entity... entity);
4449

4550
/**
4651
* {@inheritDoc}
@@ -50,7 +55,7 @@ interface DatastoreBatchWriter extends DatastoreWriter {
5055
* deletion in this writer or if not active
5156
*/
5257
@Override
53-
void update(Entity... entity);
58+
void update(Entity<Key>... entity);
5459

5560
/**
5661
* {@inheritDoc}
@@ -67,7 +72,7 @@ interface DatastoreBatchWriter extends DatastoreWriter {
6772
* @throws com.google.gcloud.datastore.DatastoreServiceException if not active
6873
*/
6974
@Override
70-
void put(Entity... entity);
75+
void put(Entity<Key>... entity);
7176

7277
/**
7378
* Returns {@code true} if still active (write operations were not sent to the Datastore).

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

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

1717
package com.google.gcloud.datastore;
1818

19+
import com.google.common.collect.Iterators;
1920
import com.google.common.collect.Maps;
2021

2122
import java.util.ArrayList;
@@ -32,18 +33,31 @@ class DatastoreHelper {
3233
private DatastoreHelper() {
3334
}
3435

36+
37+
static Key allocateId(DatastoreService service, IncompleteKey key) {
38+
return service.allocateId(new IncompleteKey[]{key}).get(0);
39+
}
40+
41+
static Entity<Key> get(DatastoreReader reader, Key key) {
42+
return Iterators.getNext(reader.get(new Key[]{key}), null);
43+
}
44+
45+
static Entity<Key> add(DatastoreWriter writer, Entity<?> entity) {
46+
return writer.add(new Entity<?>[] {entity}).get(0);
47+
}
48+
3549
/**
3650
* Returns a list with a value for each given key (ordered by input).
3751
* A {@code null} would be returned for non-existing keys.
3852
*/
39-
static List<Entity> fetch(DatastoreReader reader, Key... keys) {
40-
Iterator<Entity> entities = reader.get(keys);
41-
Map<Key, Entity> map = Maps.newHashMapWithExpectedSize(keys.length);
53+
static List<Entity<Key>> fetch(DatastoreReader reader, Key... keys) {
54+
Iterator<Entity<Key>> entities = reader.get(keys);
55+
Map<Key, Entity<Key>> map = Maps.newHashMapWithExpectedSize(keys.length);
4256
while (entities.hasNext()) {
43-
Entity entity = entities.next();
57+
Entity<Key> entity = entities.next();
4458
map.put(entity.key(), entity);
4559
}
46-
List<Entity> list = new ArrayList<>(keys.length);
60+
List<Entity<Key>> list = new ArrayList<>(keys.length);
4761
for (Key key : keys) {
4862
// this will include nulls for non-existing keys
4963
list.add(map.get(key));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface DatastoreReader {
2929
*
3030
* @throws DatastoreServiceException upon failure.
3131
*/
32-
Entity get(Key key);
32+
Entity<Key> get(Key key);
3333

3434
/**
3535
* Returns an {@link Entity} for each given {@link Key} that exists in the Datastore.
@@ -41,14 +41,14 @@ public interface DatastoreReader {
4141
* @throws DatastoreServiceException upon failure.
4242
* @see #get(Key)
4343
*/
44-
Iterator<Entity> get(Key... key);
44+
Iterator<Entity<Key>> get(Key... key);
4545

4646
/**
4747
* Returns a list with a value for each given key (ordered by input).
4848
* A {@code null} would be returned for non-existing keys.
4949
* When possible prefer using {@link #get(Key...)} which does not eagerly loads the results.
5050
*/
51-
List<Entity> fetch(Key... keys);
51+
List<Entity<Key>> fetch(Key... keys);
5252

5353
/**
5454
* Submit a {@link Query} and returns its result.

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,50 +81,19 @@ interface TransactionCallable<T> {
8181
*/
8282
List<Key> allocateId(IncompleteKey... key);
8383

84-
/**
85-
* Datastore add operation.
86-
* This method will automatically allocate an id if necessary.
87-
*
88-
* @param entity the entity to add
89-
* @return an {@code Entity} with the same properties and a key that is either newly allocated
90-
* or the same one if was already complete
91-
* @throws DatastoreServiceException upon failure
92-
* @throws IllegalArgumentException if the given entity is missing a key
93-
*/
94-
Entity add(PartialEntity entity);
95-
96-
/**
97-
* Datastore add operation.
98-
* This method will automatically allocate id for any entity with incomplete key.
99-
*
100-
* @return a list of {@code Entity} ordered by input with the same properties and a key that is
101-
* either newly allocated or the same one if was already complete
102-
* @throws DatastoreServiceException upon failure
103-
* @throws IllegalArgumentException if any of the given entities is missing a key
104-
* @see #add(PartialEntity)
105-
*/
106-
List<Entity> add(PartialEntity... entity);
107-
108-
/**
109-
* {@inheritDoc}
110-
* @throws DatastoreServiceException upon failure
111-
*/
112-
@Override
113-
void add(Entity... entity);
114-
11584
/**
11685
* {@inheritDoc}
11786
* @throws DatastoreServiceException upon failure
11887
*/
11988
@Override
120-
void update(Entity... entity);
89+
void update(Entity<Key>... entity);
12190

12291
/**
12392
* {@inheritDoc}
12493
* @throws DatastoreServiceException upon failure
12594
*/
12695
@Override
127-
void put(Entity... entity);
96+
void put(Entity<Key>... entity);
12897

12998
/**
13099
* {@inheritDoc}

0 commit comments

Comments
 (0)