use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class LocalHighRepDatastoreTest method testEventuallyConsistentGlobalQueryResult.
@Test
public void testEventuallyConsistentGlobalQueryResult() {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Key ancestor = KeyFactory.createKey("foo", 3);
ds.put(new Entity("yam", ancestor));
ds.put(new Entity("yam", ancestor));
// Global query doesn't see the data.
assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
// Ancestor query does see the data.
assertEquals(2, ds.prepare(new Query("yam", ancestor)).countEntities(withLimit(10)));
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method ancestorQueryExample_returnsMatchingEntities.
@Test
public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
// [START gae_java8_datastore_ancestor_query]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity tom = new Entity("Person", "Tom");
Key tomKey = tom.getKey();
datastore.put(tom);
Entity weddingPhoto = new Entity("Photo", tomKey);
weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg");
Entity babyPhoto = new Entity("Photo", tomKey);
babyPhoto.setProperty("imageURL", "http://domain.com/some/path/to/baby_photo.jpg");
Entity dancePhoto = new Entity("Photo", tomKey);
dancePhoto.setProperty("imageURL", "http://domain.com/some/path/to/dance_photo.jpg");
Entity campingPhoto = new Entity("Photo");
campingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/camping_photo.jpg");
List<Entity> photoList = Arrays.asList(weddingPhoto, babyPhoto, dancePhoto, campingPhoto);
datastore.put(photoList);
Query photoQuery = new Query("Photo").setAncestor(tomKey);
// This returns weddingPhoto, babyPhoto, and dancePhoto,
// but not campingPhoto, because tom is not an ancestor
List<Entity> results = datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
// [END gae_java8_datastore_ancestor_query]
assertWithMessage("query results").that(results).containsExactly(weddingPhoto, babyPhoto, dancePhoto);
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method getTallestPeople.
// [START gae_java8_datastore_query_limit]
private List<Entity> getTallestPeople() {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Person").addSort("height", SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(q);
return pq.asList(FetchOptions.Builder.withLimit(5));
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryInterface_multipleFilters_printsMatchedEntities.
@Test
public void queryInterface_multipleFilters_printsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("firstName", "Alph");
a.setProperty("lastName", "Alpha");
a.setProperty("height", 60);
Entity b = new Entity("Person", "b");
b.setProperty("firstName", "Bee");
b.setProperty("lastName", "Bravo");
b.setProperty("height", 70);
Entity c = new Entity("Person", "c");
c.setProperty("firstName", "Charles");
c.setProperty("lastName", "Charlie");
c.setProperty("height", 100);
datastore.put(ImmutableList.<Entity>of(a, b, c));
StringWriter buf = new StringWriter();
PrintWriter out = new PrintWriter(buf);
long minHeight = 60;
long maxHeight = 72;
// Act
// [START gae_java8_datastore_interface_1]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
Filter heightMaxFilter = new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);
// Use CompositeFilter to combine multiple filters
CompositeFilter heightRangeFilter = CompositeFilterOperator.and(heightMinFilter, heightMaxFilter);
// Use class Query to assemble a query
Query q = new Query("Person").setFilter(heightRangeFilter);
// Use PreparedQuery interface to retrieve results
PreparedQuery pq = datastore.prepare(q);
for (Entity result : pq.asIterable()) {
String firstName = (String) result.getProperty("firstName");
String lastName = (String) result.getProperty("lastName");
Long height = (Long) result.getProperty("height");
out.println(firstName + " " + lastName + ", " + height + " inches tall");
}
// [END gae_java8_datastore_interface_1]
// Assert
assertThat(buf.toString()).contains("Alph Alpha, 60 inches tall");
assertThat(buf.toString()).contains("Bee Bravo, 70 inches tall");
assertThat(buf.toString()).doesNotContain("Charlie");
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method creatingAnEntityInASpecificEntityGroup.
@Test
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
String boardName = "my-message-board";
// [START creating_an_entity_in_a_specific_entity_group]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String messageTitle = "Some Title";
String messageText = "Some message.";
Date postDate = new Date();
Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);
Entity message = new Entity("Message", messageBoardKey);
message.setProperty("message_title", messageTitle);
message.setProperty("message_text", messageText);
message.setProperty("post_date", postDate);
Transaction txn = datastore.beginTransaction();
datastore.put(txn, message);
txn.commit();
// [END creating_an_entity_in_a_specific_entity_group]
}
Aggregations