diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/GroupCacheImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/GroupCacheImpl.java index 170688023c..27e23e6ed8 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/account/GroupCacheImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/GroupCacheImpl.java @@ -19,11 +19,10 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.gerrit.common.TimeUtil; import com.google.gerrit.reviewdb.client.AccountGroup; -import com.google.gerrit.reviewdb.client.AccountGroupName; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.cache.CacheModule; +import com.google.gerrit.server.group.Groups; import com.google.gerrit.server.index.group.GroupIndexer; -import com.google.gwtorm.server.OrmDuplicateKeyException; import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.SchemaFactory; import com.google.inject.Inject; @@ -33,7 +32,6 @@ import com.google.inject.Singleton; import com.google.inject.TypeLiteral; import com.google.inject.name.Named; import java.io.IOException; -import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutionException; import org.slf4j.Logger; @@ -72,6 +70,7 @@ public class GroupCacheImpl implements GroupCache { private final LoadingCache> byUUID; private final SchemaFactory schema; private final Provider indexer; + private final Groups groups; @Inject GroupCacheImpl( @@ -79,12 +78,14 @@ public class GroupCacheImpl implements GroupCache { @Named(BYNAME_NAME) LoadingCache> byName, @Named(BYUUID_NAME) LoadingCache> byUUID, SchemaFactory schema, - Provider indexer) { + Provider indexer, + Groups groups) { this.byId = byId; this.byName = byName; this.byUUID = byUUID; this.schema = schema; this.indexer = indexer; + this.groups = groups; } @Override @@ -153,7 +154,7 @@ public class GroupCacheImpl implements GroupCache { @Override public ImmutableList all() { try (ReviewDb db = schema.open()) { - return ImmutableList.copyOf(db.accountGroups().all()); + return groups.getAll(db); } catch (OrmException e) { log.warn("Cannot list internal groups", e); return ImmutableList.of(); @@ -173,62 +174,54 @@ public class GroupCacheImpl implements GroupCache { static class ByIdLoader extends CacheLoader> { private final SchemaFactory schema; + private final Groups groups; @Inject - ByIdLoader(SchemaFactory sf) { + ByIdLoader(SchemaFactory sf, Groups groups) { schema = sf; + this.groups = groups; } @Override public Optional load(AccountGroup.Id key) throws Exception { try (ReviewDb db = schema.open()) { - return Optional.ofNullable(db.accountGroups().get(key)); + return groups.get(db, key); } } } static class ByNameLoader extends CacheLoader> { private final SchemaFactory schema; + private final Groups groups; @Inject - ByNameLoader(SchemaFactory sf) { + ByNameLoader(SchemaFactory sf, Groups groups) { schema = sf; + this.groups = groups; } @Override public Optional load(String name) throws Exception { try (ReviewDb db = schema.open()) { - AccountGroup.NameKey key = new AccountGroup.NameKey(name); - AccountGroupName r = db.accountGroupNames().get(key); - if (r != null) { - return Optional.ofNullable(db.accountGroups().get(r.getId())); - } - return Optional.empty(); + return groups.get(db, new AccountGroup.NameKey(name)); } } } static class ByUUIDLoader extends CacheLoader> { private final SchemaFactory schema; + private final Groups groups; @Inject - ByUUIDLoader(SchemaFactory sf) { + ByUUIDLoader(SchemaFactory sf, Groups groups) { schema = sf; + this.groups = groups; } @Override public Optional load(String uuid) throws Exception { try (ReviewDb db = schema.open()) { - List r; - - r = db.accountGroups().byUUID(new AccountGroup.UUID(uuid)).toList(); - if (r.size() == 1) { - return Optional.of(r.get(0)); - } else if (r.size() == 0) { - return Optional.empty(); - } else { - throw new OrmDuplicateKeyException("Duplicate group UUID " + uuid); - } + return groups.get(db, new AccountGroup.UUID(uuid)); } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/group/Groups.java b/gerrit-server/src/main/java/com/google/gerrit/server/group/Groups.java new file mode 100644 index 0000000000..25ab005c2c --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/group/Groups.java @@ -0,0 +1,60 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.group; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.gerrit.reviewdb.client.AccountGroup; +import com.google.gerrit.reviewdb.client.AccountGroupName; +import com.google.gerrit.reviewdb.server.ReviewDb; +import com.google.gwtorm.server.OrmDuplicateKeyException; +import com.google.gwtorm.server.OrmException; +import com.google.inject.Singleton; +import java.util.List; +import java.util.Optional; + +@Singleton +public class Groups { + + public Optional get(ReviewDb db, AccountGroup.Id groupId) throws OrmException { + return Optional.ofNullable(db.accountGroups().get(groupId)); + } + + public Optional get(ReviewDb db, AccountGroup.UUID groupUuid) throws OrmException { + List accountGroups = db.accountGroups().byUUID(groupUuid).toList(); + if (accountGroups.size() == 1) { + return Optional.of(Iterables.getOnlyElement(accountGroups)); + } else if (accountGroups.isEmpty()) { + return Optional.empty(); + } else { + throw new OrmDuplicateKeyException("Duplicate group UUID " + groupUuid); + } + } + + public Optional get(ReviewDb db, AccountGroup.NameKey groupName) + throws OrmException { + AccountGroupName accountGroupName = db.accountGroupNames().get(groupName); + if (accountGroupName == null) { + return Optional.empty(); + } + + AccountGroup.Id groupId = accountGroupName.getId(); + return Optional.ofNullable(db.accountGroups().get(groupId)); + } + + public ImmutableList getAll(ReviewDb db) throws OrmException { + return ImmutableList.copyOf(db.accountGroups().all()); + } +}