LCOV - code coverage report
Current view: top level - pgm/init/index - IndexModuleOnInit.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 22 95.5 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          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.pgm.init.index;
      16             : 
      17             : import com.google.common.collect.FluentIterable;
      18             : import com.google.common.collect.ImmutableCollection;
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.common.collect.ImmutableMap;
      21             : import com.google.gerrit.extensions.events.LifecycleListener;
      22             : import com.google.gerrit.index.IndexDefinition;
      23             : import com.google.gerrit.index.SchemaDefinitions;
      24             : import com.google.gerrit.server.account.AccountCache;
      25             : import com.google.gerrit.server.account.GroupCache;
      26             : import com.google.gerrit.server.index.SingleVersionModule;
      27             : import com.google.gerrit.server.index.SingleVersionModule.SingleVersionListener;
      28             : import com.google.gerrit.server.index.account.AccountIndexCollection;
      29             : import com.google.gerrit.server.index.account.AccountIndexDefinition;
      30             : import com.google.gerrit.server.index.account.AccountSchemaDefinitions;
      31             : import com.google.gerrit.server.index.account.AllAccountsIndexer;
      32             : import com.google.gerrit.server.index.group.AllGroupsIndexer;
      33             : import com.google.gerrit.server.index.group.GroupIndexDefinition;
      34             : import com.google.gerrit.server.index.group.GroupSchemaDefinitions;
      35             : import com.google.inject.AbstractModule;
      36             : import com.google.inject.Provides;
      37             : import com.google.inject.ProvisionException;
      38             : import com.google.inject.TypeLiteral;
      39             : import com.google.inject.name.Names;
      40             : import com.google.inject.util.Providers;
      41             : import java.util.Collection;
      42             : import java.util.Map;
      43             : import java.util.Set;
      44             : 
      45          15 : public class IndexModuleOnInit extends AbstractModule {
      46             :   static final String INDEX_MANAGER = "IndexModuleOnInit/IndexManager";
      47             : 
      48          15 :   private static final ImmutableCollection<SchemaDefinitions<?>> ALL_SCHEMA_DEFS =
      49          15 :       ImmutableList.of(AccountSchemaDefinitions.INSTANCE, GroupSchemaDefinitions.INSTANCE);
      50             : 
      51             :   @Override
      52             :   protected void configure() {
      53             :     // The LuceneAccountIndex needs AccountCache only for reading from the index.
      54             :     // On init we only want to write to the index, hence we don't need the
      55             :     // account cache.
      56          15 :     bind(AccountCache.class).toProvider(Providers.of(null));
      57             : 
      58             :     // AccountIndexDefinition wants to have AllAccountsIndexer but it is only
      59             :     // used by the Reindex program and the OnlineReindexer which are both not
      60             :     // used during init, hence we don't need AllAccountsIndexer.
      61          15 :     bind(AllAccountsIndexer.class).toProvider(Providers.of(null));
      62             : 
      63          15 :     bind(AccountIndexCollection.class);
      64             : 
      65             :     // The LuceneGroupIndex needs GroupCache only for reading from the index. On init we only want
      66             :     // to
      67             :     // write to the index, hence we don't need the group cache.
      68          15 :     bind(GroupCache.class).toProvider(Providers.of(null));
      69             : 
      70             :     // GroupIndexDefinition wants to have AllGroupsIndexer but it is only used
      71             :     // by the Reindex program and the OnlineReindexer which are both not used
      72             :     // during init, hence we don't need AllGroupsIndexer.
      73          15 :     bind(AllGroupsIndexer.class).toProvider(Providers.of(null));
      74             : 
      75          15 :     bind(new TypeLiteral<Map<String, Integer>>() {})
      76          15 :         .annotatedWith(Names.named(SingleVersionModule.SINGLE_VERSIONS))
      77          15 :         .toInstance(ImmutableMap.of());
      78          15 :     bind(LifecycleListener.class)
      79          15 :         .annotatedWith(Names.named(INDEX_MANAGER))
      80          15 :         .to(SingleVersionListener.class);
      81          15 :   }
      82             : 
      83             :   @Provides
      84             :   Collection<IndexDefinition<?, ?, ?>> getIndexDefinitions(
      85             :       AccountIndexDefinition accounts, GroupIndexDefinition groups) {
      86          15 :     Collection<IndexDefinition<?, ?, ?>> result = ImmutableList.of(accounts, groups);
      87          15 :     Set<String> expected =
      88          15 :         FluentIterable.from(ALL_SCHEMA_DEFS).transform(SchemaDefinitions::getName).toSet();
      89          15 :     Set<String> actual = FluentIterable.from(result).transform(IndexDefinition::getName).toSet();
      90          15 :     if (!expected.equals(actual)) {
      91           0 :       throw new ProvisionException(
      92             :           "need index definitions for all schemas: " + expected + " != " + actual);
      93             :     }
      94          15 :     return result;
      95             :   }
      96             : }

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