You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetricRepositoryImpl.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.metric;
  21. import com.google.common.base.Function;
  22. import com.google.common.collect.FluentIterable;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import javax.annotation.CheckForNull;
  27. import javax.annotation.Nonnull;
  28. import org.picocontainer.Startable;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.metric.MetricDto;
  32. import static com.google.common.collect.FluentIterable.from;
  33. import static java.util.Objects.requireNonNull;
  34. public class MetricRepositoryImpl implements MetricRepository, Startable {
  35. private final DbClient dbClient;
  36. @CheckForNull
  37. private Map<String, Metric> metricsByKey;
  38. @CheckForNull
  39. private Map<Long, Metric> metricsById;
  40. public MetricRepositoryImpl(DbClient dbClient) {
  41. this.dbClient = dbClient;
  42. }
  43. @Override
  44. public void start() {
  45. try (DbSession dbSession = dbClient.openSession(false)) {
  46. List<MetricDto> metricList = dbClient.metricDao().selectEnabled(dbSession);
  47. this.metricsByKey = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToKey.INSTANCE);
  48. this.metricsById = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToId.INSTANCE);
  49. }
  50. }
  51. @Override
  52. public void stop() {
  53. // nothing to do when stopping
  54. }
  55. @Override
  56. public Metric getByKey(String key) {
  57. requireNonNull(key);
  58. verifyMetricsInitialized();
  59. Metric res = this.metricsByKey.get(key);
  60. if (res == null) {
  61. throw new IllegalStateException(String.format("Metric with key '%s' does not exist", key));
  62. }
  63. return res;
  64. }
  65. @Override
  66. public Metric getById(long id) {
  67. return getOptionalById(id)
  68. .orElseThrow(() -> new IllegalStateException(String.format("Metric with id '%s' does not exist", id)));
  69. }
  70. @Override
  71. public Optional<Metric> getOptionalById(long id) {
  72. verifyMetricsInitialized();
  73. return Optional.ofNullable(this.metricsById.get(id));
  74. }
  75. @Override
  76. public Iterable<Metric> getAll() {
  77. return FluentIterable.from(metricsByKey.values()).toSet();
  78. }
  79. private void verifyMetricsInitialized() {
  80. if (this.metricsByKey == null) {
  81. throw new IllegalStateException("Metric cache has not been initialized");
  82. }
  83. }
  84. private enum MetricToKey implements Function<Metric, String> {
  85. INSTANCE;
  86. @Override
  87. @Nonnull
  88. public String apply(@Nonnull Metric metric) {
  89. return metric.getKey();
  90. }
  91. }
  92. private enum MetricToId implements Function<Metric, Long> {
  93. INSTANCE;
  94. @Override
  95. @Nonnull
  96. public Long apply(@Nonnull Metric metric) {
  97. return (long) metric.getId();
  98. }
  99. }
  100. }