@SuppressWarnings("unchecked") public static Results fromConnections( List<? extends ConnectionRef> connections, boolean forward) { Results r = new Results(); r.setConnections((List<ConnectionRef>) connections, forward); return r; }
@Test public void userLastNameSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "testLastName"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String lastName = "lastName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("lastname", lastName); Entity user = em.create("user", properties); assertNotNull(user); // EntityRef Query query = new Query(); query.addEqualityFilter("lastname", lastName); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); }
public static Results fromCounters(AggregateCounterSet counters) { Results r = new Results(); List<AggregateCounterSet> l = new ArrayList<AggregateCounterSet>(); l.add(counters); r.setCounters(l); return r; }
@Test public void testNotQueryAnd() throws Exception { UUID applicationId = createApplication("testOrganization", "testNotQueryAnd"); EntityManager em = emf.getEntityManager(applicationId); Map<String, Object> location = new LinkedHashMap<String, Object>(); location.put("Place", "24 Westminster Avenue, Venice, CA 90291, USA"); location.put("Longitude", -118.47425979999998); location.put("Latitude", 33.9887663); Map<String, Object> recipient = new LinkedHashMap<String, Object>(); recipient.put("TimeRequested", 1359077878l); recipient.put("Username", "fb_536692245"); recipient.put("Location", location); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("Flag", "requested"); properties.put("Recipient", recipient); em.create("loveobject", properties); location = new LinkedHashMap<String, Object>(); location.put( "Place", "Via Pietro Maroncelli, 48, 62012 Santa Maria Apparente Province of Macerata, Italy"); location.put("Longitude", 13.693080199999999); location.put("Latitude", 43.2985019); recipient = new LinkedHashMap<String, Object>(); recipient.put("TimeRequested", 1359077878l); recipient.put("Username", "fb_100000787138041"); recipient.put("Location", location); properties = new LinkedHashMap<String, Object>(); properties.put("Flag", "requested"); properties.put("Recipient", recipient); em.create("loveobject", properties); // String s = "select * where Flag = 'requested'"; // String s = // "select * where Flag = 'requested' and NOT Recipient.Username = '******' order by // created asc"; String s = "select * where Flag = 'requested' and NOT Recipient.Username = '******' order by created asc"; Query query = Query.fromQL(s); Results r = em.searchCollection(em.getApplicationRef(), "loveobjects", query); assertTrue(r.size() == 1); String username = (String) ((Map) r.getEntities().get(0).getProperty("Recipient")).get("Username"); // selection results should be a list of lists List<Object> sr = query.getSelectionResults(r); assertTrue(sr.size() == 1); assertEquals("fb_100000787138041", username); }
@Test public void groupTitleSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "groupTitleSearch"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String titleName = "groupName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("title", titleName); properties.put("path", "testPath"); properties.put("name", "testName"); Entity group = em.create("group", properties); assertNotNull(group); // EntityRef Query query = new Query(); query.addEqualityFilter("title", titleName); Results r = em.searchCollection(em.getApplicationRef(), "groups", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(group.getUuid(), returned.getUuid()); }
public ServiceResults updateApplicationEntity(ServicePayload payload) throws Exception { Map<String, Object> properties = payload.getProperties(); Object m = properties.get("metadata"); if (m instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> metadata = (Map<String, Object>) m; Object c = metadata.get("collections"); if (c instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> collections = (Map<String, Object>) c; for (String collection : collections.keySet()) { em.createApplicationCollection(collection); logger.info( "Created collection " + collection + " for application " + sm.getApplicationId()); } } } Entity entity = em.get(em.getApplicationRef()); em.updateProperties(entity, properties); entity.addProperties(properties); Results r = Results.fromEntity(entity); Set<String> collections = em.getApplicationCollections(); if (collections.size() > 0) { r.setMetadata(em.getApplicationRef().getUuid(), "collections", collections); } return genericServiceResults(r); }
@Test public void testKeywordsAndQuery() throws Exception { logger.info("testKeywordsOrQuery"); UUID applicationId = createApplication("testOrganization", "testKeywordsAndQuery"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("title", "Galactians 2"); properties.put("keywords", "Hot, Space Invaders, Classic"); Entity firstGame = em.create("game", properties); properties = new LinkedHashMap<String, Object>(); properties.put("title", "Bunnies Extreme"); properties.put("keywords", "Hot, New"); Entity secondGame = em.create("game", properties); properties = new LinkedHashMap<String, Object>(); properties.put("title", "Hot Shots Extreme"); properties.put("keywords", "Action, New"); Entity thirdGame = em.create("game", properties); Query query = Query.fromQL("select * where keywords contains 'new' and title contains 'extreme'"); Results r = em.searchCollection(em.getApplicationRef(), "games", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(2, r.size()); assertEquals(secondGame.getUuid(), r.getEntities().get(0).getUuid()); assertEquals(thirdGame.getUuid(), r.getEntities().get(1).getUuid()); }
@Test public void testRedefineTerms() throws Exception { UUID applicationId = createApplication("testOrganization", "testRedefineTerms"); EntityManager em = emf.getEntityManager(applicationId); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); em.create("user", properties); String s = "select {name: username, email: email} where username = '******'"; Query query = Query.fromQL(s); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() == 1); // selection results should be a list of lists List<Object> sr = query.getSelectionResults(r); assertTrue(sr.size() == 1); Map firstResult = (Map) sr.get(0); assertTrue("edanuff".equals(firstResult.get("name"))); assertTrue("*****@*****.**".equals(firstResult.get("email"))); }
@Test public void testSecondarySorts() throws Exception { logger.info("testSecondarySorts"); UUID applicationId = createApplication("testOrganization", "testSecondarySorts"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); for (int i = alphabet.length - 1; i >= 0; i--) { String name = alphabet[i]; Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("name", name); properties.put("group", i / 3); properties.put("reverse_name", alphabet[alphabet.length - 1 - i]); em.create("item", properties); } Query query = Query.fromQL("group = 1 order by name desc"); Results r = em.searchCollection(em.getApplicationRef(), "items", query); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); int i = 6; for (Entity entity : r.getEntities()) { i--; assertEquals(1L, entity.getProperty("group")); assertEquals(alphabet[i], entity.getProperty("name")); } assertEquals(3, i); }
public static Results fromRef(EntityRef ref) { if (ref instanceof Entity) { return fromEntity((Entity) ref); } Results r = new Results(); r.setRef(ref); return r; }
@Test public void testEntityManager() throws Exception { logger.info("EntityManagerTest.testEntityManagerTest"); UUID applicationId = createApplication("testEntityManagerTest"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user = em.create("user", properties); assertNotNull(user); user = em.get(user); assertNotNull(user); assertEquals("user.username not expected value", "edanuff", user.getProperty("username")); assertEquals("user.email not expected value", "*****@*****.**", user.getProperty("email")); EntityRef userRef = em.getAlias(applicationId, "user", "edanuff"); assertNotNull(userRef); assertEquals("userRef.id not expected value", user.getUuid(), userRef.getUuid()); assertEquals("userRef.type not expected value", "user", userRef.getType()); logger.info("user.username: "******"username")); logger.info("user.email: " + user.getProperty("email")); Results results = em.searchCollection( em.getApplicationRef(), "users", new Query().addEqualityFilter("username", "edanuff")); assertNotNull(results); assertEquals(1, results.size()); user = results.getEntity(); assertNotNull(user); assertEquals("user.username not expected value", "edanuff", user.getProperty("username")); assertEquals("user.email not expected value", "*****@*****.**", user.getProperty("email")); logger.info("user.username: "******"username")); logger.info("user.email: " + user.getProperty("email")); results = em.searchCollection( em.getApplicationRef(), "users", new Query().addEqualityFilter("email", "*****@*****.**")); assertNotNull(results); assertEquals(1, results.size()); user = results.getEntity(); assertNotNull(user); assertEquals("user.username not expected value", "edanuff", user.getProperty("username")); assertEquals("user.email not expected value", "*****@*****.**", user.getProperty("email")); logger.info("user.username: "******"username")); logger.info("user.email: " + user.getProperty("email")); }
public static Results fromId(UUID id) { Results r = new Results(); if (id != null) { List<UUID> l = new ArrayList<UUID>(); l.add(id); r.setIds(l); } return r; }
public static Results fromIdList(List<UUID> l, String type) { if (type == null) { return fromIdList(l); } List<EntityRef> refs = new ArrayList<EntityRef>(); for (UUID u : l) { refs.add(ref(type, u)); } Results r = new Results(); r.setRefs(refs); return r; }
@Test @Ignore("Really slow on the delete, not a good unit tests atm") public void deleteBatchTest() throws Exception { DB db = getDb(); int count = (int) (OpDelete.BATCH_SIZE * 1.5); List<DBObject> docs = new ArrayList<DBObject>(count); for (int i = 0; i < count; i++) { BasicDBObject doc = new BasicDBObject(); doc.put("index", i); docs.add(doc); } WriteResult result = db.getCollection("deletebatchtests").insert(docs); assertNull(result.getLastError().getErrorMessage()); // iterate over all the data to make sure it's been inserted DBCursor cursor = db.getCollection("deletebatchtests").find(); for (int i = 0; i < count && cursor.hasNext(); i++) { int index = new BasicDBObject(cursor.next().toMap()).getInt("index"); assertEquals(i, index); } BasicDBObject query = new BasicDBObject(); query.put("index", new BasicDBObject("$lte", count)); // now delete the objects db.getCollection("deletebatchtests").remove(query, WriteConcern.SAFE); // now try and iterate, there should be no results cursor = db.getCollection("deletebatchtests").find(); assertFalse(cursor.hasNext()); // check it has been deleted UUID appId = emf.lookupApplication("test-organization/test-app"); EntityManager em = emf.getEntityManager(appId); Results results = em.searchCollection( new SimpleEntityRef("application", appId), "deletebatchtests", new Query()); assertEquals(0, results.size()); }
public ServiceResults getApplicationEntity() throws Exception { Entity entity = em.get(em.getApplicationRef()); Results r = Results.fromEntity(entity); Map<String, Object> collections = em.getApplicationCollectionMetadata(); // Set<String> collections = em.getApplicationCollections(); if (collections.size() > 0) { r.setMetadata(em.getApplicationRef().getUuid(), "collections", collections); } return genericServiceResults(r); }
/** * Perform an intersection of the 2 results * * @param results */ public void and(Results results) { getEntitiesMap(); results.getEntitiesMap(); if ((entitiesMap != null) && (results.entitiesMap != null)) { Map<UUID, Entity> newMap = new LinkedHashMap<UUID, Entity>(); for (Map.Entry<UUID, Entity> e : entitiesMap.entrySet()) { if (results.entitiesMap.containsKey(e.getKey())) { newMap.put(e.getKey(), e.getValue()); } } entitiesMap = newMap; entities = new ArrayList<Entity>(entitiesMap.values()); level = Level.ALL_PROPERTIES; return; } getRefsMap(); results.getRefsMap(); if ((refsMap != null) && (results.refsMap != null)) { Map<UUID, EntityRef> newMap = new LinkedHashMap<UUID, EntityRef>(); for (Map.Entry<UUID, EntityRef> e : refsMap.entrySet()) { if (results.refsMap.containsKey(e.getKey())) { newMap.put(e.getKey(), e.getValue()); } } refsMap = newMap; refs = new ArrayList<EntityRef>(refsMap.values()); level = Level.REFS; ids = null; return; } getIdSet(); results.getIdSet(); if ((idSet != null) && (results.idSet != null)) { Set<UUID> newSet = new LinkedHashSet<UUID>(); for (UUID uuid : idSet) { if (results.idSet.contains(uuid)) { newSet.add(uuid); } } idSet = newSet; ids = new ArrayList<UUID>(idSet); level = Level.IDS; return; } // should be empty init(); }
@Test public void subpropertyQuerying() throws Exception { Map<String, Object> root = new HashMap<String, Object>(); Map<String, Object> subEntity = new HashMap<String, Object>(); root.put("rootprop1", "simpleprop"); subEntity.put("intprop", 10); subEntity.put("substring", "I'm a tokenized string that should be indexed"); root.put("subentity", subEntity); UUID applicationId = createApplication("testOrganization", "subpropertyQuerying"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Entity saved = em.create("test", root); Query query = new Query(); query.addEqualityFilter("rootprop1", "simpleprop"); Results results = em.searchCollection(em.getApplicationRef(), "tests", query); Entity entity = results.getEntitiesMap().get(saved.getUuid()); assertNotNull(entity); // query on the nested int value query = new Query(); query.addEqualityFilter("subentity.intprop", 10); results = em.searchCollection(em.getApplicationRef(), "tests", query); entity = results.getEntitiesMap().get(saved.getUuid()); assertNotNull(entity); // query on the nexted tokenized value query = new Query(); query.addContainsFilter("subentity.substring", "tokenized"); query.addContainsFilter("subentity.substring", "indexed"); results = em.searchCollection(em.getApplicationRef(), "tests", query); entity = results.getEntitiesMap().get(saved.getUuid()); assertNotNull(entity); }
@Test public void testSelectEmailViaConnection() throws Exception { UUID applicationId = createApplication("testOrganization", "testSelectEmail"); EntityManager em = emf.getEntityManager(applicationId); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "*****@*****.**"); properties.put("email", "*****@*****.**"); em.create("user", properties); String s = "select * where username = '******'"; Query query = Query.fromQL(s); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() == 1); // selection results should be a list of lists Entity entity = r.getEntity(); assertTrue("*****@*****.**".equals(entity.getProperty("username"))); assertTrue("*****@*****.**".equals(entity.getProperty("email"))); // now create a role and connect it properties = new LinkedHashMap<String, Object>(); properties.put("name", "test"); Entity foo = em.create("foo", properties); em.createConnection(foo, "testconnection", entity); // now query via the testConnection, this should work query = Query.fromQL(s); query.setConnectionType("testconnection"); query.setEntityType("user"); r = em.searchConnectedEntities(foo, query); assertTrue(r.size() == 1); // selection results should be a list of lists entity = r.getEntity(); assertTrue("*****@*****.**".equals(entity.getProperty("username"))); assertTrue("*****@*****.**".equals(entity.getProperty("email"))); }
@Test public void testSubkeys() throws Exception { UUID applicationId = createApplication("testOrganization", "testSubkeys"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user = em.create("user", properties); assertNotNull(user); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "tweet"); properties.put("content", "I ate a sammich"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "post"); properties.put("content", "I wrote a blog post"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "tweet"); properties.put("content", "I ate another sammich"); em.addToCollection(user, "activities", em.create("activity", properties)); properties = new LinkedHashMap<String, Object>(); properties.put("actor", hashMap("displayName", "Ed Anuff").map("objectType", "person")); properties.put("verb", "post"); properties.put("content", "I wrote another blog post"); em.addToCollection(user, "activities", em.create("activity", properties)); Results r = em.searchCollection(user, "activities", Query.searchForProperty("verb", "post")); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(2, r.size()); }
@Test public void testGroups() throws Exception { UUID applicationId = createApplication("testOrganization", "testGroups"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); Entity user1 = em.create("user", properties); assertNotNull(user1); properties = new LinkedHashMap<String, Object>(); properties.put("username", "djacobs"); properties.put("email", "*****@*****.**"); Entity user2 = em.create("user", properties); assertNotNull(user2); properties = new LinkedHashMap<String, Object>(); properties.put("path", "group1"); Entity group = em.create("group", properties); assertNotNull(group); em.addToCollection(group, "users", user1); em.addToCollection(group, "users", user2); properties = new LinkedHashMap<String, Object>(); properties.put("nickname", "ed"); em.updateProperties(new SimpleCollectionRef(group, "users", user1), properties); Results r = em.searchCollection( group, "users", new Query() .addEqualityFilter("member.nickname", "ed") .withResultsLevel(Results.Level.LINKED_PROPERTIES)); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(1, r.size()); em.removeFromCollection(user1, "groups", group); }
@Test public void andQuery() throws Exception { UUID applicationId = createApplication("testOrganization", "andQuery"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "blah,test,game"); properties.put("title", "Solitaire"); Entity game1 = em.create("game", properties); assertNotNull(game1); properties = new LinkedHashMap<String, Object>(); properties.put("keywords", "random,test"); properties.put("title", "Hearts"); Entity game2 = em.create("game", properties); assertNotNull(game2); // overlap Query query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'random'"); Results r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(1, r.size()); // disjoint query = Query.fromQL("select * where keywords contains 'random' AND keywords contains 'blah'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); // same each side query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'test'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(2, r.size()); Entity returned = r.getEntities().get(0); assertEquals(game1.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(game2.getUuid(), returned.getUuid()); // one side, left query = Query.fromQL("select * where keywords contains 'test' AND keywords contains 'foobar'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); // one side, right query = Query.fromQL("select * where keywords contains 'foobar' AND keywords contains 'test'"); r = em.searchCollection(em.getApplicationRef(), "games", query); assertEquals(0, r.size()); }
@Test public void emptyQueryReverse() throws Exception { UUID applicationId = createApplication("testOrganization", "testEmptyQueryReverse"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String firstName = "firstName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("firstname", firstName); Entity user = em.create("user", properties); assertNotNull(user); properties = new LinkedHashMap<String, Object>(); properties.put("username", "djacobs"); properties.put("email", "*****@*****.**"); Entity user2 = em.create("user", properties); assertNotNull(user2); // EntityRef Query query = new Query(); query.setReversed(true); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertEquals(2, r.size()); Entity returned = r.getEntities().get(0); assertEquals(user2.getUuid(), returned.getUuid()); returned = r.getEntities().get(1); assertEquals(user.getUuid(), returned.getUuid()); }
@Override public Results getAggregateQueueCounters( String queuePath, String category, String counterName, CounterResolution resolution, long start, long finish, boolean pad) { return Results.fromCounters( getAggregateCounters( getQueueId(queuePath), category, counterName, resolution, start, finish, pad)); }
@Test public void userFirstNameSearch() throws Exception { UUID applicationId = createApplication("testOrganization", "testFirstName"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); String firstName = "firstName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("username", "edanuff"); properties.put("email", "*****@*****.**"); properties.put("firstname", firstName); Entity user = em.create("user", properties); assertNotNull(user); // EntityRef Query query = new Query(); query.addEqualityFilter("firstname", firstName); Results r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); Entity returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); // update the username String newFirstName = "firstName" + UUIDUtils.newTimeUUID(); user.setProperty("firstname", newFirstName); em.update(user); // search with the old username, should be no results query = new Query(); query.addEqualityFilter("firstname", firstName); r = em.searchCollection(em.getApplicationRef(), "users", query); assertEquals(0, r.size()); // search with the new username, should be results. query = new Query(); query.addEqualityFilter("firstname", newFirstName); r = em.searchCollection(em.getApplicationRef(), "users", query); assertTrue(r.size() > 0); returned = r.getEntities().get(0); assertEquals(user.getUuid(), returned.getUuid()); }
@Test public void stringWithSpaces() throws Exception { Map<String, Object> props = new HashMap<String, Object>(); props.put("myString", "My simple string"); UUID applicationId = createApplication("testOrganization", "stringWithSpaces"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); Entity saved = em.create("test", props); Query query = new Query(); query.addEqualityFilter("myString", "My simple string"); Results results = em.searchCollection(em.getApplicationRef(), "tests", query); Entity entity = results.getEntitiesMap().get(saved.getUuid()); assertNotNull(entity); }
public Results startingFrom(UUID entityId) { if (entities != null) { for (int i = 0; i < entities.size(); i++) { Entity entity = entities.get(i); if (entityId.equals(entity.getUuid())) { if (i == 0) { return this; } return Results.fromEntities(entities.subList(i, entities.size())); } } } if (refs != null) { for (int i = 0; i < refs.size(); i++) { EntityRef entityRef = refs.get(i); if (entityId.equals(entityRef.getUuid())) { if (i == 0) { return this; } return Results.fromRefList(refs.subList(i, refs.size())); } } } if (ids != null) { for (int i = 0; i < ids.size(); i++) { UUID uuid = ids.get(i); if (entityId.equals(uuid)) { if (i == 0) { return this; } return Results.fromIdList(ids.subList(i, ids.size())); } } } return this; }
@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 runtimeTypeCorrect() throws Exception { UUID applicationId = createApplication("testOrganization", "runtimeTypeCorrect"); assertNotNull(applicationId); EntityManager em = emf.getEntityManager(applicationId); assertNotNull(em); int size = 20; List<User> createdEntities = new ArrayList<User>(); for (int i = 0; i < size; i++) { User user = new User(); user.setEmail(String.format("*****@*****.**", i)); user.setUsername(String.format("test%d", i)); user.setName(String.format("test%d", i)); User created = em.create(user); createdEntities.add(created); } Results r = em.getCollection(em.getApplicationRef(), "users", null, 50, Level.ALL_PROPERTIES, false); logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities())); assertEquals(size, r.size()); // check they're all the same before deletion for (int i = 0; i < size; i++) { assertEquals(createdEntities.get(i).getUuid(), r.getEntities().get(i).getUuid()); assertTrue(r.getEntities().get(i) instanceof User); } }
public Results findForProperty(String propertyName, Object propertyValue, int count) { if (propertyValue == null) { return new Results(); } List<Entity> l = getEntities(); if (l == null) { return new Results(); } List<Entity> found = new ArrayList<Entity>(); for (Entity e : l) { if (propertyValue.equals(e.getProperty(propertyName))) { found.add(e); if ((count > 0) && (found.size() == count)) { break; } } } return Results.fromEntities(found); }
public Results trim(int count) { if (count == 0) { return this; } int size = size(); if (size <= count) { return this; } List<UUID> ids = getIds(); UUID nextResult = null; String cursor = null; if (ids.size() > count) { nextResult = ids.get(count); ids = ids.subList(0, count); if (metadata != null) { cursor = StringUtils.toString(MapUtils.getMapMap(metadata, nextResult, "cursor")); } if (cursor == null) { cursor = encodeBase64URLSafeString(bytes(nextResult)); } } Results r = new Results(this); if (r.entities != null) { r.entities = r.entities.subList(0, count); } if (r.refs != null) { r.refs = r.refs.subList(0, count); } if (r.ids != null) { r.ids = r.ids.subList(0, count); } r.setNextResult(nextResult); r.setCursor(cursor); return r; }