LCOV - code coverage report
Current view: top level - index - IndexConfig.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 40 40 100.0 %
Date: 2022-11-19 15:00:39 Functions: 10 10 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.auto.value.AutoValue;
      20             : import java.util.function.Consumer;
      21             : import java.util.function.IntConsumer;
      22             : import org.eclipse.jgit.lib.Config;
      23             : 
      24             : /**
      25             :  * Implementation-specific configuration for secondary indexes.
      26             :  *
      27             :  * <p>Contains configuration that is tied to a specific index implementation but is otherwise
      28             :  * global, i.e. not tied to a specific {@link Index} and schema version.
      29             :  */
      30             : @AutoValue
      31         152 : public abstract class IndexConfig {
      32             :   private static final int DEFAULT_MAX_TERMS = 1024;
      33             :   private static final int DEFAULT_PAGE_SIZE_MULTIPLIER = 1;
      34             : 
      35             :   public static IndexConfig createDefault() {
      36           3 :     return builder().build();
      37             :   }
      38             : 
      39             :   public static Builder fromConfig(Config cfg) {
      40         151 :     Builder b = builder();
      41         151 :     setIfPresent(cfg, "maxLimit", b::maxLimit);
      42         151 :     setIfPresent(cfg, "maxPages", b::maxPages);
      43         151 :     setIfPresent(cfg, "maxTerms", b::maxTerms);
      44         151 :     setIfPresent(cfg, "pageSizeMultiplier", b::pageSizeMultiplier);
      45         151 :     setIfPresent(cfg, "maxPageSize", b::maxPageSize);
      46         151 :     setTypeOrDefault(cfg, b::type);
      47         151 :     setPaginationTypeOrDefault(cfg, b::paginationType);
      48         151 :     return b;
      49             :   }
      50             : 
      51             :   private static void setIfPresent(Config cfg, String name, IntConsumer setter) {
      52         151 :     int n = cfg.getInt("index", null, name, 0);
      53         151 :     if (n != 0) {
      54          12 :       setter.accept(n);
      55             :     }
      56         151 :   }
      57             : 
      58             :   private static void setTypeOrDefault(Config cfg, Consumer<String> setter) {
      59         151 :     String type = cfg != null ? cfg.getString("index", null, "type") : null;
      60         151 :     setter.accept(new IndexType(type).toString());
      61         151 :   }
      62             : 
      63             :   private static void setPaginationTypeOrDefault(Config cfg, Consumer<PaginationType> setter) {
      64         151 :     setter.accept(
      65         151 :         cfg != null ? cfg.getEnum("index", null, "paginationType", PaginationType.OFFSET) : null);
      66         151 :   }
      67             : 
      68             :   public static Builder builder() {
      69         152 :     return new AutoValue_IndexConfig.Builder()
      70         152 :         .maxLimit(Integer.MAX_VALUE)
      71         152 :         .maxPages(Integer.MAX_VALUE)
      72         152 :         .maxTerms(DEFAULT_MAX_TERMS)
      73         152 :         .pageSizeMultiplier(DEFAULT_PAGE_SIZE_MULTIPLIER)
      74         152 :         .maxPageSize(Integer.MAX_VALUE)
      75         152 :         .type(IndexType.getDefault())
      76         152 :         .separateChangeSubIndexes(false)
      77         152 :         .paginationType(PaginationType.OFFSET);
      78             :   }
      79             : 
      80             :   @AutoValue.Builder
      81         152 :   public abstract static class Builder {
      82             :     public abstract Builder maxLimit(int maxLimit);
      83             : 
      84             :     public abstract int maxLimit();
      85             : 
      86             :     public abstract Builder maxPages(int maxPages);
      87             : 
      88             :     public abstract int maxPages();
      89             : 
      90             :     public abstract Builder maxTerms(int maxTerms);
      91             : 
      92             :     public abstract int maxTerms();
      93             : 
      94             :     public abstract Builder type(String type);
      95             : 
      96             :     public abstract String type();
      97             : 
      98             :     public abstract Builder separateChangeSubIndexes(boolean separate);
      99             : 
     100             :     public abstract Builder paginationType(PaginationType type);
     101             : 
     102             :     public abstract Builder pageSizeMultiplier(int pageSizeMultiplier);
     103             : 
     104             :     public abstract Builder maxPageSize(int maxPageSize);
     105             : 
     106             :     abstract IndexConfig autoBuild();
     107             : 
     108             :     public IndexConfig build() {
     109         152 :       IndexConfig cfg = autoBuild();
     110         152 :       checkLimit(cfg.maxLimit(), "maxLimit");
     111         152 :       checkLimit(cfg.maxPages(), "maxPages");
     112         152 :       checkLimit(cfg.maxTerms(), "maxTerms");
     113         152 :       checkLimit(cfg.pageSizeMultiplier(), "pageSizeMultiplier");
     114         152 :       checkLimit(cfg.maxPageSize(), "maxPageSize");
     115         152 :       return cfg;
     116             :     }
     117             :   }
     118             : 
     119             :   private static void checkLimit(int limit, String name) {
     120         152 :     checkArgument(limit > 0, "%s must be positive: %s", name, limit);
     121         152 :   }
     122             : 
     123             :   /**
     124             :    * Returns maximum limit supported by the underlying index, or limited for performance reasons.
     125             :    */
     126             :   public abstract int maxLimit();
     127             : 
     128             :   /**
     129             :    * Returns maximum number of pages (limit / start) supported by the underlying index, or limited
     130             :    * for performance reasons.
     131             :    */
     132             :   public abstract int maxPages();
     133             : 
     134             :   /**
     135             :    * Returns maximum number of total index query terms supported by the underlying index, or limited
     136             :    * for performance reasons.
     137             :    */
     138             :   public abstract int maxTerms();
     139             : 
     140             :   /** Returns index type. */
     141             :   public abstract String type();
     142             : 
     143             :   /**
     144             :    * Returns whether different subsets of changes may be stored in different physical sub-indexes.
     145             :    */
     146             :   public abstract boolean separateChangeSubIndexes();
     147             : 
     148             :   /**
     149             :    * Returns pagination type to use when index queries are repeated to obtain the next set of
     150             :    * results.
     151             :    */
     152             :   public abstract PaginationType paginationType();
     153             : 
     154             :   /**
     155             :    * Returns multiplier to be used to determine the limit when queries are repeated to obtain the
     156             :    * next set of results.
     157             :    */
     158             :   public abstract int pageSizeMultiplier();
     159             : 
     160             :   /**
     161             :    * Returns maximum allowed limit when repeating index queries to obtain the next set of results.
     162             :    */
     163             :   public abstract int maxPageSize();
     164             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750