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.index; 16 : 17 : import com.google.gerrit.index.IndexConfig; 18 : import com.google.gerrit.index.project.ProjectIndex; 19 : import com.google.gerrit.lifecycle.LifecycleModule; 20 : import com.google.gerrit.server.config.GerritServerConfig; 21 : import com.google.gerrit.server.index.account.AccountIndex; 22 : import com.google.gerrit.server.index.change.ChangeIndex; 23 : import com.google.gerrit.server.index.group.GroupIndex; 24 : import com.google.inject.AbstractModule; 25 : import com.google.inject.Provides; 26 : import com.google.inject.Singleton; 27 : import com.google.inject.assistedinject.FactoryModuleBuilder; 28 : import java.util.Map; 29 : import org.eclipse.jgit.lib.Config; 30 : 31 : /** 32 : * Base class to establish implementation-independent index bindings. To be subclassed by concrete 33 : * index implementations, such as {@link com.google.gerrit.lucene.LuceneIndexModule}. 34 : */ 35 : public abstract class AbstractIndexModule extends AbstractModule { 36 : public static final String INDEX_MODULE = "index-module"; 37 : 38 : private final int threads; 39 : private final Map<String, Integer> singleVersions; 40 : private final boolean slave; 41 : 42 152 : protected AbstractIndexModule(Map<String, Integer> singleVersions, int threads, boolean slave) { 43 152 : this.singleVersions = singleVersions; 44 152 : this.threads = threads; 45 152 : this.slave = slave; 46 152 : } 47 : 48 : @Override 49 : protected void configure() { 50 152 : if (slave) { 51 1 : bind(AccountIndex.Factory.class) 52 1 : .toInstance( 53 : s -> { 54 0 : throw new UnsupportedOperationException(); 55 : }); 56 1 : bind(ChangeIndex.Factory.class) 57 1 : .toInstance( 58 : s -> { 59 0 : throw new UnsupportedOperationException(); 60 : }); 61 1 : bind(ProjectIndex.Factory.class) 62 1 : .toInstance( 63 : s -> { 64 0 : throw new UnsupportedOperationException(); 65 : }); 66 : } else { 67 152 : install( 68 : new FactoryModuleBuilder() 69 152 : .implement(AccountIndex.class, getAccountIndex()) 70 152 : .build(AccountIndex.Factory.class)); 71 152 : install( 72 : new FactoryModuleBuilder() 73 152 : .implement(ChangeIndex.class, getChangeIndex()) 74 152 : .build(ChangeIndex.Factory.class)); 75 152 : install( 76 : new FactoryModuleBuilder() 77 152 : .implement(ProjectIndex.class, getProjectIndex()) 78 152 : .build(ProjectIndex.Factory.class)); 79 : } 80 152 : install( 81 : new FactoryModuleBuilder() 82 152 : .implement(GroupIndex.class, getGroupIndex()) 83 152 : .build(GroupIndex.Factory.class)); 84 : 85 152 : install(new IndexModule(threads, slave)); 86 152 : if (singleVersions == null) { 87 138 : install(new MultiVersionModule()); 88 : } else { 89 32 : install(new SingleVersionModule(singleVersions)); 90 : } 91 152 : } 92 : 93 : protected abstract Class<? extends AccountIndex> getAccountIndex(); 94 : 95 : protected abstract Class<? extends ChangeIndex> getChangeIndex(); 96 : 97 : protected abstract Class<? extends GroupIndex> getGroupIndex(); 98 : 99 : protected abstract Class<? extends ProjectIndex> getProjectIndex(); 100 : 101 : protected abstract Class<? extends VersionManager> getVersionManager(); 102 : 103 : @Provides 104 : @Singleton 105 : IndexConfig provideIndexConfig(@GerritServerConfig Config cfg) { 106 151 : return getIndexConfig(cfg); 107 : } 108 : 109 : protected IndexConfig getIndexConfig(@GerritServerConfig Config cfg) { 110 151 : return IndexConfig.fromConfig(cfg).separateChangeSubIndexes(true).build(); 111 : } 112 : 113 138 : private class MultiVersionModule extends LifecycleModule { 114 : @Override 115 : public void configure() { 116 138 : Class<? extends VersionManager> versionManagerClass = getVersionManager(); 117 138 : bind(VersionManager.class).to(versionManagerClass); 118 138 : listener().to(versionManagerClass); 119 138 : } 120 : } 121 : }