Skip to content

Commit 7bcfe3e

Browse files
authored
Rename setters/getters/builders for Datastore classes to meet proto conventions (#1315)
* Rename setters/getters/builders for Datastore classes to meet proto conventions * Update Datastore examples, snippets and READMEs to use renamed getters/setters/builders * Make deprecated methods call renamed ones
1 parent 0f4a178 commit 7bcfe3e

File tree

92 files changed

+2678
-852
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+2678
-852
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ import com.google.cloud.datastore.Key;
282282
import com.google.cloud.datastore.KeyFactory;
283283
284284
Datastore datastore = DatastoreOptions.defaultInstance().service();
285-
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
285+
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
286286
Key key = keyFactory.newKey("keyName");
287-
Entity entity = Entity.builder(key)
287+
Entity entity = Entity.newBuilder(key)
288288
.set("name", "John Doe")
289289
.set("age", 30)
290290
.set("access_time", DateTime.now())
@@ -303,12 +303,12 @@ import com.google.cloud.datastore.Key;
303303
import com.google.cloud.datastore.KeyFactory;
304304
305305
Datastore datastore = DatastoreOptions.defaultInstance().service();
306-
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
306+
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
307307
Key key = keyFactory.newKey("keyName");
308308
Entity entity = datastore.get(key);
309309
if (entity != null) {
310310
System.out.println("Updating access_time for " + entity.getString("name"));
311-
entity = Entity.builder(entity)
311+
entity = Entity.newBuilder(entity)
312312
.set("access_time", DateTime.now())
313313
.build();
314314
datastore.update(entity);

TESTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ You can test against a temporary local Datastore by following these steps:
8484

8585
2. Create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
8686
```java
87-
Datastore localDatastore = helper.options().service();
87+
Datastore localDatastore = helper.getOptions().service();
8888
```
8989

9090
3. Run your tests.

google-cloud-datastore/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Then add the following code to put an entity in Datastore.
9292
```java
9393
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
9494
Key key = keyFactory.newKey("[email protected]");
95-
Entity entity = Entity.builder(key)
95+
Entity entity = Entity.newBuilder(key)
9696
.set("name", "John Doe")
9797
.set("age", 51)
9898
.set("favorite_food", "pizza")
@@ -126,9 +126,9 @@ import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
126126
Then add the following code to your program:
127127

128128
```java
129-
Query<Entity> query = Query.entityQueryBuilder()
130-
.kind("Person")
131-
.filter(PropertyFilter.eq("favorite_food", "pizza"))
129+
Query<Entity> query = Query.newEntityQueryBuilder()
130+
.setKind("Person")
131+
.setFilter(PropertyFilter.eq("favorite_food", "pizza"))
132132
.build();
133133
QueryResults<Entity> results = datastore.run(query);
134134
while (results.hasNext()) {

google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseDatastoreBatchWriter.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected BaseDatastoreBatchWriter(String name) {
5252
public final void addWithDeferredIdAllocation(FullEntity<?>... entities) {
5353
validateActive();
5454
for (FullEntity<?> entity : entities) {
55-
IncompleteKey key = entity.key();
55+
IncompleteKey key = entity.getKey();
5656
Preconditions.checkArgument(key != null, "Entity must have a key");
5757
if (key instanceof Key) {
5858
addInternal((FullEntity<Key>) entity);
@@ -63,10 +63,10 @@ public final void addWithDeferredIdAllocation(FullEntity<?>... entities) {
6363
}
6464

6565
private void addInternal(FullEntity<Key> entity) {
66-
Key key = entity.key();
66+
Key key = entity.getKey();
6767
if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) {
6868
throw newInvalidRequest("Entity with the key %s was already added or updated in this %s",
69-
entity.key(), name);
69+
entity.getKey(), name);
7070
}
7171
if (toDelete.remove(key)) {
7272
toPut.put(key, entity);
@@ -86,7 +86,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
8686
validateActive();
8787
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
8888
for (FullEntity<?> entity : entities) {
89-
IncompleteKey key = entity.key();
89+
IncompleteKey key = entity.getKey();
9090
Preconditions.checkArgument(key != null, "Entity must have a key");
9191
if (!(key instanceof Key)) {
9292
incompleteKeys.add(key);
@@ -95,17 +95,17 @@ public final List<Entity> add(FullEntity<?>... entities) {
9595
Iterator<Key> allocated;
9696
if (!incompleteKeys.isEmpty()) {
9797
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
98-
allocated = datastore().allocateId(toAllocate).iterator();
98+
allocated = getDatastore().allocateId(toAllocate).iterator();
9999
} else {
100100
allocated = Collections.emptyIterator();
101101
}
102102
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
103103
for (FullEntity<?> entity : entities) {
104-
if (entity.key() instanceof Key) {
104+
if (entity.getKey() instanceof Key) {
105105
addInternal((FullEntity<Key>) entity);
106106
answer.add(Entity.convert((FullEntity<Key>) entity));
107107
} else {
108-
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
108+
Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build();
109109
addInternal(entityWithAllocatedId);
110110
answer.add(entityWithAllocatedId);
111111
}
@@ -118,10 +118,10 @@ public final List<Entity> add(FullEntity<?>... entities) {
118118
public final void update(Entity... entities) {
119119
validateActive();
120120
for (Entity entity : entities) {
121-
Key key = entity.key();
121+
Key key = entity.getKey();
122122
if (toDelete.contains(key)) {
123123
throw newInvalidRequest("Entity with the key %s was already deleted in this %s",
124-
entity.key(), name);
124+
entity.getKey(), name);
125125
}
126126
if (toAdd.remove(key) != null || toPut.containsKey(key)) {
127127
toPut.put(key, entity);
@@ -132,7 +132,7 @@ public final void update(Entity... entities) {
132132
}
133133

134134
private void putInternal(FullEntity<Key> entity) {
135-
Key key = entity.key();
135+
Key key = entity.getKey();
136136
toAdd.remove(key);
137137
toUpdate.remove(key);
138138
toDelete.remove(key);
@@ -149,7 +149,7 @@ public final Entity put(FullEntity<?> entity) {
149149
public final void putWithDeferredIdAllocation(FullEntity<?>... entities) {
150150
validateActive();
151151
for (FullEntity<?> entity : entities) {
152-
IncompleteKey key = entity.key();
152+
IncompleteKey key = entity.getKey();
153153
Preconditions.checkArgument(key != null, "Entity must have a key");
154154
if (key instanceof Key) {
155155
putInternal(Entity.convert((FullEntity<Key>) entity));
@@ -165,7 +165,7 @@ public final List<Entity> put(FullEntity<?>... entities) {
165165
validateActive();
166166
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
167167
for (FullEntity<?> entity : entities) {
168-
IncompleteKey key = entity.key();
168+
IncompleteKey key = entity.getKey();
169169
Preconditions.checkArgument(key != null, "Entity must have a key");
170170
if (!(key instanceof Key)) {
171171
incompleteKeys.add(key);
@@ -174,17 +174,17 @@ public final List<Entity> put(FullEntity<?>... entities) {
174174
Iterator<Key> allocated;
175175
if (!incompleteKeys.isEmpty()) {
176176
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
177-
allocated = datastore().allocateId(toAllocate).iterator();
177+
allocated = getDatastore().allocateId(toAllocate).iterator();
178178
} else {
179179
allocated = Collections.emptyIterator();
180180
}
181181
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
182182
for (FullEntity<?> entity : entities) {
183-
if (entity.key() instanceof Key) {
183+
if (entity.getKey() instanceof Key) {
184184
putInternal((FullEntity<Key>) entity);
185185
answer.add(Entity.convert((FullEntity<Key>) entity));
186186
} else {
187-
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
187+
Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build();
188188
putInternal(entityWithAllocatedId);
189189
answer.add(entityWithAllocatedId);
190190
}
@@ -204,11 +204,17 @@ public final void delete(Key... keys) {
204204
}
205205

206206
@Override
207+
@Deprecated
207208
public boolean active() {
209+
return isActive();
210+
}
211+
212+
@Override
213+
public boolean isActive() {
208214
return active;
209215
}
210216

211-
protected String name() {
217+
protected String getName() {
212218
return name;
213219
}
214220

@@ -270,5 +276,8 @@ protected List<com.google.datastore.v1.Mutation> toMutationPbList() {
270276
return mutationsPb;
271277
}
272278

279+
@Deprecated
273280
protected abstract Datastore datastore();
281+
282+
protected abstract Datastore getDatastore();
274283
}

google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseEntity.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,23 @@ public abstract static class Builder<K extends IncompleteKey, B extends Builder<
6565
}
6666

6767
Builder(K key) {
68-
key(key);
68+
setKey(key);
6969
}
7070

7171
Builder(BaseEntity<K> entity) {
7272
this(entity.key, entity);
7373
}
7474

7575
Builder(K key, BaseEntity<?> entity) {
76-
key(key);
77-
properties(entity.properties);
76+
setKey(key);
77+
setProperties(entity.properties);
7878
}
7979

8080
protected K key() {
8181
return key;
8282
}
8383

84-
protected Map<String, Value<?>> properties() {
84+
protected Map<String, Value<?>> setProperties() {
8585
return properties;
8686
}
8787

@@ -94,22 +94,33 @@ private B self() {
9494
B fill(com.google.datastore.v1.Entity entityPb) {
9595
Map<String, Value<?>> copiedProperties = Maps.newHashMap();
9696
for (Map.Entry<String, com.google.datastore.v1.Value> entry :
97-
entityPb.getProperties().entrySet()) {
97+
entityPb.getPropertiesMap().entrySet()) {
9898
copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue()));
9999
}
100-
properties(copiedProperties);
100+
setProperties(copiedProperties);
101101
if (entityPb.hasKey()) {
102-
key((K) IncompleteKey.fromPb(entityPb.getKey()));
102+
setKey((K) IncompleteKey.fromPb(entityPb.getKey()));
103103
}
104104
return self();
105105
}
106106

107-
protected B properties(Map<String, Value<?>> properties) {
107+
protected B setProperties(Map<String, Value<?>> properties) {
108108
this.properties.putAll(properties);
109109
return self();
110110
}
111111

112+
/**
113+
* Sets the key for the entity.
114+
*/
115+
@Deprecated
112116
public B key(K key) {
117+
return setKey(key);
118+
}
119+
120+
/**
121+
* Sets the key for the entity.
122+
*/
123+
public B setKey(K key) {
113124
this.key = key;
114125
return self();
115126
}
@@ -401,7 +412,7 @@ public B set(String name, List<? extends Value<?>> values) {
401412
* @param others other values in the list
402413
*/
403414
public B set(String name, Value<?> first, Value<?> second, Value<?>... others) {
404-
properties.put(name, ListValue.builder().addValue(first).addValue(second, others).build());
415+
properties.put(name, ListValue.newBuilder().addValue(first).addValue(second, others).build());
405416
return self();
406417
}
407418

@@ -454,7 +465,7 @@ public B setNull(String name) {
454465
}
455466

456467
BaseEntity(BaseEntity<K> from) {
457-
this.key = from.key();
468+
this.key = from.getKey();
458469
this.properties = from.properties;
459470
}
460471

@@ -494,7 +505,15 @@ public boolean hasKey() {
494505
/**
495506
* Returns the associated key or null if it does not have one.
496507
*/
508+
@Deprecated
497509
public K key() {
510+
return getKey();
511+
}
512+
513+
/**
514+
* Returns the associated key or null if it does not have one.
515+
*/
516+
public K getKey() {
498517
return key;
499518
}
500519

@@ -642,19 +661,26 @@ public Blob getBlob(String name) {
642661
/**
643662
* Returns the properties name.
644663
*/
664+
@Deprecated
645665
public Set<String> names() {
666+
return getNames();
667+
}
668+
669+
/**
670+
* Returns the properties name.
671+
*/
672+
public Set<String> getNames() {
646673
return properties.keySet();
647674
}
648675

649-
ImmutableSortedMap<String, Value<?>> properties() {
676+
ImmutableSortedMap<String, Value<?>> getProperties() {
650677
return properties;
651678
}
652679

653680
final com.google.datastore.v1.Entity toPb() {
654681
com.google.datastore.v1.Entity.Builder entityPb = com.google.datastore.v1.Entity.newBuilder();
655-
Map<String, com.google.datastore.v1.Value> propertiesPb = entityPb.getMutableProperties();
656682
for (Map.Entry<String, Value<?>> entry : properties.entrySet()) {
657-
propertiesPb.put(entry.getKey(), entry.getValue().toPb());
683+
entityPb.putProperties(entry.getKey(), entry.getValue().toPb());
658684
}
659685
if (key != null) {
660686
entityPb.setKey(key.toPb());

0 commit comments

Comments
 (0)