Search in sources :

Example 31 with DatastoreService

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)));
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 32 with DatastoreService

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);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 33 with DatastoreService

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));
}
Also used : Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) DatastoreService(com.google.appengine.api.datastore.DatastoreService) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery)

Example 34 with DatastoreService

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");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) StringWriter(java.io.StringWriter) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 35 with DatastoreService

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]
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Date(java.util.Date) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)64 Entity (com.google.appengine.api.datastore.Entity)43 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)25 Query (com.google.appengine.api.datastore.Query)25 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 PersistenceManager (javax.jdo.PersistenceManager)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 Blob (com.google.appengine.api.datastore.Blob)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2