com.google.appengine.api.datastore.Query.SortDirection Java Examples
The following examples show how to use
com.google.appengine.api.datastore.Query.SortDirection.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooksByUser(String userId, String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books // Only for this user .setFilter(new Query.FilterPredicate( Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId)) // a custom datastore index is required since you are filtering by one property // but ordering by another .addSort(Book.TITLE, SortDirection.ASCENDING); PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #2
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title" PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #3
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title" PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #4
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title" PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #5
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooksByUser(String userId, String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books // Only for this user .setFilter(new Query.FilterPredicate( Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId)) // a custom datastore index is required since you are filtering by one property // but ordering by another .addSort(Book.TITLE, SortDirection.ASCENDING); PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #6
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title" PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #7
Source File: MetadataPropertiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
void printPropertyRange(DatastoreService ds, PrintWriter writer) { // Start with unrestricted keys-only property query Query q = new Query(Entities.PROPERTY_METADATA_KIND).setKeysOnly(); // Limit range q.setFilter( CompositeFilterOperator.and( new FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createPropertyKey("Employee", "salary")), new FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.LESS_THAN_OR_EQUAL, Entities.createPropertyKey("Manager", "salary")))); q.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING); // Print query results for (Entity e : ds.prepare(q).asIterable()) { writer.println(e.getKey().getParent().getName() + ": " + e.getKey().getName()); } }
Example #8
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception { long minBirthYear = 1940; // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_1] Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear); // Not valid. Missing sort on birthYear. Query q = new Query("Person") .setFilter(birthYearMinFilter) .addSort("lastName", SortDirection.ASCENDING); // [END gae_java8_datastore_inequality_filters_sort_orders_invalid_1] // Note: The local devserver behavior is different than the production // version of Cloud Datastore, so there aren't any assertions we can make // in this test. The query appears to work with the local test runner, // but will fail in production. }
Example #9
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void queryRestrictions_sortWrongOrderOnInequality_isInvalid() throws Exception { long minBirthYear = 1940; // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_2] Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear); // Not valid. Sort on birthYear needs to be first. Query q = new Query("Person") .setFilter(birthYearMinFilter) .addSort("lastName", SortDirection.ASCENDING) .addSort("birthYear", SortDirection.ASCENDING); // [END gae_java8_datastore_inequality_filters_sort_orders_invalid_2] // Note: The local devserver behavior is different than the production // version of Cloud Datastore, so there aren't any assertions we can make // in this test. The query appears to work with the local test runner, // but will fail in production. }
Example #10
Source File: PhotoManagerNoSql.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
@Override public Iterable<Photo> getActivePhotos() { Query query = new Query(getKind()); Query.Filter filter = new Query.FilterPredicate(PhotoNoSql.FIELD_NAME_ACTIVE, FilterOperator.EQUAL, true); query.addSort(PhotoNoSql.FIELD_NAME_UPLOAD_TIME, SortDirection.DESCENDING); query.setFilter(filter); FetchOptions options = FetchOptions.Builder.withDefaults(); return queryEntities(query, options); }
Example #11
Source File: CommentManagerNoSql.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
@Override public Iterable<Comment> getComments(Photo photo) { Query query = new Query(getKind()); Query.Filter photoIdFilter = new Query.FilterPredicate(CommentNoSql.FIELD_NAME_PHOTO_ID, FilterOperator.EQUAL, photo.getId()); List<Filter> filters = Arrays.asList(photoIdFilter, new Query.FilterPredicate( CommentNoSql.FIELD_NAME_PHOTO_OWNER_ID, FilterOperator.EQUAL, photo.getOwnerId())); Filter filter = new Query.CompositeFilter(CompositeFilterOperator.AND, filters); query.setFilter(filter); query.addSort(CommentNoSql.FIELD_NAME_TIMESTAMP, SortDirection.DESCENDING); FetchOptions options = FetchOptions.Builder.withDefaults(); return queryEntities(query, options); }
Example #12
Source File: ListPeopleServletTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
private String getFirstCursor() { Query q = new Query("Person").addSort("name", SortDirection.ASCENDING); PreparedQuery pq = datastore.prepare(q); FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE); QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions); return results.getCursor().toWebSafeString(); }
Example #13
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
private List<Entity> getTallestPeople() { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query q = new Query("Person").addSort("height", SortDirection.DESCENDING); PreparedQuery pq = datastore.prepare(q); return pq.asList(FetchOptions.Builder.withLimit(5)); }
Example #14
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() throws Exception { // Arrange Entity a = new Entity("Person", "a"); a.setProperty("birthYear", 1930); a.setProperty("lastName", "Someone"); Entity b = new Entity("Person", "b"); b.setProperty("birthYear", 1990); b.setProperty("lastName", "Bravo"); Entity c = new Entity("Person", "c"); c.setProperty("birthYear", 1960); c.setProperty("lastName", "Charlie"); Entity d = new Entity("Person", "d"); d.setProperty("birthYear", 1960); d.setProperty("lastName", "Delta"); datastore.put(ImmutableList.<Entity>of(a, b, c, d)); long minBirthYear = 1940; // [START gae_java8_datastore_inequality_filters_sort_orders_valid] Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear); Query q = new Query("Person") .setFilter(birthYearMinFilter) .addSort("birthYear", SortDirection.ASCENDING) .addSort("lastName", SortDirection.ASCENDING); // [END gae_java8_datastore_inequality_filters_sort_orders_valid] // Assert List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("query results").that(results).containsExactly(c, d, b).inOrder(); }
Example #15
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void sortOrderExample_multipleSortOrders_returnsSortedEntities() throws Exception { // Arrange Entity a = new Entity("Person", "a"); a.setProperty("lastName", "Alpha"); a.setProperty("height", 100); Entity b1 = new Entity("Person", "b1"); b1.setProperty("lastName", "Bravo"); b1.setProperty("height", 150); Entity b2 = new Entity("Person", "b2"); b2.setProperty("lastName", "Bravo"); b2.setProperty("height", 200); Entity c = new Entity("Person", "c"); c.setProperty("lastName", "Charlie"); c.setProperty("height", 300); datastore.put(ImmutableList.<Entity>of(a, b1, b2, c)); // Act // [START gae_java8_datastore_multiple_sort_orders] Query q = new Query("Person") .addSort("lastName", SortDirection.ASCENDING) .addSort("height", SortDirection.DESCENDING); // [END gae_java8_datastore_multiple_sort_orders] // Assert List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("query results").that(results).containsExactly(a, b2, b1, c).inOrder(); }
Example #16
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void sortOrderExample_returnsSortedEntities() throws Exception { // Arrange Entity a = new Entity("Person", "a"); a.setProperty("lastName", "Alpha"); a.setProperty("height", 100); Entity b = new Entity("Person", "b"); b.setProperty("lastName", "Bravo"); b.setProperty("height", 200); Entity c = new Entity("Person", "c"); c.setProperty("lastName", "Charlie"); c.setProperty("height", 300); datastore.put(ImmutableList.<Entity>of(a, b, c)); // Act // [START gae_java8_datastore_sort_order] // Order alphabetically by last name: Query q1 = new Query("Person").addSort("lastName", SortDirection.ASCENDING); // Order by height, tallest to shortest: Query q2 = new Query("Person").addSort("height", SortDirection.DESCENDING); // [END gae_java8_datastore_sort_order] // Assert List<Entity> lastNameResults = datastore.prepare(q1.setKeysOnly()).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("last name query results") .that(lastNameResults) .containsExactly(a, b, c) .inOrder(); List<Entity> heightResults = datastore.prepare(q2.setKeysOnly()).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("height query results") .that(heightResults) .containsExactly(c, b, a) .inOrder(); }
Example #17
Source File: ListPeopleServlet.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE); // If this servlet is passed a cursor parameter, let's use it. String startCursor = req.getParameter("cursor"); if (startCursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor)); } Query q = new Query("Person").addSort("name", SortDirection.ASCENDING); PreparedQuery pq = datastore.prepare(q); QueryResultList<Entity> results; try { results = pq.asQueryResultList(fetchOptions); } catch (IllegalArgumentException e) { // IllegalArgumentException happens when an invalid cursor is used. // A user could have manually entered a bad cursor in the URL or there // may have been an internal implementation detail change in App Engine. // Redirect to the page without the cursor parameter to show something // rather than an error. resp.sendRedirect("/people"); return; } resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); PrintWriter w = resp.getWriter(); w.println("<!DOCTYPE html>"); w.println("<meta charset=\"utf-8\">"); w.println("<title>Cloud Datastore Cursor Sample</title>"); w.println("<ul>"); for (Entity entity : results) { w.println("<li>" + entity.getProperty("name") + "</li>"); } w.println("</ul>"); String cursorString = results.getCursor().toWebSafeString(); // This servlet lives at '/people'. w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>"); }