Refine search
@Before public void createData() throws InterruptedException, ParseException { Date[] testDatas = {dfDateTime.parse("2001,1,1,23,59,59"), dfDateTime.parse("2005,5,5,13,19,19"), dfDateTime.parse("2008,8,8,3,9,9")}; Query q = new Query(kindName, rootKey); if (service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()) == 0) { List<Entity> elist = new ArrayList<Entity>(); for (Date data : testDatas) { Entity newRec = new Entity(kindName, rootKey); newRec.setProperty(propertyName, data); elist.add(newRec); } service.put(elist); sync(waitTime); } }
@Override public List<Entity> getAll(String kind) { Query query = new Query(kind); PreparedQuery prepared = datastore.prepare(query); return prepared.asList(FetchOptions.Builder.withChunkSize(CHUNKSIZE)); }
private void checkQueryWithLimit(int limit) { FetchOptions fo = FetchOptions.Builder.withLimit(limit); Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); List<Entity> results = service.prepare(query).asList(fo); assertEquals(limit, results.size()); } }
clearData(kindName); List<Key> keys = new ArrayList<>(); Transaction tx = service.beginTransaction(); Entity parent = new Entity(kindName); parent.setProperty("check", "parent"); parent.setProperty("stamp", new Date()); Key pKey = service.put(tx, parent); keys.add(pKey); child.setProperty("check", "other"); child.setProperty("stamp", new Date()); Key cKey = service.put(tx, child); keys.add(cKey); tx.commit(); sync(sleepTime); Query q = new Query(kindName).setAncestor(pKey); int count = service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()); assertEquals(2, count);
@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 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()); }
@Test public void testWithIneqi() { Query query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("loc", Query.FilterOperator.EQUAL, "ae")); Key key = service.prepare(query).asSingleEntity().getKey(); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.GREATER_THAN, key)); query.addSort("__key__"); List<Entity> ascRecs = service.prepare(query).asList(withLimit(5)); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.GREATER_THAN, key)); query.addSort("__key__", Query.SortDirection.DESCENDING); List<Entity> descRecs = service.prepare(query).asList(withLimit(5)); int size = ascRecs.size(); for (int i = 0; i < size; i++) { assertEquals(ascRecs.get(i).getProperty("pop").toString(), descRecs.get(size - i - 1).getProperty("pop").toString()); } }
Entity childB = new Entity("child", parentB.getKey()); service.beginTransaction(); service.put(parentA); service.getCurrentTransaction().commit(); service.beginTransaction(); service.beginTransaction(); Query query = new Query("child", parentA.getKey()); Transaction tx = service.getCurrentTransaction(); int numRows = service.prepare(tx, query).countEntities(FetchOptions.Builder.withDefaults()); tx.commit(); assertEquals(1, numRows); service.beginTransaction(); tx = service.getCurrentTransaction(); Entity result = service.prepare(tx, query).asSingleEntity(); assertEquals(childA.getKey(), result.getKey()); tx.commit();
Query q = new Query(kindTest); if (service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()) == 0) { entity = new Entity(kindTest); if (aNamespaceDat.equals("")) { entity.setProperty("jobType", aNamespaceDat); service.put(entity); } else { entity = service.prepare(q).asSingleEntity(); Query q = new Query(kindTest); q.setFilter(new FilterPredicate("__key__", Query.FilterOperator.EQUAL, kList.get(i))); if (namespaceDat[i].equals("")) { assertEquals(service.prepare(q).asSingleEntity().getProperty("jobType"), "google"); } else {
@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()); }
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); }
public static QueryResult queryEntityPrivate(LeanQuery leanQuery) throws LeanException { LeanAccount account = findCurrentAccount(); Query query = new Query(leanQuery.getKind()); query.addFilter("_account", Query.FilterOperator.EQUAL, account.id); for (QueryFilter queryFilter : leanQuery.getFilters()) { query.addFilter( queryFilter.getProperty(), queryFilter.getOperator().getFilterOperator(), queryFilter.getValue()); } for (QuerySort querySort : leanQuery.getSorts()) { query.addSort(querySort.getProperty(), querySort.getDirection().getSortDirection()); } FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if(leanQuery.getCursor() != null ) fetchOptions.startCursor(leanQuery.getCursor()); if(leanQuery.getOffset() != null) fetchOptions.offset(leanQuery.getOffset()); if(leanQuery.getLimit() != null) fetchOptions.limit(leanQuery.getLimit()); try { PreparedQuery pq = datastore.prepare(query); QueryResultList<Entity> result; result = pq.asQueryResultList(fetchOptions); return new QueryResult(result, result.getCursor()); } catch (DatastoreNeedIndexException dnie) { throw new LeanException(LeanException.Error.AppEngineMissingIndex); } }
private void deleteAll(final String kind, final Key rootJobKey) { logger.info("Deleting all " + kind + " with rootJobKey=" + rootJobKey); final int chunkSize = 100; final FetchOptions fetchOptions = FetchOptions.Builder.withChunkSize(chunkSize); final PreparedQuery preparedQuery = dataStore.prepare(new Query(kind).setKeysOnly().setFilter( new FilterPredicate(ROOT_JOB_KEY_PROPERTY, EQUAL, rootJobKey))); tryFiveTimes(new Operation<Void>("delete") { @Override public Void call() { Iterator<Entity> iter = preparedQuery.asIterator(fetchOptions); while (iter.hasNext()) { ArrayList<Key> keys = new ArrayList<>(chunkSize); for (int i = 0; i < chunkSize && iter.hasNext(); i++) { keys.add(iter.next().getKey()); } logger.info("Deleting " + keys.size() + " " + kind + "s with rootJobKey=" + rootJobKey); dataStore.delete(null, keys); } return null; } }); }
@Test public void testKindless() { Query query = new Query(PARENTKIND); query.setFilter(new FilterPredicate("name", Query.FilterOperator.EQUAL, "argonaut")); Entity parent = service.prepare(query).asSingleEntity(); query = new Query(parent.getKey()); assertEquals(3, service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults())); query = new Query().setAncestor(parent.getKey()); assertEquals(3, service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults())); }
public static List<Entity> getPrivateEntities(String kind) throws LeanException { if (!pattern.matcher(kind).matches()) { throw new LeanException(LeanException.Error.IllegalEntityName); } LeanAccount account = AuthService.getCurrentAccount(); // this should not happen, but we check anyway if (account == null) throw new LeanException(LeanException.Error.NotAuthorized); Query query = new Query(kind); query.addFilter("_account", Query.FilterOperator.EQUAL, account.id); PreparedQuery pq = datastore.prepare(query); return pq.asList(FetchOptions.Builder.withDefaults()); }
Query q = new Query("_AE_Backup_Information"); filters.add(less); CompositeFilter comp = new CompositeFilter(CompositeFilterOperator.AND, filters); q.setFilter(comp); PreparedQuery pq = datastore.prepare(q); List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(1)); if (results.size() != 1 || !results.get(0).getProperty("name").toString().contains(backupName)) { System.err.println("BuiltinDatatoreToBigqueryIngesterTask: can't find backupName: " + backupName);
@Test public void testDiffNamespace() { NamespaceManager.set(namespaceDat[1]); Query q = new Query(kindName); q.setFilter(new FilterPredicate("jobType", Query.FilterOperator.EQUAL, stringDat[2] + 1)); int ttl = service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()); assertEquals(0, ttl); }
public List<Entity> queryAll(final String kind, final Key rootJobKey) { Query query = new Query(kind); query.setFilter(new FilterPredicate(ROOT_JOB_KEY_PROPERTY, EQUAL, rootJobKey)); final PreparedQuery preparedQuery = dataStore.prepare(query); final FetchOptions options = FetchOptions.Builder.withChunkSize(500); return tryFiveTimes(new Operation<List<Entity>>("queryFullPipeline") { @Override public List<Entity> call() { return preparedQuery.asList(options); } }); }
@Test public void testCursor() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly"); Key key = parent.getKey(); Entity john = createEntity("Person", key) .withProperty("name", "John") .withProperty("surname", "Doe") .store(); Query query = new Query("Person") .setAncestor(key) .setKeysOnly(); PreparedQuery preparedQuery = service.prepare(query); QueryResultIterator<Entity> iter = preparedQuery.asQueryResultIterator(); Assert.assertNotNull(iter.next()); Cursor cursor = iter.getCursor(); iter = service.prepare(query).asQueryResultIterator(FetchOptions.Builder.withStartCursor(cursor)); Assert.assertFalse(iter.hasNext()); }
@Test @InSequence(8) public void testStep8FetchOption() { Query q = new Query(kindName, rootKey).addSort("count", Query.SortDirection.DESCENDING); Entity e = service.prepare(q).asIterator().next(); assertEquals(new Integer(bigCount - 1).longValue(), e.getProperty("count")); FetchOptions foTest = FetchOptions.Builder.withDefaults(); int ttl = service.prepare(q).countEntities(foTest.limit(500)); assertEquals(500, ttl); foTest = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.offset(150)); assertEquals((bigCount - 150), ttl); fo = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.offset(50).limit(150)); assertEquals(150, ttl); fo = FetchOptions.Builder.withDefaults(); ttl = service.prepare(q).countEntities(foTest.limit(150).offset(offset)); int expect = (150 < (bigCount - offset)) ? 150 : (bigCount - offset); assertEquals(expect, ttl); }