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.index; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.common.collect.ImmutableSet; 21 : import com.google.gerrit.common.Nullable; 22 : import java.util.Optional; 23 : 24 : /** 25 : * Index types supported by the secondary index. 26 : * 27 : * <p>The explicitly known index types are Lucene (the default) and a fake index used in tests. 28 : * 29 : * <p>The third supported index type is any other type String value, deemed as custom. This is for 30 : * configuring index types that are internal or not to be disclosed. Supporting custom index types 31 : * allows to not break that case upon core implementation changes. 32 : */ 33 : public class IndexType { 34 : public static final String SYS_PROP = "gerrit.index.type"; 35 : private static final String ENV_VAR = "GERRIT_INDEX_TYPE"; 36 : 37 : private static final String LUCENE = "lucene"; 38 : private static final String FAKE = "fake"; 39 : 40 : private final String type; 41 : 42 : /** 43 : * Returns the index type in case it was set by an environment variable. This is useful to run 44 : * tests against a certain index backend. 45 : */ 46 : public static Optional<IndexType> fromEnvironment() { 47 138 : String value = System.getenv(ENV_VAR); 48 138 : if (Strings.isNullOrEmpty(value)) { 49 138 : value = System.getProperty(SYS_PROP); 50 : } 51 138 : if (Strings.isNullOrEmpty(value)) { 52 138 : return Optional.empty(); 53 : } 54 1 : value = value.toUpperCase().replace("-", "_"); 55 1 : IndexType type = new IndexType(value); 56 1 : if (!Strings.isNullOrEmpty(System.getenv(ENV_VAR))) { 57 0 : checkArgument( 58 0 : type != null, "Invalid value for env variable %s: %s", ENV_VAR, System.getenv(ENV_VAR)); 59 : } else { 60 1 : checkArgument( 61 : type != null, 62 : "Invalid value for system property %s: %s", 63 : SYS_PROP, 64 1 : System.getProperty(SYS_PROP)); 65 : } 66 1 : return Optional.of(type); 67 : } 68 : 69 152 : public IndexType(@Nullable String type) { 70 152 : this.type = type == null ? getDefault() : type.toLowerCase(); 71 152 : } 72 : 73 : public static String getDefault() { 74 152 : return LUCENE; 75 : } 76 : 77 : public static ImmutableSet<String> getKnownTypes() { 78 15 : return ImmutableSet.of(LUCENE, FAKE); 79 : } 80 : 81 : public boolean isLucene() { 82 152 : return type.equals(LUCENE); 83 : } 84 : 85 : public boolean isFake() { 86 26 : return type.equals(FAKE); 87 : } 88 : 89 : @Override 90 : public String toString() { 91 151 : return type; 92 : } 93 : }