Search in sources :

Example 51 with Key

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

the class QueriesTest method ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities.

@Test
public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Grandparent", "a");
    Entity b = new Entity("Grandparent", "b");
    Entity c = new Entity("Grandparent", "c");
    Entity aa = new Entity("Parent", "aa", a.getKey());
    Entity ba = new Entity("Parent", "ba", b.getKey());
    Entity bb = new Entity("Parent", "bb", b.getKey());
    Entity bc = new Entity("Parent", "bc", b.getKey());
    Entity cc = new Entity("Parent", "cc", c.getKey());
    Entity aaa = new Entity("Child", "aaa", aa.getKey());
    Entity bbb = new Entity("Child", "bbb", bb.getKey());
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, ba, bb, bc, cc, aaa, bbb));
    // Act
    Key ancestorKey = b.getKey();
    Key lastSeenKey = bb.getKey();
    // [START gae_java8_datastore_kindless_ancestor_key_query]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query().setAncestor(ancestorKey).setFilter(keyFilter);
    // [END gae_java8_datastore_kindless_ancestor_key_query]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertWithMessage("query results").that(results).containsExactly(bc, bbb);
}
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) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 52 with Key

use of com.google.appengine.api.datastore.Key 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 53 with Key

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

the class TransactionsTest method fetchOrCreate.

private Entity fetchOrCreate(String boardName) {
    // [START uses_for_transactions_2]
    Transaction txn = datastore.beginTransaction();
    Entity messageBoard;
    Key boardKey;
    try {
        boardKey = KeyFactory.createKey("MessageBoard", boardName);
        messageBoard = datastore.get(boardKey);
    } catch (EntityNotFoundException e) {
        messageBoard = new Entity("MessageBoard", boardName);
        messageBoard.setProperty("count", 0L);
        boardKey = datastore.put(txn, messageBoard);
    }
    txn.commit();
    return messageBoard;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Transaction(com.google.appengine.api.datastore.Transaction) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 54 with Key

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

the class TransactionsTest method usesForTransactions_relativeUpdates.

@Test
public void usesForTransactions_relativeUpdates() throws Exception {
    String boardName = "my-message-board";
    Entity b = new Entity("MessageBoard", boardName);
    b.setProperty("count", 41);
    datastore.put(b);
    // [START uses_for_transactions_1]
    int retries = 3;
    while (true) {
        Transaction txn = datastore.beginTransaction();
        try {
            Key boardKey = KeyFactory.createKey("MessageBoard", boardName);
            Entity messageBoard = datastore.get(boardKey);
            long count = (Long) messageBoard.getProperty("count");
            ++count;
            messageBoard.setProperty("count", count);
            datastore.put(txn, messageBoard);
            txn.commit();
            break;
        } catch (ConcurrentModificationException e) {
            if (retries == 0) {
                throw e;
            }
            // Allow retry to occur
            --retries;
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
    }
    // [END uses_for_transactions_1]
    b = datastore.get(KeyFactory.createKey("MessageBoard", boardName));
    assertWithMessage("board.count").that((long) b.getProperty("count")).isEqualTo(42L);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ConcurrentModificationException(java.util.ConcurrentModificationException) Transaction(com.google.appengine.api.datastore.Transaction) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 55 with Key

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

the class ProjectionServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    out.printf("Latest entries from guestbook: \n");
    Key guestbookKey = KeyFactory.createKey("Guestbook", GUESTBOOK_ID);
    Query query = new Query("Greeting", guestbookKey);
    addGuestbookProjections(query);
    printGuestbookEntries(datastore, query, out);
}
Also used : Query(com.google.appengine.api.datastore.Query) Key(com.google.appengine.api.datastore.Key) PrintWriter(java.io.PrintWriter)

Aggregations

Key (com.google.appengine.api.datastore.Key)121 Entity (com.google.appengine.api.datastore.Entity)83 ArrayList (java.util.ArrayList)39 DatastoreService (com.google.appengine.api.datastore.DatastoreService)26 Query (com.google.appengine.api.datastore.Query)23 Test (org.junit.Test)23 ClassInfo (siena.ClassInfo)23 Field (java.lang.reflect.Field)22 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)21 HashMap (java.util.HashMap)14 SienaException (siena.SienaException)14 List (java.util.List)13 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)12 QueryResultList (com.google.appengine.api.datastore.QueryResultList)11 Map (java.util.Map)11 Transaction (com.google.appengine.api.datastore.Transaction)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)7 IOException (java.io.IOException)7