use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method usesForTransactions_readSnapshot.
@Test
public void usesForTransactions_readSnapshot() throws Exception {
String boardName = "my-message-board";
Entity b = new Entity("MessageBoard", boardName);
b.setProperty("count", 13);
datastore.put(b);
// [START uses_for_transactions_3]
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
// Display information about a message board and its first 10 messages.
Key boardKey = KeyFactory.createKey("MessageBoard", boardName);
Transaction txn = datastore.beginTransaction();
Entity messageBoard = datastore.get(boardKey);
long count = (Long) messageBoard.getProperty("count");
Query q = new Query("Message", boardKey);
// This is an ancestor query.
PreparedQuery pq = datastore.prepare(txn, q);
List<Entity> messages = pq.asList(FetchOptions.Builder.withLimit(10));
txn.commit();
// [END uses_for_transactions_3]
assertWithMessage("board.count").that(count).isEqualTo(13L);
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method transactionalTaskEnqueuing.
@Test
public void transactionalTaskEnqueuing() throws Exception {
// [START transactional_task_enqueuing]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Queue queue = QueueFactory.getDefaultQueue();
Transaction txn = datastore.beginTransaction();
// ...
queue.add(txn, TaskOptions.Builder.withUrl("/path/to/handler"));
// ...
txn.commit();
// [END transactional_task_enqueuing]
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method usingTransactions.
@Test
public void usingTransactions() throws Exception {
Entity joe = new Entity("Employee", "Joe");
datastore.put(joe);
// [START using_transactions]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
try {
Key employeeKey = KeyFactory.createKey("Employee", "Joe");
Entity employee = datastore.get(employeeKey);
employee.setProperty("vacationDays", 10);
datastore.put(txn, employee);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
// [END using_transactions]
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class TransactionsTest method entityGroups.
@Test
public void entityGroups() throws Exception {
try {
// [START entity_groups]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity person = new Entity("Person", "tom");
datastore.put(person);
// Transactions on root entities
Transaction txn = datastore.beginTransaction();
Entity tom = datastore.get(person.getKey());
tom.setProperty("age", 40);
datastore.put(txn, tom);
txn.commit();
// Transactions on child entities
txn = datastore.beginTransaction();
tom = datastore.get(person.getKey());
Entity photo = new Entity("Photo", tom.getKey());
// Create a Photo that is a child of the Person entity named "tom"
photo.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg");
datastore.put(txn, photo);
txn.commit();
// Transactions on entities in different entity groups
txn = datastore.beginTransaction();
tom = datastore.get(person.getKey());
Entity photoNotaChild = new Entity("Photo");
photoNotaChild.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg");
datastore.put(txn, photoNotaChild);
// Throws IllegalArgumentException because the Person entity
// and the Photo entity belong to different entity groups.
txn.commit();
// [END entity_groups]
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// We expect to get an exception that complains that we don't have a XG-transaction.
}
}
use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.
the class StatsServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// [START stat_example]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");
// [END stat_example]
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter w = resp.getWriter();
w.printf("%d bytes\n%d entities\n", totalBytes, totalEntities);
}
Aggregations