Fixed n+1 query retrieving user with brief user representation by allowing explicit eager caching of user attributese

This commit is contained in:
Sebastian Schuster 2022-06-23 19:19:00 +02:00 committed by Hynek Mlnařík
parent e3af0610e2
commit 1445646e77
2 changed files with 19 additions and 10 deletions

View file

@ -204,7 +204,7 @@ public class UserAdapter implements CachedUserModel.Streams {
@Override @Override
public String getFirstAttribute(String name) { public String getFirstAttribute(String name) {
if (updated != null) return updated.getFirstAttribute(name); if (updated != null) return updated.getFirstAttribute(name);
return cached.getAttributes(modelSupplier).getFirst(name); return cached.getFirstAttribute(name, modelSupplier);
} }
@Override @Override

View file

@ -41,16 +41,15 @@ import java.util.stream.Collectors;
public class CachedUser extends AbstractExtendableRevisioned implements InRealm { public class CachedUser extends AbstractExtendableRevisioned implements InRealm {
private final String realm; private final String realm;
private final String username;
private final Long createdTimestamp; private final Long createdTimestamp;
private final String email;
private final boolean emailVerified; private final boolean emailVerified;
private final boolean enabled; private final boolean enabled;
private final String federationLink; private final String federationLink;
private final String serviceAccountClientLink; private final String serviceAccountClientLink;
private final int notBefore; private final int notBefore;
private final LazyLoader<UserModel, Set<String>> requiredActions; private final LazyLoader<UserModel, Set<String>> requiredActions;
private final LazyLoader<UserModel, MultivaluedHashMap<String, String>> attributes; private final LazyLoader<UserModel, MultivaluedHashMap<String, String>> lazyLoadedAttributes;
private final MultivaluedHashMap<String,String> eagerLoadedAttributes;
private final LazyLoader<UserModel, Set<String>> roleMappings; private final LazyLoader<UserModel, Set<String>> roleMappings;
private final LazyLoader<UserModel, Set<String>> groups; private final LazyLoader<UserModel, Set<String>> groups;
private final LazyLoader<UserModel, List<CredentialModel>> storedCredentials; private final LazyLoader<UserModel, List<CredentialModel>> storedCredentials;
@ -58,16 +57,19 @@ public class CachedUser extends AbstractExtendableRevisioned implements InRealm
public CachedUser(Long revision, RealmModel realm, UserModel user, int notBefore) { public CachedUser(Long revision, RealmModel realm, UserModel user, int notBefore) {
super(revision, user.getId()); super(revision, user.getId());
this.realm = realm.getId(); this.realm = realm.getId();
this.username = user.getUsername();
this.createdTimestamp = user.getCreatedTimestamp(); this.createdTimestamp = user.getCreatedTimestamp();
this.email = user.getEmail();
this.emailVerified = user.isEmailVerified(); this.emailVerified = user.isEmailVerified();
this.enabled = user.isEnabled(); this.enabled = user.isEnabled();
this.federationLink = user.getFederationLink(); this.federationLink = user.getFederationLink();
this.serviceAccountClientLink = user.getServiceAccountClientLink(); this.serviceAccountClientLink = user.getServiceAccountClientLink();
this.notBefore = notBefore; this.notBefore = notBefore;
this.eagerLoadedAttributes = new MultivaluedHashMap<>();
this.eagerLoadedAttributes.putSingle(UserModel.USERNAME,user.getUsername());
this.eagerLoadedAttributes.putSingle(UserModel.FIRST_NAME,user.getFirstName());
this.eagerLoadedAttributes.putSingle(UserModel.LAST_NAME,user.getLastName());
this.eagerLoadedAttributes.putSingle(UserModel.EMAIL,user.getEmail());
this.lazyLoadedAttributes = new DefaultLazyLoader<>(userModel -> new MultivaluedHashMap<>(userModel.getAttributes()), MultivaluedHashMap::new);
this.requiredActions = new DefaultLazyLoader<>(userModel -> userModel.getRequiredActionsStream().collect(Collectors.toSet()), Collections::emptySet); this.requiredActions = new DefaultLazyLoader<>(userModel -> userModel.getRequiredActionsStream().collect(Collectors.toSet()), Collections::emptySet);
this.attributes = new DefaultLazyLoader<>(userModel -> new MultivaluedHashMap<>(userModel.getAttributes()), MultivaluedHashMap::new);
this.roleMappings = new DefaultLazyLoader<>(userModel -> userModel.getRoleMappingsStream().map(RoleModel::getId).collect(Collectors.toSet()), Collections::emptySet); this.roleMappings = new DefaultLazyLoader<>(userModel -> userModel.getRoleMappingsStream().map(RoleModel::getId).collect(Collectors.toSet()), Collections::emptySet);
this.groups = new DefaultLazyLoader<>(userModel -> userModel.getGroupsStream().map(GroupModel::getId).collect(Collectors.toCollection(LinkedHashSet::new)), LinkedHashSet::new); this.groups = new DefaultLazyLoader<>(userModel -> userModel.getGroupsStream().map(GroupModel::getId).collect(Collectors.toCollection(LinkedHashSet::new)), LinkedHashSet::new);
this.storedCredentials = new DefaultLazyLoader<>(userModel -> userModel.credentialManager().getStoredCredentialsStream().collect(Collectors.toCollection(LinkedList::new)), LinkedList::new); this.storedCredentials = new DefaultLazyLoader<>(userModel -> userModel.credentialManager().getStoredCredentialsStream().collect(Collectors.toCollection(LinkedList::new)), LinkedList::new);
@ -78,7 +80,14 @@ public class CachedUser extends AbstractExtendableRevisioned implements InRealm
} }
public String getUsername() { public String getUsername() {
return username; return eagerLoadedAttributes.getFirst(UserModel.USERNAME);
}
public String getFirstAttribute(String name, Supplier<UserModel> userModel) {
if(eagerLoadedAttributes.containsKey(name))
return eagerLoadedAttributes.getFirst(name);
else
return this.lazyLoadedAttributes.get(userModel).getFirst(name);
} }
public Long getCreatedTimestamp() { public Long getCreatedTimestamp() {
@ -86,7 +95,7 @@ public class CachedUser extends AbstractExtendableRevisioned implements InRealm
} }
public String getEmail() { public String getEmail() {
return email; return eagerLoadedAttributes.getFirst(UserModel.EMAIL);
} }
public boolean isEmailVerified() { public boolean isEmailVerified() {
@ -98,7 +107,7 @@ public class CachedUser extends AbstractExtendableRevisioned implements InRealm
} }
public MultivaluedHashMap<String, String> getAttributes(Supplier<UserModel> userModel) { public MultivaluedHashMap<String, String> getAttributes(Supplier<UserModel> userModel) {
return attributes.get(userModel); return lazyLoadedAttributes.get(userModel);
} }
public Set<String> getRequiredActions(Supplier<UserModel> userModel) { public Set<String> getRequiredActions(Supplier<UserModel> userModel) {