Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename setters/getters/builders for Datastore classes to meet proto conventions #1315

Merged
merged 3 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = Entity.builder(key)
Entity entity = Entity.newBuilder(key)
.set("name", "John Doe")
.set("age", 30)
.set("access_time", DateTime.now())
Expand All @@ -303,12 +303,12 @@ import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.builder(entity)
entity = Entity.newBuilder(entity)
.set("access_time", DateTime.now())
.build();
datastore.update(entity);
Expand Down
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ You can test against a temporary local Datastore by following these steps:

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

3. Run your tests.
Expand Down
8 changes: 4 additions & 4 deletions google-cloud-datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Then add the following code to put an entity in Datastore.
```java
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
Key key = keyFactory.newKey("john.doe@gmail.com");
Entity entity = Entity.builder(key)
Entity entity = Entity.newBuilder(key)
.set("name", "John Doe")
.set("age", 51)
.set("favorite_food", "pizza")
Expand Down Expand Up @@ -126,9 +126,9 @@ import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
Then add the following code to your program:

```java
Query<Entity> query = Query.entityQueryBuilder()
.kind("Person")
.filter(PropertyFilter.eq("favorite_food", "pizza"))
Query<Entity> query = Query.newEntityQueryBuilder()
.setKind("Person")
.setFilter(PropertyFilter.eq("favorite_food", "pizza"))
.build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected BaseDatastoreBatchWriter(String name) {
public final void addWithDeferredIdAllocation(FullEntity<?>... entities) {
validateActive();
for (FullEntity<?> entity : entities) {
IncompleteKey key = entity.key();
IncompleteKey key = entity.getKey();
Preconditions.checkArgument(key != null, "Entity must have a key");
if (key instanceof Key) {
addInternal((FullEntity<Key>) entity);
Expand All @@ -63,10 +63,10 @@ public final void addWithDeferredIdAllocation(FullEntity<?>... entities) {
}

private void addInternal(FullEntity<Key> entity) {
Key key = entity.key();
Key key = entity.getKey();
if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) {
throw newInvalidRequest("Entity with the key %s was already added or updated in this %s",
entity.key(), name);
entity.getKey(), name);
}
if (toDelete.remove(key)) {
toPut.put(key, entity);
Expand All @@ -86,7 +86,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
validateActive();
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
for (FullEntity<?> entity : entities) {
IncompleteKey key = entity.key();
IncompleteKey key = entity.getKey();
Preconditions.checkArgument(key != null, "Entity must have a key");
if (!(key instanceof Key)) {
incompleteKeys.add(key);
Expand All @@ -95,17 +95,17 @@ public final List<Entity> add(FullEntity<?>... entities) {
Iterator<Key> allocated;
if (!incompleteKeys.isEmpty()) {
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
allocated = datastore().allocateId(toAllocate).iterator();
allocated = getDatastore().allocateId(toAllocate).iterator();
} else {
allocated = Collections.emptyIterator();
}
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
for (FullEntity<?> entity : entities) {
if (entity.key() instanceof Key) {
if (entity.getKey() instanceof Key) {
addInternal((FullEntity<Key>) entity);
answer.add(Entity.convert((FullEntity<Key>) entity));
} else {
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build();
addInternal(entityWithAllocatedId);
answer.add(entityWithAllocatedId);
}
Expand All @@ -118,10 +118,10 @@ public final List<Entity> add(FullEntity<?>... entities) {
public final void update(Entity... entities) {
validateActive();
for (Entity entity : entities) {
Key key = entity.key();
Key key = entity.getKey();
if (toDelete.contains(key)) {
throw newInvalidRequest("Entity with the key %s was already deleted in this %s",
entity.key(), name);
entity.getKey(), name);
}
if (toAdd.remove(key) != null || toPut.containsKey(key)) {
toPut.put(key, entity);
Expand All @@ -132,7 +132,7 @@ public final void update(Entity... entities) {
}

private void putInternal(FullEntity<Key> entity) {
Key key = entity.key();
Key key = entity.getKey();
toAdd.remove(key);
toUpdate.remove(key);
toDelete.remove(key);
Expand All @@ -149,7 +149,7 @@ public final Entity put(FullEntity<?> entity) {
public final void putWithDeferredIdAllocation(FullEntity<?>... entities) {
validateActive();
for (FullEntity<?> entity : entities) {
IncompleteKey key = entity.key();
IncompleteKey key = entity.getKey();
Preconditions.checkArgument(key != null, "Entity must have a key");
if (key instanceof Key) {
putInternal(Entity.convert((FullEntity<Key>) entity));
Expand All @@ -165,7 +165,7 @@ public final List<Entity> put(FullEntity<?>... entities) {
validateActive();
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
for (FullEntity<?> entity : entities) {
IncompleteKey key = entity.key();
IncompleteKey key = entity.getKey();
Preconditions.checkArgument(key != null, "Entity must have a key");
if (!(key instanceof Key)) {
incompleteKeys.add(key);
Expand All @@ -174,17 +174,17 @@ public final List<Entity> put(FullEntity<?>... entities) {
Iterator<Key> allocated;
if (!incompleteKeys.isEmpty()) {
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
allocated = datastore().allocateId(toAllocate).iterator();
allocated = getDatastore().allocateId(toAllocate).iterator();
} else {
allocated = Collections.emptyIterator();
}
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
for (FullEntity<?> entity : entities) {
if (entity.key() instanceof Key) {
if (entity.getKey() instanceof Key) {
putInternal((FullEntity<Key>) entity);
answer.add(Entity.convert((FullEntity<Key>) entity));
} else {
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build();
putInternal(entityWithAllocatedId);
answer.add(entityWithAllocatedId);
}
Expand All @@ -204,11 +204,17 @@ public final void delete(Key... keys) {
}

@Override
@Deprecated
public boolean active() {
return isActive();
}

@Override
public boolean isActive() {
return active;
}

protected String name() {
protected String getName() {
return name;
}

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

@Deprecated
protected abstract Datastore datastore();

protected abstract Datastore getDatastore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ public abstract static class Builder<K extends IncompleteKey, B extends Builder<
}

Builder(K key) {
key(key);
setKey(key);
}

Builder(BaseEntity<K> entity) {
this(entity.key, entity);
}

Builder(K key, BaseEntity<?> entity) {
key(key);
properties(entity.properties);
setKey(key);
setProperties(entity.properties);
}

protected K key() {
return key;
}

protected Map<String, Value<?>> properties() {
protected Map<String, Value<?>> setProperties() {
return properties;
}

Expand All @@ -94,22 +94,33 @@ private B self() {
B fill(com.google.datastore.v1.Entity entityPb) {
Map<String, Value<?>> copiedProperties = Maps.newHashMap();
for (Map.Entry<String, com.google.datastore.v1.Value> entry :
entityPb.getProperties().entrySet()) {
entityPb.getPropertiesMap().entrySet()) {
copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue()));
}
properties(copiedProperties);
setProperties(copiedProperties);
if (entityPb.hasKey()) {
key((K) IncompleteKey.fromPb(entityPb.getKey()));
setKey((K) IncompleteKey.fromPb(entityPb.getKey()));
}
return self();
}

protected B properties(Map<String, Value<?>> properties) {
protected B setProperties(Map<String, Value<?>> properties) {
this.properties.putAll(properties);
return self();
}

/**
* Sets the key for the entity.
*/
@Deprecated
public B key(K key) {
return setKey(key);
}

/**
* Sets the key for the entity.
*/
public B setKey(K key) {
this.key = key;
return self();
}
Expand Down Expand Up @@ -401,7 +412,7 @@ public B set(String name, List<? extends Value<?>> values) {
* @param others other values in the list
*/
public B set(String name, Value<?> first, Value<?> second, Value<?>... others) {
properties.put(name, ListValue.builder().addValue(first).addValue(second, others).build());
properties.put(name, ListValue.newBuilder().addValue(first).addValue(second, others).build());
return self();
}

Expand Down Expand Up @@ -454,7 +465,7 @@ public B setNull(String name) {
}

BaseEntity(BaseEntity<K> from) {
this.key = from.key();
this.key = from.getKey();
this.properties = from.properties;
}

Expand Down Expand Up @@ -494,7 +505,15 @@ public boolean hasKey() {
/**
* Returns the associated key or null if it does not have one.
*/
@Deprecated
public K key() {
return getKey();
}

/**
* Returns the associated key or null if it does not have one.
*/
public K getKey() {
return key;
}

Expand Down Expand Up @@ -642,19 +661,26 @@ public Blob getBlob(String name) {
/**
* Returns the properties name.
*/
@Deprecated
public Set<String> names() {
return getNames();
}

/**
* Returns the properties name.
*/
public Set<String> getNames() {
return properties.keySet();
}

ImmutableSortedMap<String, Value<?>> properties() {
ImmutableSortedMap<String, Value<?>> getProperties() {
return properties;
}

final com.google.datastore.v1.Entity toPb() {
com.google.datastore.v1.Entity.Builder entityPb = com.google.datastore.v1.Entity.newBuilder();
Map<String, com.google.datastore.v1.Value> propertiesPb = entityPb.getMutableProperties();
for (Map.Entry<String, Value<?>> entry : properties.entrySet()) {
propertiesPb.put(entry.getKey(), entry.getValue().toPb());
entityPb.putProperties(entry.getKey(), entry.getValue().toPb());
}
if (key != null) {
entityPb.setKey(key.toPb());
Expand Down
Loading