/** * @param kind * @param ancestor * @return */ public static Iterable<Entity> listChildKeys(String kind, Key ancestor) { logger.log(Level.INFO, "Search entities based on parent"); Query q = new Query(kind); q.setAncestor(ancestor).setKeysOnly(); q.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, ancestor); PreparedQuery pq = datastore.prepare(q); return pq.asIterable(); }
/** * Counts the rows of the specified table * * @param searchIn the kind of entity (table) to search in * @param parent search for all entities with this parent * @param filters all properties to search for * @return number of rows */ public static int count(String searchIn, Key parent, FilterWrapper... filters) { Query query = new Query(searchIn); query.setKeysOnly(); if (parent != null) { query.setAncestor(parent); } setFilterProperties(query, filters); PreparedQuery preparedQuery = mDatastore.prepare(query); return preparedQuery.countEntities(FetchOptions.Builder.withDefaults()); }
/** * Get all entities with the specified parent and properties * * @param searchIn what kind of entity (table) to search in * @param parent search for all entities with this parent * @param filters all properties to search for * @return an iterable of all found entities with the specified parent */ public static Iterable<Entity> getEntities( String searchIn, Key parent, FilterWrapper... filters) { Query query = new Query(searchIn); // Parent if (parent != null) { query.setAncestor(parent); } setFilterProperties(query, filters); PreparedQuery preparedQuery = mDatastore.prepare(query); return preparedQuery.asIterable(); }
/** Google issueId:1458158 */ @Test public void testIntFilter() { Query q = new Query(kindName); Query.Filter filter = Query.CompositeFilterOperator.and( new FilterPredicate("intData1", Query.FilterOperator.LESS_THAN, 20), new FilterPredicate("intData1", Query.FilterOperator.GREATER_THAN, 1), new FilterPredicate("intData1", Query.FilterOperator.EQUAL, null)); q.setFilter(filter); q.addSort("intData1", Query.SortDirection.ASCENDING); q.setAncestor(rootKey); assertEquals(1, service.prepare(q).countEntities(fo)); List<Entity> elist = service.prepare(q).asList(fo); assertEquals(Arrays.asList(1L, 10L, null), elist.get(0).getProperty("intData1")); }
@Test public void testStrFilter() { Query q = new Query(kindName); q.setAncestor(rootKey); Query.Filter filter = Query.CompositeFilterOperator.and( new FilterPredicate("stringData", Query.FilterOperator.LESS_THAN, "qqq"), new FilterPredicate("stringData", Query.FilterOperator.GREATER_THAN, "mmm")); q.setFilter(filter); q.addSort("stringData", Query.SortDirection.ASCENDING); assertEquals(2, service.prepare(q).countEntities(fo)); List<Entity> elist = service.prepare(q).asList(fo); assertEquals(Arrays.asList("abc", "xyz", "mno"), elist.get(0).getProperty("stringData")); assertEquals(Arrays.asList("ppp", "iii", "ddd"), elist.get(1).getProperty("stringData")); }
/** * Searches for an existing entity * * @param searchIn what kind of entity to search in * @param parent the parent of the entity to find, set to null to skip * @param onlyKeys will only retrieve keys for the found entity * @param filters property name and values to search for * @return found entity, null if none or more than 1 was found */ private static Entity getSingleEntity( String searchIn, Key parent, boolean onlyKeys, FilterWrapper... filters) { Query query = new Query(searchIn); if (onlyKeys) { query.setKeysOnly(); } setFilterProperties(query, filters); if (parent != null) { query.setAncestor(parent); } try { return mDatastore.prepare(query).asSingleEntity(); } catch (TooManyResultsException e) { // Does nothing } return null; }
/** * Get all entities (with only keys) with the specified parent and properties * * @param searchIn what kind of entity (table) to search in * @param parent search for all entities with this parent * @param filters all properties to search for * @return an array list of all found entities with the specified parent */ public static List<Key> getKeys(String searchIn, Key parent, FilterWrapper... filters) { Query query = new Query(searchIn); query.setKeysOnly(); // Parent if (parent != null) { query.setAncestor(parent); } // Search by properties setFilterProperties(query, filters); PreparedQuery preparedQuery = mDatastore.prepare(query); ArrayList<Key> keys = new ArrayList<>(); for (Entity entity : preparedQuery.asIterable()) { keys.add(entity.getKey()); } return keys; }