Skip to content

Commit 04ab56e

Browse files
committed
Merge pull request #648 from ajkannan/datastore-fixes
Datastore fixes
2 parents 9412d69 + 662f57f commit 04ab56e

10 files changed

Lines changed: 332 additions & 24 deletions

File tree

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

Lines changed: 230 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.protobuf.InvalidProtocolBufferException;
3434

3535
import java.util.HashMap;
36+
import java.util.LinkedList;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.Objects;
@@ -128,61 +129,286 @@ public B remove(String name) {
128129
return self();
129130
}
130131

132+
/**
133+
* Sets a property.
134+
*
135+
* @param name name of the property
136+
* @param value value associated with the property
137+
*/
131138
public B set(String name, Value<?> value) {
132139
properties.put(name, value);
133140
return self();
134141
}
135142

143+
/**
144+
* Sets a property of type {@link StringValue}.
145+
*
146+
* @param name name of the property
147+
* @param value value associated with the property
148+
*/
136149
public B set(String name, String value) {
137150
properties.put(name, of(value));
138151
return self();
139152
}
140153

154+
/**
155+
* Sets a list property containing elements of type {@link StringValue}.
156+
*
157+
* @param name name of the property
158+
* @param first the first string in the list
159+
* @param second the second string in the list
160+
* @param others other strings in the list
161+
*/
162+
public B set(String name, String first, String second, String... others) {
163+
List<StringValue> values = new LinkedList<>();
164+
values.add(of(first));
165+
values.add(of(second));
166+
for (String other : others) {
167+
values.add(of(other));
168+
}
169+
properties.put(name, of(values));
170+
return self();
171+
}
172+
173+
/**
174+
* Sets a property of type {@link LongValue}.
175+
*
176+
* @param name name of the property
177+
* @param value value associated with the property
178+
*/
141179
public B set(String name, long value) {
142180
properties.put(name, of(value));
143181
return self();
144182
}
145183

184+
/**
185+
* Sets a list property containing elements of type {@link LongValue}.
186+
*
187+
* @param name name of the property
188+
* @param first the first long in the list
189+
* @param second the second long in the list
190+
* @param others other longs in the list
191+
*/
192+
public B set(String name, long first, long second, long... others) {
193+
List<LongValue> values = new LinkedList<>();
194+
values.add(of(first));
195+
values.add(of(second));
196+
for (long other : others) {
197+
values.add(of(other));
198+
}
199+
properties.put(name, of(values));
200+
return self();
201+
}
202+
203+
/**
204+
* Sets a property of type {@link DoubleValue}.
205+
*
206+
* @param name name of the property
207+
* @param value value associated with the property
208+
*/
146209
public B set(String name, double value) {
147210
properties.put(name, of(value));
148211
return self();
149212
}
150213

214+
/**
215+
* Sets a list property containing elements of type {@link DoubleValue}.
216+
*
217+
* @param name name of the property
218+
* @param first the first double in the list
219+
* @param second the second double in the list
220+
* @param others other doubles in the list
221+
*/
222+
public B set(String name, double first, double second, double... others) {
223+
List<DoubleValue> values = new LinkedList<>();
224+
values.add(of(first));
225+
values.add(of(second));
226+
for (double other : others) {
227+
values.add(of(other));
228+
}
229+
properties.put(name, of(values));
230+
return self();
231+
}
232+
233+
/**
234+
* Sets a property of type {@link BooleanValue}.
235+
*
236+
* @param name name of the property
237+
* @param value value associated with the property
238+
*/
151239
public B set(String name, boolean value) {
152240
properties.put(name, of(value));
153241
return self();
154242
}
155243

244+
/**
245+
* Sets a list property containing elements of type {@link BooleanValue}.
246+
*
247+
* @param name name of the property
248+
* @param first the first boolean in the list
249+
* @param second the second boolean in the list
250+
* @param others other booleans in the list
251+
*/
252+
public B set(String name, boolean first, boolean second, boolean... others) {
253+
List<BooleanValue> values = new LinkedList<>();
254+
values.add(of(first));
255+
values.add(of(second));
256+
for (boolean other : others) {
257+
values.add(of(other));
258+
}
259+
properties.put(name, of(values));
260+
return self();
261+
}
262+
263+
/**
264+
* Sets a property of type {@link DateTimeValue}.
265+
*
266+
* @param name name of the property
267+
* @param value value associated with the property
268+
*/
156269
public B set(String name, DateTime value) {
157270
properties.put(name, of(value));
158271
return self();
159272
}
160273

274+
/**
275+
* Sets a list property containing elements of type {@link DateTimeValue}.
276+
*
277+
* @param name name of the property
278+
* @param first the first {@link DateTime} in the list
279+
* @param second the second {@link DateTime} in the list
280+
* @param others other {@link DateTime}s in the list
281+
*/
282+
public B set(String name, DateTime first, DateTime second, DateTime... others) {
283+
List<DateTimeValue> values = new LinkedList<>();
284+
values.add(of(first));
285+
values.add(of(second));
286+
for (DateTime other : others) {
287+
values.add(of(other));
288+
}
289+
properties.put(name, of(values));
290+
return self();
291+
}
292+
293+
/**
294+
* Sets a property of type {@link KeyValue}.
295+
*
296+
* @param name name of the property
297+
* @param value value associated with the property
298+
*/
161299
public B set(String name, Key value) {
162300
properties.put(name, of(value));
163301
return self();
164302
}
165303

