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]
}
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);
}
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);
}
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");
}
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");
}
Aggregations