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");
}
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);
}
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);
}
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]
}
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);
}
Aggregations