Getting a group membership count safely

I would like to get a count of the total members for a user group. I see plenty of ways to get the global user count in the Confluence API, but nothing similar to Jira’s GroupManager#getUsersInGroupCount(com.atlassian.crowd.embedded.api.Group group).

I am wary of getting the entire member set due to the potential for a very large group resulting in a big memory hit. Raw SQL is an obvious conclusion but a minefield in terms of syntax and maintenance. Any thoughts?

You are right, there is no equivalent method in Confluence. You may try this, but I am not sure, how this handles very large groups.

import com.atlassian.confluence.user.UserAccessor;
import org.apache.commons.collections4.IterableUtils;


final Iterable<ConfluenceUser> members = userAccessor.getMembers(group);

I would also NOT recommend to use Raw SQL for this task.

Thanks for your input. I had considered this approach but this seems quite memory-intensive considering that I only need the total count. Something in the API which does a COUNT SQL query under the hood sounds better to me…

The underlying problem is, that group memberships are not only stored in one Confluence database table (e.g. if you have an eternal user directory like LDAP or Crowd). So one SQL-query (if you find one) might not return all group members or has to be dependent on the user directory.

I found an existing improvement issue that suggests one possible path using Crowd’s API: That sounds like what is needed - the API should delegate meaning that we don’t need to know what the underlying directory implementation is.

Open for other suggestions that work in the present though!