Remove access of groups related db tables from GroupCacheImpl
Groups will be the common class to gather all read access on the database tables related to groups, similarly as Accounts is for accounts. Change-Id: I72e3b575c030678494299cd6b44753a6d511c7f9
This commit is contained in:
@@ -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<String, Optional<AccountGroup>> byUUID;
|
||||
private final SchemaFactory<ReviewDb> schema;
|
||||
private final Provider<GroupIndexer> indexer;
|
||||
private final Groups groups;
|
||||
|
||||
@Inject
|
||||
GroupCacheImpl(
|
||||
@@ -79,12 +78,14 @@ public class GroupCacheImpl implements GroupCache {
|
||||
@Named(BYNAME_NAME) LoadingCache<String, Optional<AccountGroup>> byName,
|
||||
@Named(BYUUID_NAME) LoadingCache<String, Optional<AccountGroup>> byUUID,
|
||||
SchemaFactory<ReviewDb> schema,
|
||||
Provider<GroupIndexer> indexer) {
|
||||
Provider<GroupIndexer> 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<AccountGroup> 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<AccountGroup.Id, Optional<AccountGroup>> {
|
||||
private final SchemaFactory<ReviewDb> schema;
|
||||
private final Groups groups;
|
||||
|
||||
@Inject
|
||||
ByIdLoader(SchemaFactory<ReviewDb> sf) {
|
||||
ByIdLoader(SchemaFactory<ReviewDb> sf, Groups groups) {
|
||||
schema = sf;
|
||||
this.groups = groups;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AccountGroup> 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<String, Optional<AccountGroup>> {
|
||||
private final SchemaFactory<ReviewDb> schema;
|
||||
private final Groups groups;
|
||||
|
||||
@Inject
|
||||
ByNameLoader(SchemaFactory<ReviewDb> sf) {
|
||||
ByNameLoader(SchemaFactory<ReviewDb> sf, Groups groups) {
|
||||
schema = sf;
|
||||
this.groups = groups;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AccountGroup> 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<String, Optional<AccountGroup>> {
|
||||
private final SchemaFactory<ReviewDb> schema;
|
||||
private final Groups groups;
|
||||
|
||||
@Inject
|
||||
ByUUIDLoader(SchemaFactory<ReviewDb> sf) {
|
||||
ByUUIDLoader(SchemaFactory<ReviewDb> sf, Groups groups) {
|
||||
schema = sf;
|
||||
this.groups = groups;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AccountGroup> load(String uuid) throws Exception {
|
||||
try (ReviewDb db = schema.open()) {
|
||||
List<AccountGroup> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<AccountGroup> get(ReviewDb db, AccountGroup.Id groupId) throws OrmException {
|
||||
return Optional.ofNullable(db.accountGroups().get(groupId));
|
||||
}
|
||||
|
||||
public Optional<AccountGroup> get(ReviewDb db, AccountGroup.UUID groupUuid) throws OrmException {
|
||||
List<AccountGroup> 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<AccountGroup> 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<AccountGroup> getAll(ReviewDb db) throws OrmException {
|
||||
return ImmutableList.copyOf(db.accountGroups().all());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user