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 static com.google.gerrit.server.index.change.ChangeField.CHANGE; 18 : import static com.google.gerrit.server.index.change.ChangeField.LEGACY_ID_STR; 19 : import static com.google.gerrit.server.index.change.ChangeField.PROJECT_SPEC; 20 : 21 : import com.google.common.collect.ImmutableSet; 22 : import com.google.common.collect.Sets; 23 : import com.google.gerrit.exceptions.StorageException; 24 : import com.google.gerrit.index.QueryOptions; 25 : import com.google.gerrit.index.project.ProjectField; 26 : import com.google.gerrit.server.CurrentUser; 27 : import com.google.gerrit.server.config.SitePaths; 28 : import com.google.gerrit.server.index.account.AccountField; 29 : import com.google.gerrit.server.index.group.GroupField; 30 : import com.google.gerrit.server.query.change.GroupBackedUser; 31 : import java.io.IOException; 32 : import java.util.Set; 33 : import org.eclipse.jgit.errors.ConfigInvalidException; 34 : 35 : /** Set of index-related utility methods. */ 36 : public final class IndexUtils { 37 : 38 : /** Mark an index version as ready to serve queries. */ 39 : public static void setReady(SitePaths sitePaths, String name, int version, boolean ready) { 40 : try { 41 16 : GerritIndexStatus cfg = new GerritIndexStatus(sitePaths); 42 16 : cfg.setReady(name, version, ready); 43 16 : cfg.save(); 44 0 : } catch (ConfigInvalidException | IOException e) { 45 0 : throw new StorageException(e); 46 16 : } 47 16 : } 48 : 49 : /** 50 : * Returns a sanitized set of fields for account index queries by removing fields that the current 51 : * index version doesn't support and accounting for numeric vs. string primary keys. The primary 52 : * key situation is temporary and should be removed after the migration is done. 53 : */ 54 : public static Set<String> accountFields(QueryOptions opts, boolean useLegacyNumericFields) { 55 5 : return accountFields(opts.fields(), useLegacyNumericFields); 56 : } 57 : 58 : /** 59 : * Returns a sanitized set of fields for account index queries by removing fields that the current 60 : * index version doesn't support and accounting for numeric vs. string primary keys. The primary 61 : * key situation is temporary and should be removed after the migration is done. 62 : */ 63 : public static Set<String> accountFields(Set<String> fields, boolean useLegacyNumericFields) { 64 : String idFieldName = 65 6 : useLegacyNumericFields 66 0 : ? AccountField.ID_FIELD_SPEC.getName() 67 6 : : AccountField.ID_STR_FIELD_SPEC.getName(); 68 6 : return fields.contains(idFieldName) ? fields : Sets.union(fields, ImmutableSet.of(idFieldName)); 69 : } 70 : 71 : /** 72 : * Returns a sanitized set of fields for change index queries by removing fields that the current 73 : * index version doesn't support. 74 : */ 75 : public static Set<String> changeFields(QueryOptions opts) { 76 : // Ensure we request enough fields to construct a ChangeData. We need both 77 : // change ID and project, which can either come via the Change field or 78 : // separate fields. 79 4 : Set<String> fs = opts.fields(); 80 4 : if (fs.contains(CHANGE.getName())) { 81 : // A Change is always sufficient. 82 4 : return fs; 83 : } 84 4 : if (fs.contains(PROJECT_SPEC.getName()) && fs.contains(LEGACY_ID_STR.getName())) { 85 0 : return fs; 86 : } 87 4 : return Sets.union(fs, ImmutableSet.of(LEGACY_ID_STR.getName(), PROJECT_SPEC.getName())); 88 : } 89 : 90 : /** 91 : * Returns a sanitized set of fields for group index queries by removing fields that the index 92 : * doesn't support and accounting for numeric vs. string primary keys. The primary key situation 93 : * is temporary and should be removed after the migration is done. 94 : */ 95 : public static Set<String> groupFields(QueryOptions opts) { 96 7 : Set<String> fs = opts.fields(); 97 7 : return fs.contains(GroupField.UUID_FIELD_SPEC.getName()) 98 7 : ? fs 99 0 : : Sets.union(fs, ImmutableSet.of(GroupField.UUID_FIELD_SPEC.getName())); 100 : } 101 : 102 : /** Returns a index-friendly representation of a {@link CurrentUser} to be used in queries. */ 103 : public static String describe(CurrentUser user) { 104 117 : if (user.isIdentifiedUser()) { 105 117 : return user.getAccountId().toString(); 106 : } 107 12 : if (user instanceof GroupBackedUser) { 108 4 : return "group:" + user.getEffectiveGroups().getKnownGroups().iterator().next().toString(); 109 : } 110 12 : return user.toString(); 111 : } 112 : 113 : /** 114 : * Returns a sanitized set of fields for project index queries by removing fields that the index 115 : * doesn't support. 116 : */ 117 : public static Set<String> projectFields(QueryOptions opts) { 118 2 : Set<String> fs = opts.fields(); 119 2 : return fs.contains(ProjectField.NAME.getName()) 120 2 : ? fs 121 0 : : Sets.union(fs, ImmutableSet.of(ProjectField.NAME.getName())); 122 : } 123 : 124 : private IndexUtils() { 125 : // hide default constructor 126 : } 127 : }