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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.server.computation.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 javax.annotation.CheckForNull;
  26. import javax.annotation.Nonnull;
  27. import org.picocontainer.Startable;
  28. import org.sonar.db.metric.MetricDto;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.server.db.DbClient;
  31. import static com.google.common.collect.FluentIterable.from;
  32. import static java.util.Objects.requireNonNull;
  33. public class MetricRepositoryImpl implements MetricRepository, Startable {
  34. private final DbClient dbClient;
  35. @CheckForNull
  36. private Map<String, Metric> metricsByKey;
  37. @CheckForNull
  38. private Map<Long, Metric> metricsById;
  39. public MetricRepositoryImpl(DbClient dbClient) {
  40. this.dbClient = dbClient;
  41. }
  42. @Override
  43. public void start() {
  44. DbSession dbSession = dbClient.openSession(false);
  45. try {
  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. } finally {
  50. dbSession.close();
  51. }
  52. }
  53. @Override
  54. public void stop() {
  55. // nothing to do when stopping
  56. }
  57. @Override
  58. public Metric getByKey(String key) {
  59. requireNonNull(key);
  60. verifyMetricsInitialized();
  61. Metric res = this.metricsByKey.get(key);
  62. if (res == null) {
  63. throw new IllegalStateException(String.format("Metric with key '%s' does not exist", key));
  64. }
  65. return res;
  66. }
  67. @Override
  68. public Metric getById(long id) {
  69. verifyMetricsInitialized();
  70. Metric res = this.metricsById.get(id);
  71. if (res == null) {
  72. throw new IllegalStateException(String.format("Metric with id '%s' does not exist", id));
  73. }
  74. return res;
  75. }
  76. @Override
  77. public Iterable<Metric> getAll() {
  78. return FluentIterable.from(metricsByKey.values()).toSet();
  79. }
  80. private void verifyMetricsInitialized() {
  81. if (this.metricsByKey == null) {
  82. throw new IllegalStateException("Metric cache has not been initialized");
  83. }
  84. }
  85. private enum MetricToKey implements Function<Metric, String> {
  86. INSTANCE;
  87. @Override
  88. @Nonnull
  89. public String apply(@Nonnull Metric metric) {
  90. return metric.getKey();
  91. }
  92. }
  93. private enum MetricToId implements Function<Metric, Long> {
  94. INSTANCE;
  95. @Override
  96. @Nonnull
  97. public Long apply(@Nonnull Metric metric) {
  98. return (long) metric.getId();
  99. }
  100. }
  101. }