LCOV - code coverage report
Current view: top level - server/schema - NoteDbSchemaVersions.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 12 15 80.0 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2018 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.schema;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkArgument;
      18             : import static com.google.common.collect.ImmutableSortedMap.toImmutableSortedMap;
      19             : import static java.util.Comparator.naturalOrder;
      20             : 
      21             : import com.google.common.collect.ImmutableSortedMap;
      22             : import com.google.common.primitives.Ints;
      23             : import com.google.gerrit.common.UsedAt;
      24             : import java.lang.reflect.InvocationTargetException;
      25             : import java.util.Optional;
      26             : import java.util.stream.Stream;
      27             : 
      28           0 : public class NoteDbSchemaVersions {
      29         151 :   static final ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> ALL =
      30             :       // List all supported NoteDb schema versions here.
      31         151 :       Stream.of(
      32             :               Schema_180.class,
      33             :               Schema_181.class,
      34             :               Schema_182.class,
      35             :               Schema_183.class,
      36             :               Schema_184.class,
      37             :               Schema_185.class)
      38         151 :           .collect(toImmutableSortedMap(naturalOrder(), v -> guessVersion(v).get(), v -> v));
      39             : 
      40         151 :   public static final int FIRST = ALL.firstKey();
      41         151 :   public static final int LATEST = ALL.lastKey();
      42             : 
      43             :   // TODO(dborowitz): Migrate delete-project plugin to use this implementation.
      44             :   @UsedAt(UsedAt.Project.PLUGIN_DELETE_PROJECT)
      45             :   public static Optional<Integer> guessVersion(Class<?> c) {
      46         151 :     String prefix = "Schema_";
      47         151 :     if (!c.getSimpleName().startsWith(prefix)) {
      48           1 :       return Optional.empty();
      49             :     }
      50         151 :     return Optional.ofNullable(Ints.tryParse(c.getSimpleName().substring(prefix.length())));
      51             :   }
      52             : 
      53             :   public static NoteDbSchemaVersion get(
      54             :       ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> schemaVersions, int i) {
      55           1 :     Class<? extends NoteDbSchemaVersion> clazz = schemaVersions.get(i);
      56           1 :     checkArgument(clazz != null, "Schema version not found: %s", i);
      57             :     try {
      58           1 :       return clazz.getDeclaredConstructor().newInstance();
      59           0 :     } catch (InstantiationException
      60             :         | IllegalAccessException
      61             :         | NoSuchMethodException
      62             :         | InvocationTargetException e) {
      63           0 :       throw new IllegalStateException("failed to invoke constructor on " + clazz.getName(), e);
      64             :     }
      65             :   }
      66             : }

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