Line data Source code
1 : // Copyright (C) 2018 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.restapi.project; 16 : 17 : import static com.google.gerrit.server.git.QueueProvider.QueueType.BATCH; 18 : 19 : import com.google.common.util.concurrent.ListeningExecutorService; 20 : import com.google.gerrit.common.data.GlobalCapability; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.extensions.annotations.RequiresCapability; 23 : import com.google.gerrit.extensions.api.projects.IndexProjectInput; 24 : import com.google.gerrit.extensions.common.ProjectInfo; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestModifyView; 27 : import com.google.gerrit.index.project.ProjectIndexer; 28 : import com.google.gerrit.server.index.IndexExecutor; 29 : import com.google.gerrit.server.project.ProjectResource; 30 : import com.google.inject.Inject; 31 : import com.google.inject.Provider; 32 : import com.google.inject.Singleton; 33 : import java.util.concurrent.Future; 34 : 35 : @RequiresCapability(GlobalCapability.MAINTAIN_SERVER) 36 : @Singleton 37 : public class Index implements RestModifyView<ProjectResource, IndexProjectInput> { 38 : private final ProjectIndexer indexer; 39 : private final ListeningExecutorService executor; 40 : private final Provider<ListChildProjects> listChildProjectsProvider; 41 : 42 : @Inject 43 : Index( 44 : ProjectIndexer indexer, 45 : @IndexExecutor(BATCH) ListeningExecutorService executor, 46 146 : Provider<ListChildProjects> listChildProjectsProvider) { 47 146 : this.indexer = indexer; 48 146 : this.executor = executor; 49 146 : this.listChildProjectsProvider = listChildProjectsProvider; 50 146 : } 51 : 52 : @Override 53 : public Response.Accepted apply(ProjectResource rsrc, IndexProjectInput input) throws Exception { 54 2 : String response = "Project " + rsrc.getName() + " submitted for reindexing"; 55 : 56 2 : reindex(rsrc.getNameKey(), input.async); 57 2 : if (Boolean.TRUE.equals(input.indexChildren)) { 58 : for (ProjectInfo child : 59 1 : listChildProjectsProvider.get().withRecursive(true).apply(rsrc).value()) { 60 1 : reindex(Project.nameKey(child.name), input.async); 61 1 : } 62 : 63 1 : response += " (indexing children recursively)"; 64 : } 65 2 : return Response.accepted(response); 66 : } 67 : 68 : private void reindex(Project.NameKey project, Boolean async) { 69 2 : if (Boolean.TRUE.equals(async)) { 70 : @SuppressWarnings("unused") 71 0 : Future<?> possiblyIgnoredError = executor.submit(() -> indexer.index(project)); 72 0 : } else { 73 2 : indexer.index(project); 74 : } 75 2 : } 76 : }