Search in sources :

Example 86 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)

Example 87 with Key

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

the class EntitiesTest method keyFactoryBuilder_makeKeyWithParents.

@Test
public void keyFactoryBuilder_makeKeyWithParents() {
    Key greatKey = KeyFactory.createKey("Person", "GreatGrandpa");
    Key grandKey = KeyFactory.createKey(greatKey, "Person", "Grandpa");
    Key dadKey = KeyFactory.createKey(grandKey, "Person", "Dad");
    Key meKey = KeyFactory.createKey(dadKey, "Person", "Me");
    // [START generating_keys_2]
    Key k = new KeyFactory.Builder("Person", "GreatGrandpa").addChild("Person", "Grandpa").addChild("Person", "Dad").addChild("Person", "Me").getKey();
    // [END generating_keys_2]
    assertThat(k).isEqualTo(meKey);
}
Also used : Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 88 with Key

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

the class EntitiesTest method creatingAnEntity_withoutKeyName_writesEntity.

@Test
public void creatingAnEntity_withoutKeyName_writesEntity() throws Exception {
    Key employeeKey = writeEmptyEmployee();
    // [START retrieving_an_entity]
    // Key employeeKey = ...;
    Entity employee = datastore.get(employeeKey);
    // [END retrieving_an_entity]
    assertWithMessage("retrieved key ID").that(employee.getKey().getId()).isEqualTo(employeeKey.getId());
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 89 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 90 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)

Aggregations

Key (com.google.appengine.api.datastore.Key)104 Entity (com.google.appengine.api.datastore.Entity)74 ArrayList (java.util.ArrayList)36 ClassInfo (siena.ClassInfo)24 Test (org.junit.Test)23 Field (java.lang.reflect.Field)22 DatastoreService (com.google.appengine.api.datastore.DatastoreService)18 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)18 Query (com.google.appengine.api.datastore.Query)18 HashMap (java.util.HashMap)14 List (java.util.List)14 SienaException (siena.SienaException)14 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)12 QueryResultList (com.google.appengine.api.datastore.QueryResultList)12 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 EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)5