Skip to content

Commit

Permalink
Fix nits in pom/README/concept snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Feb 12, 2016
1 parent 7f34191 commit 63f756a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ This directory contains sample code used in Google Cloud Datastore documentation

2. Compile the program by typing `mvn clean compile` in command line.

3. Run the program by typing `mvn exec:java` in command line.
3. Run the program by typing `mvn exec:java` in command line. In addition to listing tasks via this command line interface, you can view tasks you create in the [Google Cloud Developer's Console](https://console.cloud.google.com/).
4 changes: 2 additions & 2 deletions datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- // [END maven]-->
Expand Down
64 changes: 34 additions & 30 deletions datastore/src/main/java/com/google/datastore/snippets/Concepts.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertNull;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.gcloud.datastore.Cursor;
Expand All @@ -49,7 +50,6 @@
import com.google.gcloud.datastore.StructuredQuery.Projection;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
import com.google.gcloud.datastore.Transaction;
import com.google.gcloud.datastore.Value;
import com.google.gcloud.datastore.testing.LocalGcdHelper;

import org.junit.AfterClass;
Expand Down Expand Up @@ -715,9 +715,8 @@ private Cursor cursorPaging(int pageSize, Cursor pageCursor) {
queryBuilder.startCursor(pageCursor);
}
QueryResults<Entity> tasks = datastore.run(queryBuilder.build());
Entity task;
while (tasks.hasNext()) {
task = tasks.next();
Entity task = tasks.next();
// do something with the task
}
Cursor nextPageCursor = tasks.cursorAfter();
Expand Down Expand Up @@ -919,17 +918,16 @@ public void testPropertyRunQuery() {
Key property = results.next();
String kind = property.ancestors().get(property.ancestors().size() - 1).name();
String propertyName = property.name();
if (!propertiesByKind.containsKey(kind)) {
propertiesByKind.put(kind, new HashSet<String>());
Collection<String> properties = propertiesByKind.get(kind);
if (properties == null) {
properties = new HashSet<>();
propertiesByKind.put(kind, properties);
}
propertiesByKind.get(kind).add(propertyName);
properties.add(propertyName);
}
// [END property_run_query]
Map<String, Collection<String>> expected = new HashMap<>();
expected.put(
"Task",
ImmutableSet.of(
"done", "type", "done", "completed", "priority", "created", "percent_complete", "tag"));
Map<String, ImmutableSet<String>> expected = ImmutableMap.of("Task", ImmutableSet.of(
"done", "type", "done", "completed", "priority", "created", "percent_complete", "tag"));
assertEquals(expected, propertiesByKind);
}

Expand All @@ -940,30 +938,34 @@ public void testPropertyByKindRunQuery() {
Key key = datastore.newKeyFactory().kind("__kind__").newKey("Task");
Query<Entity> query = Query.entityQueryBuilder()
.kind("__property__")
.filter(PropertyFilter.hasAncestor(key))
.filter(PropertyFilter.hasAncestor(key))
.build();
QueryResults<Entity> results = datastore.run(query);
Map<String, Collection<String>> representationsByProperty = new HashMap<>();
while (results.hasNext()) {
Entity property = results.next();
String propertyName = property.key().name();
List<? extends Value<?>> representations = property.getList("property_representation");
if (!representationsByProperty.containsKey(propertyName)) {
representationsByProperty.put(propertyName, new HashSet<String>());
List<StringValue> representations =
(List<StringValue>) property.getList("property_representation");
Collection<String> currentRepresentations = representationsByProperty.get(propertyName);
if (currentRepresentations == null) {
currentRepresentations = new HashSet<>();
representationsByProperty.put(propertyName, currentRepresentations);
}
for (Value<?> value : representations) {
representationsByProperty.get(propertyName).add((String) value.get());
for (StringValue value : representations) {
currentRepresentations.add(value.get());
}
}
// [END property_by_kind_run_query]
Map<String, Collection<String>> expected = new HashMap<>();
expected.put("type", Collections.singleton("STRING"));
expected.put("done", Collections.singleton("BOOLEAN"));
expected.put("completed", Collections.singleton("BOOLEAN"));
expected.put("priority", Collections.singleton("INT64"));
expected.put("created", Collections.singleton("INT64"));
expected.put("percent_complete", Collections.singleton("DOUBLE"));
expected.put("tag", Collections.singleton("STRING"));
Map<String, Collection<String>> expected = ImmutableMap.<String, Collection<String>>builder()
.put("type", Collections.singleton("STRING"))
.put("done", Collections.singleton("BOOLEAN"))
.put("completed", Collections.singleton("BOOLEAN"))
.put("priority", Collections.singleton("INT64"))
.put("created", Collections.singleton("INT64"))
.put("percent_complete", Collections.singleton("DOUBLE"))
.put("tag", Collections.singleton("STRING"))
.build();
assertEquals(expected, representationsByProperty);
}

Expand All @@ -986,14 +988,16 @@ public void testPropertyFilteringRunQuery() {
Key property = results.next();
String kind = property.ancestors().get(property.ancestors().size() - 1).name();
String propertyName = property.name();
if (!propertiesByKind.containsKey(kind)) {
propertiesByKind.put(kind, new HashSet<String>());
Collection<String> properties = propertiesByKind.get(kind);
if (properties == null) {
properties = new HashSet<String>();
propertiesByKind.put(kind, properties);
}
propertiesByKind.get(kind).add(propertyName);
properties.add(propertyName);
}
// [END property_filtering_run_query]
Map<String, Collection<String>> expected = new HashMap<>();
expected.put("Task", ImmutableSet.of("priority", "tag", "type"));
Map<String, ImmutableSet<String>> expected =
ImmutableMap.of("Task", ImmutableSet.of("priority", "tag", "type"));
assertEquals(expected, propertiesByKind);
}

Expand Down

0 comments on commit 63f756a

Please sign in to comment.