03-20-2024 12:39 AM - edited 03-26-2024 8:29 PM
Hello Experts,
I have 300k members in b2busergroup. when I try to remove a member from the group using DefaultB2BCommerceB2BUserGroupService.removeMemberFromUserGroup(), I am getting out of memory error. The below code is not written effectively to handle such a huge number of members.
protected void removeMemberFromUserGroup(final B2BUserGroupModel usergroup, final B2BCustomerModel user)
{
final HashSet<PrincipalModel> members = new HashSet<>(usergroup.getMembers());
members.remove(user);
usergroup.setMembers(members);
}
my analysis shows that if the user that I need to remove is in the last positions in the members set, then members.remove(user) is taking a longer time, because internally it queries the user table too many times. For example, if the user to be removed is at the 250000th position then I can see select * from users where PK = ? query getting executed 250,000 times.
Is there a better way to Add/remove members into a user group?
Thanks,
Jobin
I have replaced the out-of-box code with the below code. It is working much better.
/**
*
* usergroup
* user
*/
protected void addMemberToUserGroup(final UserGroupModel usergroup, final CustomerModel user) {
ServicesUtil.validateParameterNotNullStandardMessage(USER, user);
ServicesUtil.validateParameterNotNullStandardMessage(USER_GROUP, usergroup);
try {
if (getPrincipalGroupRelationModel("PrincipalGroupRelation", user, usergroup) == null) {
LinkModel createPrincipalGroupRelationModel = createPrincipalGroupRelationModel(PRINCIPAL_GROUP_RELATION, user, usergroup, null, null);
getModelService().save(createPrincipalGroupRelationModel);
getModelService().refresh(createPrincipalGroupRelationModel);
}
} catch (final Exception ex) {
LOG.error("Error during adding user " + user.getUid() + " to " + usergroup.getUid(), ex);
}
}
/**
*
* usergroup
* user
*/
protected void removeMemberFromUserGroup(final UserGroupModel usergroup, final CustomerModel user) {
ServicesUtil.validateParameterNotNullStandardMessage(USER, user);
ServicesUtil.validateParameterNotNullStandardMessage(USER_GROUP, usergroup);
LinkModel createPrincipalGroupRelationModel = getPrincipalGroupRelationModel("PrincipalGroupRelation", user, usergroup);
if (createPrincipalGroupRelationModel != null) {
getModelService().remove(createPrincipalGroupRelationModel);
}
else {
LOG.error("Error during fetching the PrincipalGroupRelationModel for user " + user.getUid() + " from " + usergroup.getUid());
}
}
/**
* type
* user
* usergroup
* @return
*/
protected LinkModel getPrincipalGroupRelationModel(final String type, final CustomerModel user, final UserGroupModel usergroup) {
LinkModel createPrincipalGroupRelationModel = new LinkModel();
createPrincipalGroupRelationModel.setQualifier(type);
createPrincipalGroupRelationModel.setSource(user);
createPrincipalGroupRelationModel.setTarget(usergroup);
try {
createPrincipalGroupRelationModel = flexibleSearchService.getModelByExample(createPrincipalGroupRelationModel);
} catch (final Exception ex) {
createPrincipalGroupRelationModel = null;
}
return createPrincipalGroupRelationModel;
}
/**
* type
* user
* usergroup
* seq
* rSeq
* @return
*/
protected LinkModel createPrincipalGroupRelationModel(final String type, final CustomerModel user, final UserGroupModel usergroup, final Integer seq,
final Integer rSeq) {
ServicesUtil.validateParameterNotNullStandardMessage("type", type);
ServicesUtil.validateParameterNotNullStandardMessage(USER, user);
ServicesUtil.validateParameterNotNullStandardMessage(USER_GROUP, usergroup);
final LinkModel link = getModelService().create(type);
link.setSource(user);
link.setTarget(usergroup);
if (seq != null) {
link.setSequenceNumber(seq);
}
if (rSeq != null) {
link.setReverseSequenceNumber(rSeq);
}
return link;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.