Repository: usergrid Updated Branches: refs/heads/userCollectionFix a72da10f8 -> b44e537d5
Added fix where when not returning all entities would prematurely exit the program Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/b44e537d Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/b44e537d Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/b44e537d Branch: refs/heads/userCollectionFix Commit: b44e537d5ceca642ffc3049f982e1fa41ceb374c Parents: a72da10 Author: George Reyes <[email protected]> Authored: Mon Nov 2 08:42:05 2015 -0800 Committer: George Reyes <[email protected]> Committed: Mon Nov 2 08:42:05 2015 -0800 ---------------------------------------------------------------------- .../usergrid/tools/CollectionUserFix.java | 131 ++++++++++--------- 1 file changed, 70 insertions(+), 61 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/b44e537d/stack/tools/src/main/java/org/apache/usergrid/tools/CollectionUserFix.java ---------------------------------------------------------------------- diff --git a/stack/tools/src/main/java/org/apache/usergrid/tools/CollectionUserFix.java b/stack/tools/src/main/java/org/apache/usergrid/tools/CollectionUserFix.java index 829487a..2e66858 100644 --- a/stack/tools/src/main/java/org/apache/usergrid/tools/CollectionUserFix.java +++ b/stack/tools/src/main/java/org/apache/usergrid/tools/CollectionUserFix.java @@ -55,7 +55,7 @@ import com.google.common.collect.Multimap; * user shows up there. If you see that then run this tool. */ public class CollectionUserFix extends ExportingToolBase { - private static final int PAGE_SIZE = 100; + private static final int PAGE_SIZE = 1000; private static final Logger logger = LoggerFactory.getLogger( CollectionUserFix.class ); @@ -159,12 +159,12 @@ public class CollectionUserFix extends ExportingToolBase { query.setLimit( PAGE_SIZE ); Results r = null; EntityManager em = null; + int numberOfUsers = 0; for ( Application application : app ) { //This will hold all of the applications users. This will be stored in memory to do a simple check to see if //there are any duped usernames in the collection. //Memory concerns means that - Multimap<String, UUID> usernames = HashMultimap.create(); //This means that we need to set it for each and every single application thus it gets set here instead of //the method that calls us. @@ -176,10 +176,12 @@ public class CollectionUserFix extends ExportingToolBase { } do { + Multimap<String, UUID> usernames = HashMultimap.create(); //get all users in the management app and page for each set of a PAGE_SIZE r = em.searchCollection( application, "users", query ); - System.out.println("found "+r.size()+" number of entities"); + numberOfUsers+=r.size(); + System.out.println("found "+numberOfUsers+" users"); for ( Entity entity : r.getEntities() ) { //grab all usernames returned. @@ -188,68 +190,75 @@ public class CollectionUserFix extends ExportingToolBase { query.setCursor( r.getCursor() ); - logger.info( "Searching next page" ); - System.out.println("Searching next page"); - } - while ( r != null && r.size() == PAGE_SIZE ); + System.out.println("Starting username crawl of "+usernames.size()+" number of usernames"); + usernameVerificationFix( em, usernames ); + - System.out.println("Starting username crawl of "+usernames.size()+" number of usernames"); + } + while ( r != null && r.size() >0); + System.out.println("Repair Complete"); //do a get on a specific username, if it shows up more than once then remove it - for ( String username : usernames.keySet() ) { - Collection<UUID> ids = usernames.get( username ); + } + } - if ( ids.size() > 1 ) { - logger.info( "Found multiple users with the username {}", username ); - System.out.println( "Found multiple users with the username: " + username ); - } - //UserInfo targetUser = managementService.getAdminUserByEmail( email ); - Identifier identifier = new Identifier(); - EntityRef targetUser = em.getUserByIdentifier( identifier.fromName( username ) ); - - - if ( targetUser == null ) { - //This means that the username isn't properly associated with targetUser - List<UUID> tempIds = new ArrayList<UUID>( ids ); - //Collections.sort( tempIds ); - - UUID toLoad = tempIds.get( 0 ); - - logger.warn( "Could not load target user by username {}, loading by UUID {} instead", username, toLoad ); - System.out.println( "Could not load the target user by username: " + username - + ". Loading by the following uuid instead: " + toLoad.toString() ); - - User targetUserEntity = null; - try { - targetUserEntity = em.get( toLoad, User.class ); - }catch(Exception e){ - System.out.println("The follow uuid has no data in this cassandra node: "+toLoad.toString()); - throw e; - } - - - try { - if ( targetUserEntity != null&& targetUserEntity.getUuid().equals( toLoad )) { - System.out.println("Updating uuid: "+targetUserEntity.getUuid().toString()); - em.update( targetUserEntity ); - } - } - catch ( DuplicateUniquePropertyExistsException dup ) { - System.out.println( "Found duplicate unique property: " + dup.getPropertyName() + ". " - + "Duplicate property is: " - + dup.getPropertyValue() ); - //if there are duplicate unique properties then - if ( dup.getPropertyName().equals( "username" ) ) { - System.out.println("can I replace this with a different value since these are duplicated in the code base"); - //targetUserEntity.setUsername( targetUserEntity.getUsername() ); - } - //else throw dup; - } - catch (Exception e){ - System.out.println("There was an issue with updating: "+e.getMessage()); - } - } - } + private void usernameVerificationFix( final EntityManager em, final Multimap<String, UUID> usernames ) + throws Exception { + Identifier identifier = new Identifier(); + for ( String username : usernames.keySet() ) { + // Collection<UUID> ids = usernames.get( username ); + +// if ( ids.size() > 1 ) { +// logger.info( "Found multiple users with the username {}", username ); +// System.out.println( "Found multiple users with the username: " + username ); +// } + + //UserInfo targetUser = managementService.getAdminUserByEmail( email ); + em.getUserByIdentifier( identifier.fromName( username ) ); + + + +// if ( targetUser == null ) { +// //This means that the username isn't properly associated with targetUser +// List<UUID> tempIds = new ArrayList<UUID>( ids ); +// //Collections.sort( tempIds ); +// +// UUID toLoad = tempIds.get( 0 ); +// +// logger.warn( "Could not load target user by username {}, loading by UUID {} instead", username, toLoad ); +// System.out.println( "Could not load the target user by username: " + username +// + ". Loading by the following uuid instead: " + toLoad.toString() ); +// +// User targetUserEntity = null; +// try { +// targetUserEntity = em.get( toLoad, User.class ); +// }catch(Exception e){ +// System.out.println("The follow uuid has no data in this cassandra node: "+toLoad.toString()); +// throw e; +// } +// +// +// try { +// if ( targetUserEntity != null&& targetUserEntity.getUuid().equals( toLoad )) { +// System.out.println("Updating uuid: "+targetUserEntity.getUuid().toString()); +// em.update( targetUserEntity ); +// } +// } +// catch ( DuplicateUniquePropertyExistsException dup ) { +// System.out.println( "Found duplicate unique property: " + dup.getPropertyName() + ". " +// + "Duplicate property is: " +// + dup.getPropertyValue() ); +// //if there are duplicate unique properties then +// if ( dup.getPropertyName().equals( "username" ) ) { +// System.out.println("can I replace this with a different value since these are duplicated in the code base"); +// //targetUserEntity.setUsername( targetUserEntity.getUsername() ); +// } +// //else throw dup; +// } +// catch (Exception e){ +// System.out.println("There was an issue with updating: "+e.getMessage()); +// } +// } } } }
