Line data Source code
1 : // Copyright (C) 2022 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 com.google.gerrit.common.Nullable; 18 : import java.io.IOException; 19 : 20 : /** Interfaces that define properties of fields in {@link Schema}. */ 21 0 : public class SchemaFieldDefs { 22 : 23 : /** 24 : * Definition of a field stored in the secondary index. 25 : * 26 : * <p>{@link SchemaField}-s must not be changed once introduced to the codebase. Instead, a new 27 : * FieldDef must be added and the old one removed from the schema (in two upgrade steps, see 28 : * {@code com.google.gerrit.index.IndexUpgradeValidator}). 29 : * 30 : * @param <I> input type from which documents are created and search results are returned. 31 : * @param <T> type that should be extracted from the input object when converting to an index 32 : * document. 33 : */ 34 : public interface SchemaField<T, I> { 35 : 36 : /** Returns whether the field should be stored in the index. */ 37 : boolean isStored(); 38 : 39 : /** Returns whether the field is repeatable. */ 40 : boolean isRepeatable(); 41 : 42 : /** 43 : * Get the field contents from the input object. 44 : * 45 : * @param input input object. 46 : * @return the field value(s) to index. 47 : */ 48 : @Nullable 49 : I get(T input); 50 : 51 : /** Returns the name of the field. */ 52 : String getName(); 53 : 54 : /** 55 : * Returns type of the field; for repeatable fields, the inner type, not the iterable type. 56 : * TODO(mariasavtchuk): remove after migrating to the new field formats 57 : */ 58 : FieldType<?> getType(); 59 : 60 : /** 61 : * Set the field contents back to an object. Used to reconstruct fields from indexed values. 62 : * No-op if the field can't be reconstructed. 63 : * 64 : * @param object input object. 65 : * @param doc indexed document 66 : * @return {@code true} if the field was set, {@code false} otherwise 67 : */ 68 : boolean setIfPossible(T object, StoredValue doc); 69 : } 70 : 71 : /** 72 : * Getter to extract value that should be stored in index from the input object. 73 : * 74 : * <p>This interface allows to specify a method or lambda for populating an index field. Note that 75 : * for existing fields, changing the code of either the {@link Getter} implementation or the 76 : * method(s) that it calls would invalidate existing index data. Therefore, instead of changing 77 : * the semantics of an existing field, a new field must be added using the new semantics from the 78 : * start. The old field can be removed in another upgrade step (cf. {@code 79 : * com.google.gerrit.index.IndexUpgradeValidator}). 80 : * 81 : * @param <I> type from which documents are created and search results are returned. 82 : * @param <T> type that should be extracted from the input object to an index field. 83 : */ 84 : @FunctionalInterface 85 : public interface Getter<I, T> { 86 : @Nullable 87 : T get(I input) throws IOException; 88 : } 89 : 90 : /** 91 : * Setter to reconstruct fields from indexed values back to an object. 92 : * 93 : * <p>See {@link Getter} for restrictions on changing the implementation. 94 : * 95 : * @param <I> type from which documents are created and search results are returned. 96 : * @param <T> type that should be extracted from the input object when converting toto an index 97 : * field. 98 : */ 99 : @FunctionalInterface 100 : public interface Setter<I, T> { 101 : void set(I object, T value); 102 : } 103 : }