Refine search
protected List<Entity> doQuery(String kind, String pName, Class<?> type, boolean indexed) { FetchOptions fo = FetchOptions.Builder.withDefaults(); Query query = new Query(kind, rootKey); if (indexed) { query.addProjection(new PropertyProjection(pName, type)); query.addSort(pName); } return service.prepare(query).asList(fo); }
/** * Query on all of the keys for the data store ID. * * @param keysOnly whether to call {@link Query#setKeysOnly()} * @return iterable over the entities in the query result */ private Iterable<Entity> query(boolean keysOnly) { Query query = new Query(getId()); if (keysOnly) { query.setKeysOnly(); } return dataStoreService.prepare(query).asIterable(); } }
private Entity getParent() { Query query = new Query(PARENTKIND); query.setFilter(new FilterPredicate("name", Query.FilterOperator.EQUAL, "argonaut")); Entity parent = service.prepare(query).asSingleEntity(); return parent; } }
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query gaeQuery = new Query(entityName); gaeQuery.setKeysOnly(); gaeQuery.addFilter("__key__", FilterOperator.EQUAL, key); PreparedQuery pq = datastore.prepare(gaeQuery); List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(1)); Key elementKey = null; if (results.size() == 1) { elementKey = results.get(0).getKey(); }
public static LeanAccount findAccountByProvider(String providerID, String provider) { if (providerID == null) { log.severe("Empty providerID. Can not find account without providerID."); return null; } Query query = new Query(accountsKind); query.addFilter("_provider_id", Query.FilterOperator.EQUAL, providerID); query.addFilter("_provider", Query.FilterOperator.EQUAL, provider); PreparedQuery pq = datastore.prepare(query); Entity accountEntity = pq.asSingleEntity(); return (accountEntity == null) ? null : toLeanAccount(accountEntity); }
ListIterator<?> getChildrenByKeys(List<Key> childKeys, final ExecutionContext ec) { String kindName = elementTable.getIdentifier().getIdentifierName(); Query q = new Query(kindName); NucleusLogger.PERSISTENCE.debug("Preparing to query for " + childKeys); q.addFilter(Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.IN, childKeys); for (Query.SortPredicate sp : getSortPredicates()) { q.addSort(sp.getPropertyName(), sp.getDirection()); } DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads(); DatastoreService ds = DatastoreServiceFactoryInternal.getDatastoreService(config); Utils.Function<Entity, Object> func = new Utils.Function<Entity, java.lang.Object>() { @Override public Object apply(Entity from) { return EntityUtils.entityToPojo(from, elementCmd, clr, ec, false, ec.getFetchPlan()); } }; return new LazyResult(ds.prepare(q).asIterable(), func, true).listIterator(); }
protected int[] internalShift(ObjectProvider op, boolean batched, int oldIndex, int amount) throws MappedDatastoreException { if (orderMapping == null) { return null; } DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads(); DatastoreService service = DatastoreServiceFactoryInternal.getDatastoreService(config); AbstractClassMetaData acmd = elementCmd; String kind = storeMgr.getIdentifierFactory().newDatastoreContainerIdentifier(acmd).getIdentifierName(); Query q = new Query(kind); ExecutionContext ec = op.getExecutionContext(); Object id = ec.getApiAdapter().getTargetKeyForSingleFieldIdentity(op.getInternalObjectId()); Key key = id instanceof Key ? (Key) id : KeyFactory.stringToKey((String) id); q.setAncestor(key); // create an entity just to capture the name of the index property Entity entity = new Entity(kind); orderMapping.setObject(ec, entity, new int[] {1}, oldIndex); String indexProp = entity.getProperties().keySet().iterator().next(); q.addFilter(indexProp, Query.FilterOperator.GREATER_THAN_OR_EQUAL, oldIndex); for (Entity shiftMe : service.prepare(service.getCurrentTransaction(null), q).asIterable()) { Long pos = (Long) shiftMe.getProperty(indexProp); shiftMe.setProperty(indexProp, pos + amount); EntityUtils.putEntityIntoDatastore(ec, shiftMe); } return null; }
@Test public void testFilterByEntityKey() { Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByEntityKey"); Key parentKey = parentEntity.getKey(); Key fooKey = KeyFactory.createKey(parentKey, "foo", 1); Entity fooEntity = new Entity(fooKey); service.put(fooEntity); Query query = new Query("foo") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, EQUAL, fooKey)); PreparedQuery preparedQuery = service.prepare(query); List<Entity> results = preparedQuery.asList(FetchOptions.Builder.withDefaults()); assertEquals(1, results.size()); assertEquals(fooEntity, results.get(0)); }
@Test public void testSetFilterInt() { Query query = new Query(kindName, rootKey); List<Filter> filterList = new ArrayList<>(); filterList.add(Query.FilterOperator.EQUAL.of("intData", 20)); filterList.add(Query.FilterOperator.GREATER_THAN.of("intData", 0)); Filter filter = Query.CompositeFilterOperator.and(filterList); query.setFilter(filter); assertEquals(1, service.prepare(query).countEntities(fo)); }
@Test public void queriesDontReturnDeletedEntities() throws Exception { String methodName = "queriesDontReturnDeletedEntities"; Entity entity = createTestEntityWithUniqueMethodNameKey(SMOKE_TEST_ENTITY, methodName); Key key = entity.getKey(); service.put(entity); service.delete(key); List<Entity> entities = service.prepare(new Query(SMOKE_TEST_ENTITY).setAncestor(key)) .asList(FetchOptions.Builder.withDefaults()); assertEquals(0, entities.size()); }
public void purgeTestRunRecords() { DatastoreService datastoreService = DatastoreServiceFactory. getDatastoreService(); FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId); Query query = new Query(entityName).setFilter(testRunFilter).setKeysOnly(); for (Entity readRec : datastoreService.prepare(query).asIterable()) { datastoreService.delete(readRec.getKey()); } }
@Override public List<Entity> getAll(String kind) { Query query = new Query(kind); PreparedQuery prepared = datastore.prepare(query); return prepared.asList(FetchOptions.Builder.withChunkSize(CHUNKSIZE)); }
@Test public void testDistinctFilter() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.NOT_EQUAL, "string1")); query.addSort("stringData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(5, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); }
@Test public void testQueryWithoutAnyConstraints() throws Exception { Key parentKey = createQueryBasicsTestParent("testQueryWithoutAnyConstraints"); Entity person = new Entity("Person", parentKey); service.put(person); Entity address = new Entity("Address", parentKey); service.put(address); PreparedQuery preparedQuery = service.prepare(new Query().setAncestor(parentKey)); assertTrue(preparedQuery.countEntities(withDefaults()) >= 2); List<Entity> results = preparedQuery.asList(withDefaults()); assertTrue(results.containsAll(Arrays.asList(person, address))); }
private QueryResultList<Entity> getNamespaces() { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); fetchOptions.limit(BATCH_SIZE); if (cursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)); } Query q = new Query(NAMESPACE_METADATA_KIND); q.setKeysOnly(); return datastore.prepare(q).asQueryResultList(fetchOptions); }
@After public void tearDown() { List<Entity> entities = service.prepare(new Query(TEST_ENTITY_KIND).setKeysOnly()).asList(withDefaults()); for (Entity entity : entities) { service.delete(entity.getKey()); } }
@Test public void testEntityOnlyContainsProjectedProperties() throws Exception { String methodName = "testEntityOnlyContainsProjectedProperties"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity b = createEntity("Product", key) .withProperty("name", "b") .withProperty("price", 1L) .store(); Entity a = createEntity("Product", key) .withProperty("name", "a") .withProperty("price", 2L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("price", Long.class)) .setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("a", "b"))); Entity firstResult = service.prepare(query).asList(FetchOptions.Builder.withDefaults()).get(0); assertEquals(1, firstResult.getProperties().size()); assertEquals("price", firstResult.getProperties().keySet().iterator().next()); query = new Query("Product") .setKeysOnly() .setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("a", "b"))); firstResult = service.prepare(query).asList(FetchOptions.Builder.withDefaults()).get(0); assertEquals(0, firstResult.getProperties().size()); }
@Test public void testIndexListFromIterator() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly"); Key key = parent.getKey(); Entity tom = createEntity("Person", key) .withProperty("name", "Tom") .withProperty("surname", "Foe") .store(); Query query = new Query("Person") .setAncestor(key) .setKeysOnly(); PreparedQuery preparedQuery = service.prepare(query); QueryResultIterator<Entity> iter = preparedQuery.asQueryResultIterator(); List<Index> indexes = iter.getIndexList(); if (indexes != null) { // TODO -- something useful System.out.println("indexes = " + indexes); } }
private QueryResultList<Entity> executeQuery(FetchOptions fetchOptions) { Query query = new Query("Foo").addSort("bar"); return service.prepare(query).asQueryResultList(fetchOptions); }
/** * Make a new Query object that is exactly like the old. Too bad Query isn't Cloneable. */ protected com.google.appengine.api.datastore.Query cloneRawQuery(com.google.appengine.api.datastore.Query orig) { com.google.appengine.api.datastore.Query copy = new com.google.appengine.api.datastore.Query(orig.getKind(), orig.getAncestor()); for (FilterPredicate filter: orig.getFilterPredicates()) copy.addFilter(filter.getPropertyName(), filter.getOperator(), filter.getValue()); for (SortPredicate sort: orig.getSortPredicates()) copy.addSort(sort.getPropertyName(), sort.getDirection()); // This should be impossible but who knows what might happen in the future if (orig.isKeysOnly()) copy.setKeysOnly(); return copy; }