Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.project; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static com.google.gerrit.server.query.project.ProjectQueryBuilder.FIELD_LIMIT; 19 : 20 : import com.google.gerrit.index.IndexConfig; 21 : import com.google.gerrit.index.project.ProjectData; 22 : import com.google.gerrit.index.project.ProjectIndexCollection; 23 : import com.google.gerrit.index.project.ProjectIndexRewriter; 24 : import com.google.gerrit.index.project.ProjectSchemaDefinitions; 25 : import com.google.gerrit.index.query.AndSource; 26 : import com.google.gerrit.index.query.IndexPredicate; 27 : import com.google.gerrit.index.query.Predicate; 28 : import com.google.gerrit.index.query.QueryProcessor; 29 : import com.google.gerrit.metrics.MetricMaker; 30 : import com.google.gerrit.server.CurrentUser; 31 : import com.google.gerrit.server.account.AccountLimits; 32 : import com.google.gerrit.server.permissions.PermissionBackend; 33 : import com.google.gerrit.server.project.ProjectCache; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Provider; 36 : 37 : /** 38 : * Query processor for the project index. 39 : * 40 : * <p>Instances are one-time-use. Other singleton classes should inject a Provider rather than 41 : * holding on to a single instance. 42 : * 43 : * <p>By default, enforces visibility to CurrentUser. 44 : */ 45 : public class ProjectQueryProcessor extends QueryProcessor<ProjectData> { 46 : private final PermissionBackend permissionBackend; 47 : private final Provider<CurrentUser> userProvider; 48 : private final ProjectCache projectCache; 49 : private final IndexConfig indexConfig; 50 : 51 : static { 52 : // It is assumed that basic rewrites do not touch visibleto predicates. 53 6 : checkState( 54 6 : !ProjectIsVisibleToPredicate.class.isAssignableFrom(IndexPredicate.class), 55 : "ProjectQueryProcessor assumes visibleto is not used by the index rewriter."); 56 6 : } 57 : 58 : @Inject 59 : protected ProjectQueryProcessor( 60 : Provider<CurrentUser> userProvider, 61 : AccountLimits.Factory limitsFactory, 62 : MetricMaker metricMaker, 63 : IndexConfig indexConfig, 64 : ProjectIndexCollection indexes, 65 : ProjectIndexRewriter rewriter, 66 : PermissionBackend permissionBackend, 67 : ProjectCache projectCache) { 68 6 : super( 69 : metricMaker, 70 : ProjectSchemaDefinitions.INSTANCE, 71 : indexConfig, 72 : indexes, 73 : rewriter, 74 : FIELD_LIMIT, 75 6 : () -> limitsFactory.create(userProvider.get()).getQueryLimit()); 76 6 : this.permissionBackend = permissionBackend; 77 6 : this.userProvider = userProvider; 78 6 : this.projectCache = projectCache; 79 6 : this.indexConfig = indexConfig; 80 6 : } 81 : 82 : @Override 83 : protected Predicate<ProjectData> enforceVisibility(Predicate<ProjectData> pred) { 84 6 : return new AndSource<>( 85 : pred, 86 6 : new ProjectIsVisibleToPredicate(permissionBackend, userProvider.get()), 87 : start, 88 : indexConfig); 89 : } 90 : 91 : @Override 92 : protected String formatForLogging(ProjectData projectData) { 93 3 : return projectData.getProject().getName(); 94 : } 95 : 96 : @Override 97 : protected int getIndexSize() { 98 0 : return projectCache.all().size(); 99 : } 100 : 101 : @Override 102 : protected int getBatchSize() { 103 0 : return 1; 104 : } 105 : }