use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class GuestbookTestUtilities method cleanDatastore.
public static void cleanDatastore(DatastoreService ds, String book) {
Query query = new Query("Greeting").setAncestor(new KeyFactory.Builder("Guestbook", book).getKey()).setKeysOnly();
PreparedQuery pq = ds.prepare(query);
List<Entity> entities = pq.asList(FetchOptions.Builder.withDefaults());
ArrayList<Key> keys = new ArrayList<>(entities.size());
for (Entity e : entities) {
keys.add(e.getKey());
}
ds.delete(keys);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class StatsServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// [START stat_example]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");
// [END stat_example]
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter w = resp.getWriter();
w.printf("%d bytes\n%d entities\n", totalBytes, totalEntities);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class GuestbookStrong method listGreetingEntities.
@Override
protected List<Entity> listGreetingEntities(DatastoreService datastore) {
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
Query query = new Query("Greeting", guestbookKey).setAncestor(guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
return datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class LocalHighRepDatastoreTest method testEventuallyConsistentGlobalQueryResult.
@Test
public void testEventuallyConsistentGlobalQueryResult() {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Key ancestor = KeyFactory.createKey("foo", 3);
ds.put(new Entity("yam", ancestor));
ds.put(new Entity("yam", ancestor));
// Global query doesn't see the data.
assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
// Ancestor query does see the data.
assertEquals(2, ds.prepare(new Query("yam", ancestor)).countEntities(withLimit(10)));
}
use of com.google.appengine.api.datastore.Query in project CodeU-Spring-2018 by maksymko.
the class PersistentDataStore method loadMessages.
/**
* Loads all Message objects from the Datastore service and returns them in a List.
*
* @throws PersistentDataStoreException if an error was detected during the load from the
* Datastore service
*/
public List<Message> loadMessages() throws PersistentDataStoreException {
List<Message> messages = new ArrayList<>();
// Retrieve all messages from the datastore.
Query query = new Query("chat-messages");
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
try {
UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
UUID conversationUuid = UUID.fromString((String) entity.getProperty("conv_uuid"));
UUID authorUuid = UUID.fromString((String) entity.getProperty("author_uuid"));
Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
String content = (String) entity.getProperty("content");
Message message = new Message(uuid, conversationUuid, authorUuid, content, creationTime);
messages.add(message);
} catch (Exception e) {
// database entity definition mismatches, or service mismatches.
throw new PersistentDataStoreException(e);
}
}
return messages;
}
Aggregations