LCOV - code coverage report
Current view: top level - server/index - SingleVersionModule.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 28 31 90.3 %
Date: 2022-11-19 15:00:39 Functions: 7 7 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.server.index;
      16             : 
      17             : import com.google.common.collect.ImmutableSet;
      18             : import com.google.gerrit.extensions.events.LifecycleListener;
      19             : import com.google.gerrit.index.Index;
      20             : import com.google.gerrit.index.IndexDefinition;
      21             : import com.google.gerrit.index.Schema;
      22             : import com.google.gerrit.lifecycle.LifecycleModule;
      23             : import com.google.gerrit.server.config.GerritServerConfig;
      24             : import com.google.inject.Inject;
      25             : import com.google.inject.ProvisionException;
      26             : import com.google.inject.Singleton;
      27             : import com.google.inject.TypeLiteral;
      28             : import com.google.inject.name.Named;
      29             : import com.google.inject.name.Names;
      30             : import com.google.inject.util.Providers;
      31             : import java.util.Collection;
      32             : import java.util.Map;
      33             : import java.util.Set;
      34             : import org.eclipse.jgit.lib.Config;
      35             : 
      36             : /**
      37             :  * Module that installs a listener to Gerrit's lifecycle events to specify which index versions to
      38             :  * use.
      39             :  */
      40             : @Singleton
      41             : public class SingleVersionModule extends LifecycleModule {
      42             :   public static final String SINGLE_VERSIONS = "IndexModule/SingleVersions";
      43             : 
      44             :   private final Map<String, Integer> singleVersions;
      45             : 
      46          32 :   public SingleVersionModule(Map<String, Integer> singleVersions) {
      47          32 :     this.singleVersions = singleVersions;
      48          32 :   }
      49             : 
      50             :   @Override
      51             :   public void configure() {
      52          32 :     listener().to(SingleVersionListener.class);
      53          32 :     bind(new TypeLiteral<Map<String, Integer>>() {})
      54          32 :         .annotatedWith(Names.named(SINGLE_VERSIONS))
      55          32 :         .toProvider(Providers.of(singleVersions));
      56          32 :   }
      57             : 
      58             :   /** Listener to Gerrit's lifecycle events to specify which index versions to use. */
      59             :   @Singleton
      60             :   public static class SingleVersionListener implements LifecycleListener {
      61             :     private final Set<String> disabled;
      62             :     private final Collection<IndexDefinition<?, ?, ?>> defs;
      63             :     private final Map<String, Integer> singleVersions;
      64             : 
      65             :     @Inject
      66             :     SingleVersionListener(
      67             :         @GerritServerConfig Config cfg,
      68             :         Collection<IndexDefinition<?, ?, ?>> defs,
      69          31 :         @Named(SINGLE_VERSIONS) Map<String, Integer> singleVersions) {
      70          31 :       this.defs = defs;
      71          31 :       this.singleVersions = singleVersions;
      72             : 
      73          31 :       disabled = ImmutableSet.copyOf(cfg.getStringList("index", null, "testDisable"));
      74          31 :     }
      75             : 
      76             :     @Override
      77             :     public void start() {
      78          31 :       for (IndexDefinition<?, ?, ?> def : defs) {
      79          31 :         start(def);
      80          31 :       }
      81          31 :     }
      82             : 
      83             :     private <K, V, I extends Index<K, V>> void start(IndexDefinition<K, V, I> def) {
      84          31 :       if (disabled.contains(def.getName())) {
      85           0 :         return;
      86             :       }
      87             :       Schema<V> schema;
      88          31 :       Integer v = singleVersions.get(def.getName());
      89          31 :       if (v == null) {
      90          31 :         schema = def.getLatest();
      91             :       } else {
      92           8 :         schema = def.getSchemas().get(v);
      93           8 :         if (schema == null) {
      94           0 :           throw new ProvisionException(
      95           0 :               String.format("Unrecognized %s schema version: %s", def.getName(), v));
      96             :         }
      97             :       }
      98          31 :       I index = def.getIndexFactory().create(schema);
      99          31 :       def.getIndexCollection().setSearchIndex(index);
     100          31 :       def.getIndexCollection().addWriteIndex(index);
     101          31 :     }
     102             : 
     103             :     @Override
     104             :     public void stop() {
     105             :       // Do nothing; indexes are closed by IndexCollection.
     106          31 :     }
     107             :   }
     108             : }

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