Search in sources :

Example 11 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class TransactionsTest method crossGroupTransactions.

@Test
public void crossGroupTransactions() throws Exception {
    // [START cross-group_XG_transactions_using_the_Java_low-level_API]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    TransactionOptions options = TransactionOptions.Builder.withXG(true);
    Transaction txn = datastore.beginTransaction(options);
    Entity a = new Entity("A");
    a.setProperty("a", 22);
    datastore.put(txn, a);
    Entity b = new Entity("B");
    b.setProperty("b", 11);
    datastore.put(txn, b);
    txn.commit();
// [END cross-group_XG_transactions_using_the_Java_low-level_API]
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Transaction(com.google.appengine.api.datastore.Transaction) TransactionOptions(com.google.appengine.api.datastore.TransactionOptions) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Test(org.junit.Test)

Example 12 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities.

@Test
public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities() throws Exception {
    // [START gae_java8_datastore_kindless_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 weddingVideo = new Entity("Video", tomKey);
    weddingVideo.setProperty("videoURL", "http://domain.com/some/path/to/wedding_video.avi");
    List<Entity> mediaList = Arrays.asList(weddingPhoto, weddingVideo);
    datastore.put(mediaList);
    // By default, ancestor queries include the specified ancestor itself.
    // The following filter excludes the ancestor from the query results.
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, tomKey);
    Query mediaQuery = new Query().setAncestor(tomKey).setFilter(keyFilter);
    // Returns both weddingPhoto and weddingVideo,
    // even though they are of different entity kinds
    List<Entity> results = datastore.prepare(mediaQuery).asList(FetchOptions.Builder.withDefaults());
    // [END gae_java8_datastore_kindless_ancestor_query]
    assertWithMessage("query result keys").that(results).containsExactly(weddingPhoto, weddingVideo);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) 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) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 13 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService 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]
    assertWithMessage("query results").that(results).containsExactly(tom);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 14 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class StartupServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key isPopulatedKey = KeyFactory.createKey(IS_POPULATED_ENTITY, IS_POPULATED_KEY_NAME);
    boolean isAlreadyPopulated;
    try {
        datastore.get(isPopulatedKey);
        isAlreadyPopulated = true;
    } catch (EntityNotFoundException expected) {
        isAlreadyPopulated = false;
    }
    if (isAlreadyPopulated) {
        resp.getWriter().println("ok");
        return;
    }
    ImmutableList.Builder<Entity> people = ImmutableList.builder();
    for (String name : US_PRESIDENTS) {
        Entity person = new Entity(PERSON_ENTITY);
        person.setProperty(NAME_PROPERTY, name);
        people.add(person);
    }
    datastore.put(people.build());
    datastore.put(new Entity(isPopulatedKey));
    resp.getWriter().println("ok");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ImmutableList(com.google.common.collect.ImmutableList) DatastoreService(com.google.appengine.api.datastore.DatastoreService) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 15 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class IndexesServletTest method doGet_repeatedPropertyEntities_writesWidgets.

// CHECKSTYLE.OFF: VariableDeclarationUsageDistance
@SuppressWarnings("VariableDeclarationUsageDistance")
@Test
public void doGet_repeatedPropertyEntities_writesWidgets() throws Exception {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    // [START exploding_index_example_3]
    Entity widget = new Entity("Widget");
    widget.setProperty("x", Arrays.asList(1, 2, 3, 4));
    widget.setProperty("y", Arrays.asList("red", "green", "blue"));
    widget.setProperty("date", new Date());
    datastore.put(widget);
    // [END exploding_index_example_3]
    servletUnderTest.doGet(mockRequest, mockResponse);
    assertWithMessage("IndexesServlet response").that(responseWriter.toString()).isEqualTo("Got 1 widgets.\n");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)59 Entity (com.google.appengine.api.datastore.Entity)38 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)22 Query (com.google.appengine.api.datastore.Query)20 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 Date (java.util.Date)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 ArrayList (java.util.ArrayList)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)2 ReadPolicy (com.google.appengine.api.datastore.ReadPolicy)2 Text (com.google.appengine.api.datastore.Text)2