304+
/**
305+
* Sets a list property containing elements of type {@link KeyValue}.
306+
*
307+
* @param name name of the property
308+
* @param first the first {@link Key} in the list
309+
* @param second the second {@link Key} in the list
310+
* @param others other {@link Key}s in the list
311+
*/
312+
public B set(String name, Key first, Key second, Key... others) {
313+
List<KeyValue> values = new LinkedList<>();
314+
values.add(of(first));
315+
values.add(of(second));
316+
for (Key other : others) {
317+
values.add(of(other));
318+
}
319+
properties.put(name, of(values));
320+
return self();
321+
}
322+
323+
/**
324+
* Sets a property of type {@link EntityValue}.
325+
*
326+
* @param name name of the property
327+
* @param value value associated with the property
328+
*/
166329
public B set(String name, FullEntity<?> value) {
167330
properties.put(name, of(value));
168331
return self();
169332
}
170333

334+
/**
335+
* Sets a list property containing elements of type {@link EntityValue}.
336+
*
337+
* @param name name of the property
338+
* @param first the first {@link FullEntity} in the list
339+
* @param second the second {@link FullEntity} in the list
340+
* @param others other entities in the list
341+
*/
342+
public B set(String name, FullEntity<?> first, FullEntity<?> second, FullEntity<?>... others) {
343+
List<EntityValue> values = new LinkedList<>();
344+
values.add(of(first));
345+
values.add(of(second));
346+
for (FullEntity<?> other : others) {
347+
values.add(of(other));
348+
}
349+
properties.put(name, of(values));
350+
return self();
351+
}
352+
353+
/**
354+
* Sets a property of type {@link ListValue}.
355+
*
356+
* @param name name of the property
357+
* @param values list of values associated with the property
358+
*/
171359
public B set(String name, List<? extends Value<?>> values) {
172360
properties.put(name, of(values));
173361
return self();
174362
}
175363

176-
public B set(String name, Value<?> value, Value<?>... other) {
177-
properties.put(name, of(value, other));
364+
/**
365+
* Sets a property of type {@link ListValue}.
366+
*
367+
* @param name name of the property
368+
* @param first the first value in the list
369+
* @param second the second value in the list
370+
* @param others other values in the list
371+
*/
372+
public B set(String name, Value<?> first, Value<?> second, Value<?>... others) {
373+
properties.put(name, ListValue.builder().addValue(first).addValue(second, others).build());
178374
return self();
179375
}
180376

377+
/**
378+
* Sets a property of type {@link BlobValue}.
379+
*
380+
* @param name name of the property
381+
* @param value value associated with the property
382+
*/
181383
public B set(String name, Blob value) {
182384
properties.put(name, of(value));
183385
return self();
184386
}
185387

388+
/**
389+
* Sets a list property containing elements of type {@link BlobValue}.
390+
*
391+
* @param name name of the property
392+
* @param first the first {@link Blob} in the list
393+
* @param second the second {@link Blob} in the list
394+
* @param others other {@link Blob}s in the list
395+
*/
396+
public B set(String name, Blob first, Blob second, Blob... others) {
397+
List<BlobValue> values = new LinkedList<>();
398+
values.add(of(first));
399+
values.add(of(second));
400+
for (Blob other : others) {
401+
values.add(of(other));
402+
}
403+
properties.put(name, of(values));
404+
return self();
405+
}
406+
407+
/**
408+
* Sets a property of type {@code NullValue}.
409+
*
410+
* @param name name of the property
411+
*/
186412
public B setNull(String name) {
187413
properties.put(name, of());
188414
return self();
@@ -348,8 +574,8 @@ public <K extends IncompleteKey> FullEntity<K> getEntity(String name) {
348574
* @throws ClassCastException if value is not a list of values
349575
*/
350576
@SuppressWarnings("unchecked")
351-
public List<? extends Value<?>> getList(String name) {
352-
return ((Value<List<? extends Value<?>>>) getValue(name)).get();
577+
public <T extends Value<?>> List<T> getList(String name) {
578+
return (List<T>) getValue(name).get();
353579
}
354580

355581
/**

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public String kind() {
157157
return leaf().kind();
158158
}
159159

160+
abstract BaseKey parent();
161+
160162
@Override
161163
public int hashCode() {
162164
return Objects.hash(projectId(), namespace(), path());

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ static IncompleteKey fromPb(DatastoreV1.Key keyPb) {
8484
return new IncompleteKey(projectId, namespace, path);
8585
}
8686

87+
/**
88+
* Returns the key's parent.
89+
*/
90+
@Override
91+
public Key parent() {
92+
List<PathElement> ancestors = ancestors();
93+
if (ancestors.isEmpty()) {
94+
return null;
95+
}
96+
PathElement parent = ancestors.get(ancestors.size() - 1);
97+
Key.Builder keyBuilder;
98+
if (parent.hasName()) {
99+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.name());
100+
} else {
101+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.id());
102+
}
103+
String namespace = namespace();
104+
if (namespace != null) {
105+
keyBuilder.namespace(namespace);
106+
}
107+
return keyBuilder.ancestors(ancestors.subList(0, ancestors.size() - 1)).build();
108+
}
109+
87110
public static Builder builder(String projectId, String kind) {
88111
return new Builder(projectId, kind);
89112
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ListValue.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ private Builder() {
7070
super(ValueType.LIST);
7171
}
7272

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

8079
public Builder addValue(Value<?> first, Value<?>... other) {
81-
addValue(first);
80+
addValueHelper(first);
8281
for (Value<?> value : other) {
83-
addValue(value);
82+
addValueHelper(value);
8483
}
8584
return this;
8685
}

0 commit comments

Comments
 (0)