Line data Source code
1 : // Copyright (C) 2016 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.primitives.Ints; 18 : import com.google.gerrit.server.config.SitePaths; 19 : import com.google.gerrit.server.index.change.ChangeSchemaDefinitions; 20 : import java.io.IOException; 21 : import org.eclipse.jgit.errors.ConfigInvalidException; 22 : import org.eclipse.jgit.storage.file.FileBasedConfig; 23 : import org.eclipse.jgit.util.FS; 24 : 25 : /** 26 : * Status to decide if a specific index version (e.g. change v55) is initialized and ready for use. 27 : * An index version is ready for use after documents for all entities were created. 28 : */ 29 : public class GerritIndexStatus { 30 : private static final String SECTION = "index"; 31 : private static final String KEY_READY = "ready"; 32 : 33 : private final FileBasedConfig cfg; 34 : 35 138 : public GerritIndexStatus(SitePaths sitePaths) throws ConfigInvalidException, IOException { 36 138 : cfg = 37 : new FileBasedConfig( 38 138 : sitePaths.index_dir.resolve("gerrit_index.config").toFile(), FS.detect()); 39 138 : cfg.load(); 40 138 : convertLegacyConfig(); 41 138 : } 42 : 43 : public void setReady(String indexName, int version, boolean ready) { 44 138 : cfg.setBoolean(SECTION, indexDirName(indexName, version), KEY_READY, ready); 45 138 : } 46 : 47 : public boolean getReady(String indexName, int version) { 48 138 : return cfg.getBoolean(SECTION, indexDirName(indexName, version), KEY_READY, false); 49 : } 50 : 51 : public boolean exists(String indexName) { 52 15 : return cfg.getSubsections(SECTION).stream().anyMatch(n -> n.startsWith(indexName)); 53 : } 54 : 55 : public void save() throws IOException { 56 138 : cfg.save(); 57 138 : } 58 : 59 : private void convertLegacyConfig() throws IOException { 60 138 : boolean dirty = false; 61 : // Convert legacy [index "25"] to modern [index "changes_0025"]. 62 138 : for (String subsection : cfg.getSubsections(SECTION)) { 63 138 : Integer v = Ints.tryParse(subsection); 64 138 : if (v != null) { 65 0 : String ready = cfg.getString(SECTION, subsection, KEY_READY); 66 0 : if (ready != null) { 67 0 : dirty = false; 68 0 : cfg.unset(SECTION, subsection, KEY_READY); 69 0 : cfg.setString(SECTION, indexDirName(ChangeSchemaDefinitions.NAME, v), KEY_READY, ready); 70 : } 71 : } 72 138 : } 73 138 : if (dirty) { 74 0 : cfg.save(); 75 : } 76 138 : } 77 : 78 : private static String indexDirName(String indexName, int version) { 79 138 : return String.format("%s_%04d", indexName, version); 80 : } 81 : }