Search in sources :

Example 36 with DatastoreService

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

Example 37 with DatastoreService

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]
}
Also used : Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Queue(com.google.appengine.api.taskqueue.Queue) Test(org.junit.Test)

Example 38 with DatastoreService

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

Example 39 with DatastoreService

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

Example 40 with DatastoreService

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

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