LCOV - code coverage report
Current view: top level - pgm/init - InitIndex.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 23 25 92.0 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2013 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.pgm.init;
      16             : 
      17             : import com.google.common.collect.Iterables;
      18             : import com.google.gerrit.index.IndexType;
      19             : import com.google.gerrit.index.SchemaDefinitions;
      20             : import com.google.gerrit.pgm.init.api.ConsoleUI;
      21             : import com.google.gerrit.pgm.init.api.InitFlags;
      22             : import com.google.gerrit.pgm.init.api.InitStep;
      23             : import com.google.gerrit.pgm.init.api.Section;
      24             : import com.google.gerrit.server.config.SitePaths;
      25             : import com.google.gerrit.server.index.IndexModule;
      26             : import com.google.gerrit.server.index.IndexUtils;
      27             : import com.google.inject.Inject;
      28             : import com.google.inject.Singleton;
      29             : import java.io.IOException;
      30             : import java.nio.file.DirectoryStream;
      31             : import java.nio.file.Files;
      32             : import java.nio.file.Path;
      33             : 
      34             : /** Initialize the {@code index} configuration section. */
      35             : @Singleton
      36             : class InitIndex implements InitStep {
      37             :   private final ConsoleUI ui;
      38             :   private final Section index;
      39             :   private final SitePaths site;
      40             :   private final InitFlags initFlags;
      41             :   private final Section gerrit;
      42             : 
      43             :   @Inject
      44          15 :   InitIndex(ConsoleUI ui, Section.Factory sections, SitePaths site, InitFlags initFlags) {
      45          15 :     this.ui = ui;
      46          15 :     this.index = sections.get("index", null);
      47          15 :     this.gerrit = sections.get("gerrit", null);
      48          15 :     this.site = site;
      49          15 :     this.initFlags = initFlags;
      50          15 :   }
      51             : 
      52             :   @Override
      53             :   public void run() throws IOException {
      54          15 :     ui.header("Index");
      55          15 :     IndexType type =
      56             :         new IndexType(
      57          15 :             index.select("Type", "type", IndexType.getDefault(), IndexType.getKnownTypes()));
      58             : 
      59          15 :     if ((site.isNew || isEmptySite()) && type.isLucene()) {
      60           1 :       for (SchemaDefinitions<?> def : IndexModule.ALL_SCHEMA_DEFS) {
      61           1 :         IndexUtils.setReady(site, def.getName(), def.getLatest().getVersion(), true);
      62           1 :       }
      63             :     } else {
      64          15 :       String message =
      65          15 :           String.format(
      66             :               "\nThe index must be %sbuilt before starting Gerrit:\n"
      67             :                   + "  java -jar gerrit.war reindex -d site_path\n",
      68          15 :               site.isNew ? "" : "re");
      69          15 :       ui.message(message);
      70          15 :       initFlags.autoStart = false;
      71             :     }
      72          15 :   }
      73             : 
      74             :   private boolean isEmptySite() {
      75          15 :     try (DirectoryStream<Path> files =
      76          15 :         Files.newDirectoryStream(site.resolve(gerrit.get("basePath")))) {
      77          15 :       return Iterables.isEmpty(files);
      78           0 :     } catch (IOException e) {
      79           0 :       return true;
      80             :     }
      81             :   }
      82             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750