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

Datastore fixes #648

Merged
merged 6 commits into from
Feb 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -128,61 +129,286 @@ public B remove(String name) {
return self();
}

/**
* Sets a property.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, Value<?> value) {
properties.put(name, value);
return self();
}

/**
* Sets a property of type {@link StringValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, String value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link StringValue}.
*
* @param name name of the property
* @param first the first string in the list
* @param second the second string in the list
* @param others other strings in the list
*/
public B set(String name, String first, String second, String... others) {
List<StringValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (String other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link LongValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, long value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link LongValue}.
*
* @param name name of the property
* @param first the first long in the list
* @param second the second long in the list
* @param others other longs in the list
*/
public B set(String name, long first, long second, long... others) {
List<LongValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (long other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link DoubleValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, double value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link DoubleValue}.
*
* @param name name of the property
* @param first the first double in the list
* @param second the second double in the list
* @param others other doubles in the list
*/
public B set(String name, double first, double second, double... others) {
List<DoubleValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (double other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link BooleanValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, boolean value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link BooleanValue}.
*
* @param name name of the property
* @param first the first boolean in the list
* @param second the second boolean in the list
* @param others other booleans in the list
*/
public B set(String name, boolean first, boolean second, boolean... others) {
List<BooleanValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (boolean other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link DateTimeValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, DateTime value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link DateTimeValue}.
*
* @param name name of the property
* @param first the first {@link DateTime} in the list
* @param second the second {@link DateTime} in the list
* @param others other {@link DateTime}s in the list
*/
public B set(String name, DateTime first, DateTime second, DateTime... others) {
List<DateTimeValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (DateTime other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link KeyValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, Key value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link KeyValue}.
*
* @param name name of the property
* @param first the first {@link Key} in the list
* @param second the second {@link Key} in the list
* @param others other {@link Key}s in the list
*/
public B set(String name, Key first, Key second, Key... others) {
List<KeyValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (Key other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link EntityValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, FullEntity<?> value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link EntityValue}.
*
* @param name name of the property
* @param first the first {@link FullEntity} in the list
* @param second the second {@link FullEntity} in the list
* @param others other entities in the list
*/
public B set(String name, FullEntity<?> first, FullEntity<?> second, FullEntity<?>... others) {
List<EntityValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (FullEntity<?> other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@link ListValue}.
*
* @param name name of the property
* @param values list of values associated with the property
*/
public B set(String name, List<? extends Value<?>> values) {
properties.put(name, of(values));
return self();
}

public B set(String name, Value<?> value, Value<?>... other) {
properties.put(name, of(value, other));
/**
* Sets a property of type {@link ListValue}.
*
* @param name name of the property
* @param first the first value in the list
* @param second the second value in the list
* @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());
return self();
}

/**
* Sets a property of type {@link BlobValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, Blob value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link BlobValue}.
*
* @param name name of the property
* @param first the first {@link Blob} in the list
* @param second the second {@link Blob} in the list
* @param others other {@link Blob}s in the list
*/
public B set(String name, Blob first, Blob second, Blob... others) {

This comment was marked as spam.

List<BlobValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (Blob other : others) {
values.add(of(other));
}
properties.put(name, of(values));
return self();
}

/**
* Sets a property of type {@code NullValue}.
*
* @param name name of the property
*/
public B setNull(String name) {
properties.put(name, of());
return self();
Expand Down Expand Up @@ -348,8 +574,8 @@ public <K extends IncompleteKey> FullEntity<K> getEntity(String name) {
* @throws ClassCastException if value is not a list of values
*/
@SuppressWarnings("unchecked")
public List<? extends Value<?>> getList(String name) {
return ((Value<List<? extends Value<?>>>) getValue(name)).get();
public <T extends Value<?>> List<T> getList(String name) {
return (List<T>) getValue(name).get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public String kind() {
return leaf().kind();
}

abstract BaseKey parent();

@Override
public int hashCode() {
return Objects.hash(projectId(), namespace(), path());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ static IncompleteKey fromPb(DatastoreV1.Key keyPb) {
return new IncompleteKey(projectId, namespace, path);
}

/**
* Returns the key's parent.
*/
@Override
public Key parent() {
List<PathElement> ancestors = ancestors();
if (ancestors.isEmpty()) {
return null;
}
PathElement parent = ancestors.get(ancestors.size() - 1);
Key.Builder keyBuilder;
if (parent.hasName()) {
keyBuilder = Key.builder(projectId(), parent.kind(), parent.name());
} else {
keyBuilder = Key.builder(projectId(), parent.kind(), parent.id());
}
String namespace = namespace();
if (namespace != null) {
keyBuilder.namespace(namespace);
}
return keyBuilder.ancestors(ancestors.subList(0, ancestors.size() - 1)).build();
}

public static Builder builder(String projectId, String kind) {
return new Builder(projectId, kind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ private Builder() {
super(ValueType.LIST);
}

public Builder addValue(Value<?> value) {
private void addValueHelper(Value<?> value) {
// see datastore_v1.proto definition for list_value
Preconditions.checkArgument(value.type() != ValueType.LIST, "Cannot contain another list");
listBuilder.add(value);
return this;
}

public Builder addValue(Value<?> first, Value<?>... other) {
addValue(first);
addValueHelper(first);
for (Value<?> value : other) {
addValue(value);
addValueHelper(value);
}
return this;
}
Expand Down
Loading