@Override public Results searchCollection( Entity user, String collection, Query query ) throws Exception { return em.searchCollection( user, collection, query ); }
private int countEntities( EntityManager em, String collectionName, int expectedEntities) throws Exception { app.waitForQueueDrainAndRefreshIndex(); Query q = Query.fromQL( "select * where key1=1000" ).withLimit( 1000 ); Results results = em.searchCollectionConsistent( em.getApplicationRef(), collectionName, q, expectedEntities ); int count = 0; while ( true ) { count += results.size(); if ( results.hasCursor() ) { logger.info( "Counted {} : query again with cursor", count ); q.setCursor( results.getCursor() ); results = em.searchCollection( em.getApplicationRef(), collectionName, q ); } else { break; } } assertEquals( "Did not get expected entities", expectedEntities, count ); return count; }
@Override public Map<String, String> getServiceProperties() { Map<String, String> props = new HashMap<String,String>(); EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL("select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting system properties", ex); } if ( results == null || results.isEmpty() ) { return props; } org.apache.usergrid.persistence.Entity e = results.getEntity(); for ( String key : e.getProperties().keySet() ) { props.put( key, props.get(key).toString() ); } return props; }
private int countEntities( EntityManager em, String collectionName, int expectedEntities) throws Exception { app.waitForQueueDrainAndRefreshIndex(); Query q = Query.fromQL( "select * where key1=1000" ).withLimit( 1000 ); Results results = em.searchCollectionConsistent( em.getApplicationRef(), collectionName, q, expectedEntities ); int count = 0; while ( true ) { count += results.size(); if ( results.hasCursor() ) { logger.info( "Counted {} : query again with cursor", count ); q.setCursor( results.getCursor() ); results = em.searchCollection( em.getApplicationRef(), collectionName, q ); } else { break; } } assertEquals( "Did not get expected entities", expectedEntities, count ); return count; }
@Override public Results getImports(final UUID applicationId, @Nullable final String ql, @Nullable final String cursor) { Preconditions.checkNotNull(applicationId, "applicationId must be specified"); try { final EntityManager rootEm = emf.getEntityManager(emf.getManagementAppId()); final Entity appInfo = getApplicationInfoEntity(rootEm, applicationId); Query query = Query.fromQLNullSafe(ql); query.setCursor(cursor); //set our entity type query.setEntityType(Schema.getDefaultSchema().getEntityType(Import.class)); return rootEm.searchCollection(appInfo, APP_IMPORT_CONNECTION, query); } catch (Exception e) { throw new RuntimeException("Unable to get import entity", e); } }
private Results getResultsFor( EntityRef ref ) { entityRef.setUUID(ref.getUuid()); entityRef.setType(ref.getType()); try { return ( query.getCollection() != null ) ? entityManager.searchCollection( entityRef, query.getCollection(), query ) : entityManager.searchTargetEntities(entityRef, query); } catch ( Exception e ) { throw new RuntimeException( e ); } }
@Override public JobStat getStatsForJob( String jobName, UUID jobId ) throws Exception { EntityManager em = emf.getEntityManager( emf.getManagementAppId() ); Query query = Query.fromQL( "select * where " + JOB_NAME + " = '" + jobName + "' AND " + JOB_ID + " = " + jobId ); Results r = em.searchCollection( em.getApplicationRef(), "job_stats", query ); if ( r.size() == 1 ) { return ( JobStat ) r.getEntity(); } return null; }
@Override public Results getResults( Query query ) throws Exception { return app.getEntityManager().searchCollection( app.getEntityManager().getApplicationRef(), "tests", query ); } }
@Override public Results queryJobData( Query query ) throws Exception { if ( query == null ) { query = new Query(); } String jobDataType = Schema.getDefaultSchema().getEntityType(JobData.class); return getEm().searchCollection( getEm().getApplicationRef(), Schema.defaultCollectionName(jobDataType), query ); }
/** * Go around EntityManager and execute query directly against Core Persistence. * Results may include stale index entries. */ private Results queryCollectionEm( final String collName, final String query ) throws Exception { EntityManager em = app.getEntityManager(); final Results results = em.searchCollection( em.getApplicationRef(), collName, Query.fromQL( query ).withLimit( 10000 ) ); return results; } }
@Test public void testCreateAndDelete() throws Exception { logger.info( "EntityDaoTest.testCreateAndDelete" ); EntityManager em = app.getEntityManager(); String name = "test.thing" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", name ); properties.put( "foo", "bar" ); logger.info( "Starting entity create" ); Entity thing = em.create( "thing", properties ); logger.info( "Entity created" ); app.waitForQueueDrainAndRefreshIndex(); logger.info( "Starting entity delete" ); em.delete( thing ); logger.info( "Entity deleted" ); app.waitForQueueDrainAndRefreshIndex(); // now search by username, no results should be returned final Query emailQuery = Query.fromQL( "name = '" + name + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "thing", emailQuery ); assertEquals( 0, r.size() ); }
@Test public void stringWithSpaces() throws Exception { EntityManager em = app.getEntityManager(); assertNotNull( em ); logger.debug( "stringWithSpaces" ); Map<String, Object> props = new HashMap<String, Object>(); props.put( "myString", "My simple string" ); Entity saved = em.create( "test", props ); app.waitForQueueDrainAndRefreshIndex(); Query query = Query.fromQL( "myString = 'My simple string'" ); Results results = em.searchCollection( em.getApplicationRef(), "tests", query ); Entity entity = results.getEntitiesMap().get( saved.getUuid() ); assertNotNull( entity ); }
@Test public void badOrderByBadGrammarAsc() throws Exception { logger.debug( "badOrderByBadGrammarAsc" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String s = "select * where name = 'bob' order by"; String error = null; String entityType = null; String propertyName = null; try { em.searchCollection( em.getApplicationRef(), "users", Query.fromQL( s ) ); fail( "I should throw an exception" ); } catch ( Exception nie ) { error = nie.getMessage(); // entityType = nie.getEntityType(); // propertyName = nie.getPropertyName(); } // assertEquals( "Entity 'user' with property named '' is not indexed. " // + "You cannot use the this field in queries.", error ); // assertEquals( "user", entityType ); // assertEquals( "", propertyName ); }
@Test public void testRedefineTerms() throws Exception { logger.debug( "testRedefineTerms" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); em.create( "user", properties ); app.waitForQueueDrainAndRefreshIndex(); String s = "select {name: username, email: email} where username = 'edanuff'"; Query query = Query.fromQL( s ); Results r = em.searchCollection( em.getApplicationRef(), "users", query ); assertTrue( r.size() == 1 ); // TODO: do we need selection results? // 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( "ed@anuff.com".equals( firstResult.get( "email" ) ) ); }
@Test public void groupTitleSearch() throws Exception { logger.debug( "groupTitleSearch" ); EntityManager em = app.getEntityManager(); 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 ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "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() ); }
@Test public void groupNameSearch() throws Exception { logger.debug( "groupNameSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String groupName = "groupName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "title", "testTitle" ); properties.put( "path", "testPath" ); properties.put( "name", groupName ); Entity group = em.create( "group", properties ); assertNotNull( group ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "name = '" + groupName + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "groups", query ); assertTrue( r.size() > 0 ); Entity returned = r.getEntities().get( 0 ); assertEquals( group.getUuid(), returned.getUuid() ); }
@Test public void userLastNameSearch() throws Exception { logger.debug( "userLastNameSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String lastName = "lastName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); properties.put( "lastname", lastName ); Entity user = em.create( "user", properties ); assertNotNull( user ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "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() ); }
@Test public void userMiddleNameSearch() throws Exception { logger.debug( "userMiddleNameSearch" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String middleName = "middleName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); properties.put( "middlename", middleName ); Entity user = em.create( "user", properties ); assertNotNull( user ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef final Query query = Query.fromQL( "middlename = '" + middleName + "'" ); Results r = em.searchCollection( em.getApplicationRef(), "users", query ); assertTrue( r.size() > 0 ); Entity returned = r.getEntities().get( 0 ); assertEquals( user.getUuid(), returned.getUuid() ); }
private void checkApplicationsOk( String orgName) throws Exception { if (logger.isDebugEnabled()) { logger.debug("\n\nChecking applications OK\n"); } for (int i=0; i<10; i++) { String appName = orgName + "/application" + i; UUID uuid = setup.getEmf().lookupApplication(appName); assertTrue ("Should be able to get application", uuid != null ); EntityManager em = setup.getEmf().getEntityManager( uuid ); Application app = em.getApplication(); assertEquals( appName, app.getName() ); Results results = em.searchCollection( em.getApplicationRef(), "things", Query.fromQL("select *")); assertEquals( "Should have 10 entities", 10, results.size() ); } }
@Override public ServiceResults putItemsByQuery( ServiceContext context, Query query ) throws Exception { checkPermissionsForCollection( context ); if ( context.moreParameters() ) { return getItemsByQuery( context, query ); } query = new Query( query ); query.setResultsLevel( Level.ALL_PROPERTIES ); query.setLimit( 1000 ); if ( !query.isReversedSet() ) { query.setReversed( isCollectionReversed( context ) ); } Results r = em.searchCollection( context.getOwner(), context.getCollectionName(), query ); if ( r.isEmpty() ) { throw new ServiceResourceNotFoundException( context ); } updateEntities( context, r ); return new ServiceResults( this, context, Type.COLLECTION, r, null, null ); }