Search in sources :

Example 91 with Key

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

the class MetadataEntityGroupTest method entityGroupCount_printsCount.

// [END entity_group_2]
@Test
public void entityGroupCount_printsCount() throws Exception {
    StringWriter responseWriter = new StringWriter();
    MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
    Entity entity1 = new Entity("Simple");
    Key key1 = datastore.put(entity1);
    Key entityGroupKey = Entities.createEntityGroupKey(key1);
    EntityGroupCount groupCount = new EntityGroupCount(0, 0);
    groupCount.showEntityGroupCount(datastore, cache, new PrintWriter(responseWriter), entityGroupKey);
    assertThat(responseWriter.toString()).contains(" entities");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) MemcacheService(com.google.appengine.api.memcache.MemcacheService) StringWriter(java.io.StringWriter) Key(com.google.appengine.api.datastore.Key) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 92 with Key

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

the class QueriesTest method ancestorQueryExample_returnsMatchingEntities.

@Test
public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
    // [START gae_java8_datastore_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 babyPhoto = new Entity("Photo", tomKey);
    babyPhoto.setProperty("imageURL", "http://domain.com/some/path/to/baby_photo.jpg");
    Entity dancePhoto = new Entity("Photo", tomKey);
    dancePhoto.setProperty("imageURL", "http://domain.com/some/path/to/dance_photo.jpg");
    Entity campingPhoto = new Entity("Photo");
    campingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/camping_photo.jpg");
    List<Entity> photoList = Arrays.asList(weddingPhoto, babyPhoto, dancePhoto, campingPhoto);
    datastore.put(photoList);
    Query photoQuery = new Query("Photo").setAncestor(tomKey);
    // This returns weddingPhoto, babyPhoto, and dancePhoto,
    // but not campingPhoto, because tom is not an ancestor
    List<Entity> results = datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
    // [END gae_java8_datastore_ancestor_query]
    assertWithMessage("query results").that(results).containsExactly(weddingPhoto, babyPhoto, dancePhoto);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 93 with Key

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

the class QueriesTest method keyFilterExample_kindless_returnsMatchingEntities.

@Test
public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Child", "a");
    Entity b = new Entity("Child", "b");
    Entity c = new Entity("Child", "c");
    Entity aa = new Entity("Child", "aa", b.getKey());
    Entity bb = new Entity("Child", "bb", b.getKey());
    Entity aaa = new Entity("Child", "aaa", bb.getKey());
    Entity bbb = new Entity("Child", "bbb", bb.getKey());
    Entity adult = new Entity("Adult", "a");
    Entity zooAnimal = new Entity("ZooAnimal", "a");
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal));
    // Act
    Key lastSeenKey = bb.getKey();
    // [START gae_java8_datastore_kindless_query]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query().setFilter(keyFilter);
    // [END gae_java8_datastore_kindless_query]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertWithMessage("query results").that(results).containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
    aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
    bbb, // Kind "ZooAnimal" is greater than "Child"
    zooAnimal, // Key name identifier "c" is greater than b.
    c);
}
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 94 with Key

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

the class TransactionsTest method creatingAnEntityInASpecificEntityGroup.

@Test
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
    String boardName = "my-message-board";
    // [START creating_an_entity_in_a_specific_entity_group]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    String messageTitle = "Some Title";
    String messageText = "Some message.";
    Date postDate = new Date();
    Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);
    Entity message = new Entity("Message", messageBoardKey);
    message.setProperty("message_title", messageTitle);
    message.setProperty("message_text", messageText);
    message.setProperty("post_date", postDate);
    Transaction txn = datastore.beginTransaction();
    datastore.put(txn, message);
    txn.commit();
// [END creating_an_entity_in_a_specific_entity_group]
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Date(java.util.Date) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 95 with Key

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

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