use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class StartupServletTest method doGet_emptyDatastore_writesPresidents.
@Test
public void doGet_emptyDatastore_writesPresidents() throws Exception {
servletUnderTest.doGet(mockRequest, mockResponse);
Filter nameFilter = new FilterPredicate("name", FilterOperator.EQUAL, "George Washington");
Query q = new Query("Person").setFilter(nameFilter);
Entity result = datastore.prepare(q).asSingleEntity();
assertThat(result.getProperty("name")).named("name").isEqualTo("George Washington");
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class ReadPolicyTest method readPolicy_strong_returnsAllResults.
@Test
public void readPolicy_strong_returnsAllResults() {
double deadline = 5.0;
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG);
DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
Entity parent = new Entity("Person", "a");
Entity child = new Entity("Person", "b", parent.getKey());
datastore.put(ImmutableList.<Entity>of(parent, child));
Query q = new Query("Person").setAncestor(parent.getKey());
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").hasSize(2);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class ReadPolicyTest method readPolicy_eventual_returnsNoResults.
@Test
public void readPolicy_eventual_returnsNoResults() {
// [START data_consistency]
double deadline = 5.0;
// Construct a read policy for eventual consistency
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL);
// Set the read policy
DatastoreServiceConfig eventuallyConsistentConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy);
// Set the call deadline
DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline);
// Set both the read policy and the call deadline
DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
// Get Datastore service with the given configuration
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
// [END data_consistency]
Entity parent = new Entity("Person", "a");
Entity child = new Entity("Person", "b", parent.getKey());
datastore.put(ImmutableList.<Entity>of(parent, child));
// Even though we are using an ancestor query, the policy is set to
// eventual, so we should get eventually-consistent results. Since the
// local data store test config is set to 100% unapplied jobs, there
// should be no results.
Query q = new Query("Person").setAncestor(parent.getKey());
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").isEmpty();
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class IndexesTest method propertyFilterExample_returnsMatchingEntities.
@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
// [START unindexed_properties_1]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key acmeKey = KeyFactory.createKey("Company", "Acme");
Entity tom = new Entity("Person", "Tom", acmeKey);
tom.setProperty("name", "Tom");
tom.setProperty("age", 32);
datastore.put(tom);
Entity lucy = new Entity("Person", "Lucy", acmeKey);
lucy.setProperty("name", "Lucy");
lucy.setUnindexedProperty("age", 29);
datastore.put(lucy);
Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25);
Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter);
// Returns tom but not lucy, because her age is unindexed
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
// [END unindexed_properties_1]
assertThat(results).named("query results").containsExactly(tom);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class ListPeopleServletTest method getFirstCursor.
private String getFirstCursor() {
Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
PreparedQuery pq = datastore.prepare(q);
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE);
QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
return results.getCursor().toWebSafeString();
}
Aggregations