Skip to content

Commit

Permalink
Rename setters/getters/builders for Datastore classes to meet proto c…
Browse files Browse the repository at this point in the history
…onventions
  • Loading branch information
mziccard committed Oct 13, 2016
1 parent 513ca70 commit c1deb59
Show file tree
Hide file tree
Showing 77 changed files with 2,414 additions and 675 deletions.
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 active;
}

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

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,26 +94,38 @@ 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) {
this.key = key;
return self();
}

/**
* Sets the key for the entity.
*/
public B setKey(K key) {
this.key = key;
return self();
}

/**
* Clears all the properties.
*/
Expand Down Expand Up @@ -401,7 +413,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 +466,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,10 +506,18 @@ public boolean hasKey() {
/**
* Returns the associated key or null if it does not have one.
*/
@Deprecated
public K key() {
return key;
}

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

/**
* Returns {@code true} if the entity contains a property with the given {@code name}.
*/
Expand Down Expand Up @@ -642,19 +662,26 @@ public Blob getBlob(String name) {
/**
* Returns the properties name.
*/
@Deprecated
public Set<String> names() {
return properties.keySet();
}

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

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

0 comments on commit c1deb59

Please sign in to comment.