@Test public void pagingWithBoundsCriteria() throws Exception { UUID applicationId = createApplication("testOrganization", "pagingWithBoundsCriteria"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); int size = 40; List<UUID> entityIds = new ArrayList<UUID>(); for (int i = 0; i < size; i++) { Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("index", i); Entity created = em.create("page", properties); entityIds.add(created.getUuid()); } int pageSize = 10; Query query = new Query(); query.setLimit(pageSize); query.addFilter("index >= 10"); query.addFilter("index <= 29"); Results r = null; // check they're all the same before deletion for (int i = 1; i < 3; i++) { r = em.searchCollection(em.getApplicationRef(), "pages", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(pageSize, r.size()); for (int j = 0; j < pageSize; j++) { assertEquals(entityIds.get(i * pageSize + j), r.getEntities().get(j).getUuid()); } query.setCursor(r.getCursor()); } r = em.searchCollection(em.getApplicationRef(), "pages", query); assertEquals(0, r.size()); assertNull(r.getCursor()); }
@Test public void testCase2() throws ODKDatastoreException { CallingContext cc = TestContextFactory.getCallingContext(); Datastore ds = cc.getDatastore(); User user = cc.getCurrentUser(); MyRelation rel = MyRelation.assertRelation(cc); Query query = ds.createQuery(rel, "QueryResultTest.testCase2", user); query.addFilter(MyRelation.fieldInt, FilterOperation.GREATER_THAN, SET_SIZE - 2); query.addSort(MyRelation.fieldDate, Direction.ASCENDING); query.addSort(MyRelation.fieldDbl, Direction.DESCENDING); Set<String> pkSet = new HashSet<String>(); int len = 0; QueryResult result = query.executeQuery(null, 10); len += result.getResultList().size(); assertEquals(true, result.hasMoreResults()); assertEquals(10, result.getResultList().size()); for (CommonFieldsBase cb : result.getResultList()) { assertEquals(false, pkSet.contains(cb.getUri())); pkSet.add(cb.getUri()); ((MyRelation) cb).print(); } boolean done = false; QueryResumePoint startCursor = result.getResumeCursor(); while (!done) { System.out.println("Issuing follow-up query"); result = query.executeQuery(startCursor, 20); len += result.getResultList().size(); startCursor = result.getResumeCursor(); done = !result.hasMoreResults(); for (CommonFieldsBase cb : result.getResultList()) { assertEquals(false, pkSet.contains(cb.getUri())); pkSet.add(cb.getUri()); ((MyRelation) cb).print(); } } assertEquals(false, result.hasMoreResults()); assertEquals(2 * values.length, len); System.out.println("done with testCase2"); }
public static List<StorageNode> getStorageNodesInFolder(String path, Owner owner) { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); ArrayList<StorageNode> nodes = new ArrayList<StorageNode>(); String folderPath = PathHelper.getFolderpath(path); // Read folders Query q = new Query("Folder"); q.addFilter("path", FilterOperator.GREATER_THAN_OR_EQUAL, path); q.addFilter("path", FilterOperator.LESS_THAN, path + "\ufffd"); q.addFilter("ownerId", FilterOperator.EQUAL, owner.getId()); PreparedQuery pq = ds.prepare(q); List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(99999)); for (Entity e : results) { Folder f = new Folder(); ReflectionHelper.setPropertiesFromEntity(Folder.class, f, e); log.debug("Folder compare: {} =?= {}", PathHelper.getFolderpath(f.getPath()), folderPath); if (PathHelper.getFolderpath(f.getPath()).equals(folderPath)) { nodes.add(f); } } // Read files Query q2 = new Query("File"); q2.addFilter("path", FilterOperator.GREATER_THAN_OR_EQUAL, path); q2.addFilter("path", FilterOperator.LESS_THAN, path + "\ufffd"); q2.addFilter("ownerId", FilterOperator.EQUAL, owner.getId()); PreparedQuery pq2 = ds.prepare(q); List<Entity> results2 = pq2.asList(FetchOptions.Builder.withLimit(99999)); for (Entity e : results2) { File f = new File(); ReflectionHelper.setPropertiesFromEntity(File.class, f, e); log.debug("File compare: {} =?= {}", PathHelper.getFolderpath(f.getPath()), folderPath); if (PathHelper.getFolderpath(f.getPath()).equals(folderPath)) { nodes.add(f); } } return nodes; }
@Test public void testCase3() throws ODKDatastoreException { CallingContext cc = TestContextFactory.getCallingContext(); Datastore ds = cc.getDatastore(); User user = cc.getCurrentUser(); MyRelation rel = MyRelation.assertRelation(cc); System.out.println("start testCase3"); Query query = ds.createQuery(rel, "QueryResultTest.testCase3(1st)", user); query.addFilter(MyRelation.fieldDbl, FilterOperation.EQUAL, new BigDecimal("0.9")); query.addFilter(MyRelation.fieldBool, FilterOperation.EQUAL, true); query.addSort(MyRelation.fieldInt, Direction.ASCENDING); query.addSort(MyRelation.fieldDate, Direction.DESCENDING); Query backquery = ds.createQuery(rel, "QueryResultTest.testCase3(2nd)", user); backquery.addFilter(MyRelation.fieldDbl, FilterOperation.EQUAL, new BigDecimal("0.9")); backquery.addFilter(MyRelation.fieldBool, FilterOperation.EQUAL, true); backquery.addSort(MyRelation.fieldInt, Direction.DESCENDING); backquery.addSort(MyRelation.fieldDate, Direction.ASCENDING); Set<String> pkTotalSet = new HashSet<String>(); List<String> pkOrdering = new ArrayList<String>(); int TOTAL_SIZE = 3 * SET_SIZE; int fetchSizes[] = {1, 2, 1021, 303, 101, 2831}; int idxFetch = 4; int len = 0; QueryResult result = query.executeQuery(null, fetchSizes[idxFetch]); len += result.getResultList().size(); assertEquals(true, result.hasMoreResults()); assertEquals(fetchSizes[idxFetch], result.getResultList().size()); System.out.println("Accumulating forward query results"); for (CommonFieldsBase cb : result.getResultList()) { assertEquals(false, pkOrdering.contains(cb.getUri())); assertEquals(false, pkTotalSet.contains(cb.getUri())); pkOrdering.add(cb.getUri()); pkTotalSet.add(cb.getUri()); ((MyRelation) cb).print(); } System.out.println("Verifying initial backward query is empty"); QueryResult backResult = backquery.executeQuery(result.getBackwardCursor(), pkOrdering.size()); assertEquals(false, backResult.hasMoreResults()); assertEquals(0, backResult.getResultList().size()); boolean notFirst = false; boolean done = false; QueryResumePoint startCursor = result.getResumeCursor(); while (!done) { idxFetch = (idxFetch + 1) % fetchSizes.length; System.out.println("Issuing follow-up query"); result = query.executeQuery(startCursor, fetchSizes[idxFetch]); len += result.getResultList().size(); startCursor = result.getResumeCursor(); done = !result.hasMoreResults(); System.out.println("Verifying backward query against ordering of earlier result"); backResult = backquery.executeQuery(result.getBackwardCursor(), pkOrdering.size()); assertEquals(notFirst, backResult.hasMoreResults()); notFirst = true; assertEquals(pkOrdering.size(), backResult.getResultList().size()); for (int i = 0; i < pkOrdering.size(); ++i) { CommonFieldsBase cb = backResult.getResultList().get(i); ((MyRelation) cb).print(); assertEquals(pkOrdering.get(pkOrdering.size() - i - 1), cb.getUri()); } System.out.println("Accumulating forward query results"); pkOrdering.clear(); for (CommonFieldsBase cb : result.getResultList()) { assertEquals(false, pkOrdering.contains(cb.getUri())); assertEquals(false, pkTotalSet.contains(cb.getUri())); pkOrdering.add(cb.getUri()); pkTotalSet.add(cb.getUri()); ((MyRelation) cb).print(); } } idxFetch = (idxFetch + 1) % fetchSizes.length; System.out.println("Before Issuing (what should be empty) follow-up query"); result = query.executeQuery(startCursor, fetchSizes[idxFetch]); len += result.getResultList().size(); startCursor = result.getResumeCursor(); done = !result.hasMoreResults(); System.out.println("Verifying backward query against ordering of earlier result"); // backquery should match existing data backResult = backquery.executeQuery(result.getBackwardCursor(), pkOrdering.size()); assertEquals(true, backResult.hasMoreResults()); assertEquals(pkOrdering.size(), backResult.getResultList().size()); for (int i = 0; i < pkOrdering.size(); ++i) { CommonFieldsBase cb = backResult.getResultList().get(i); ((MyRelation) cb).print(); assertEquals(pkOrdering.get(pkOrdering.size() - i - 1), cb.getUri()); } assertEquals(false, result.hasMoreResults()); assertEquals(0, result.getResultList().size()); idxFetch = (idxFetch + 1) % fetchSizes.length; System.out.println("Before Re-Issuing (what should be empty) follow-up query"); // this should be an empty list result = query.executeQuery(startCursor, fetchSizes[idxFetch]); assertEquals(false, result.hasMoreResults()); assertEquals(0, result.getResultList().size()); System.out.println("Verifying backward query (again) against ordering of earlier result"); // backquery should match existing data backResult = backquery.executeQuery(result.getBackwardCursor(), pkOrdering.size()); assertEquals(true, backResult.hasMoreResults()); assertEquals(pkOrdering.size(), backResult.getResultList().size()); for (int i = 0; i < pkOrdering.size(); ++i) { CommonFieldsBase cb = backResult.getResultList().get(i); ((MyRelation) cb).print(); assertEquals(pkOrdering.get(pkOrdering.size() - i - 1), cb.getUri()); } assertEquals(TOTAL_SIZE, len); }
private Entity getBlobInfoEntityThroughQuery(String creationHandle) { Query query = new Query(BlobInfoFactory.KIND); query.addFilter(BLOB_INFO_CREATION_HANDLE_PROPERTY, Query.FilterOperator.EQUAL, creationHandle); return getDatastoreService().prepare(query).asSingleEntity(); }
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { int index; DatastoreService ds; MemcacheService ms; Cookie[] cookies; boolean insideFlag; String delpw; String paramOffset; String paramSize; int offset; int size; Key postObjGroupKey; Query q; List<Entity> postObjList; PostObj postObj; Gson gson; List<String> filelinkList; resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain"); ds = DatastoreServiceFactory.getDatastoreService(); ms = MemcacheServiceFactory.getMemcacheService(); insideFlag = false; try { cookies = req.getCookies(); if (cookies.length > 0) { if (ms.contains(cookies[0].getValue()) == true) { insideFlag = true; } } } catch (Exception e) { insideFlag = false; } delpw = req.getParameter("delpw"); if (delpw != null) { if (delpw.equals("") == true) { delpw = null; } } paramOffset = req.getParameter("offset"); if (paramOffset != null) { offset = Integer.valueOf(paramOffset); } else { offset = 0; } paramSize = req.getParameter("size"); if (paramSize != null) { size = Integer.valueOf(paramSize); } else { size = 4096; } postObjGroupKey = KeyFactory.createKey("PostObjGroup", 1L); q = new Query("PostObj", postObjGroupKey); if (delpw != null) { q.addFilter("delpw", FilterOperator.EQUAL, delpw); q.addSort("delpw"); } q.addSort("posttime", SortDirection.DESCENDING); postObjList = ds.prepare(q).asList(FetchOptions.Builder.withOffset(offset).limit(size)); postObj = new PostObj(); filelinkList = new ArrayList<String>(); for (index = 0; index < postObjList.size(); index++) { postObj.getDB(postObjList.get(index)); if ((postObj.flag.equals("showgallery") == false && insideFlag == true) || (postObj.flag.equals("showgallery") == true && insideFlag == false) || delpw != null) { filelinkList.add(postObj.filelink); } } gson = new Gson(); resp.getWriter().print(gson.toJson(filelinkList)); }