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