From 126c225b6584c414447317521ff0e0231055ac65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20=C5=BDivkov?= Date: Wed, 21 Jan 2015 17:22:27 +0100 Subject: [PATCH] Reduce number of LDAP queries when having multiple accountBases When searching for an account in LDAP we used to first execute one query for each account base and, after that, check if the account was found. For an LDAP configuration with N accountBases this always executed N LDAP queries. In most cases this was not necessary as the account was often found in the first configured accountBase. Check if the account is found after each query and return as soon as it is found. When most users are found in the first configured accountBase this should reduce the number of LDAP queries by a factor of N. Change-Id: I6eced365506ac9a2716cef643b5760b68fc3966d --- .../google/gerrit/server/auth/ldap/Helper.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/auth/ldap/Helper.java b/gerrit-server/src/main/java/com/google/gerrit/server/auth/ldap/Helper.java index b5dfd0b1f2..0698203008 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/auth/ldap/Helper.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/auth/ldap/Helper.java @@ -191,21 +191,15 @@ import javax.security.auth.login.LoginException; final HashMap params = new HashMap<>(); params.put(LdapRealm.USERNAME, username); - final List res = new ArrayList<>(); for (LdapQuery accountQuery : schema.accountQueryList) { - res.addAll(accountQuery.query(ctx, params)); - } - - switch (res.size()) { - case 0: - throw new NoSuchUserException(username); - - case 1: + List res = accountQuery.query(ctx, params); + if (res.size() == 1) { return res.get(0); - - default: + } else if (res.size() > 1) { throw new AccountException("Duplicate users: " + username); + } } + throw new NoSuchUserException(username); } Set queryForGroups(final DirContext ctx,