From ffda1f87ba6b4fb5be0837ba5644d842d4e82354 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 21 Nov 2014 13:22:38 +0100 Subject: [PATCH] Fix quality flaws --- .../sonar/server/db/ResultSetIterator.java | 28 +++-------- .../java/org/sonar/server/es/BulkIndexer.java | 9 ++-- .../sonar/server/issue/index/IssueDoc.java | 46 ++++++++--------- .../server/es/IndexDefinitionContextTest.java | 50 +++++++++++++++++++ .../sonar/core/persistence/BatchSession.java | 4 +- 5 files changed, 86 insertions(+), 51 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/es/IndexDefinitionContextTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/ResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/db/ResultSetIterator.java index a8f882e4f30..8e83eefcb36 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/ResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/ResultSetIterator.java @@ -37,10 +37,6 @@ public abstract class ResultSetIterator implements Iterator, Closeable { private final ResultSet rs; private final PreparedStatement stmt; - // TODO can be simpler by using rs.isLast(). See ResultSetIterator from commons-dbutils - private boolean didNext = false; - private boolean hasNext = false; - public ResultSetIterator(PreparedStatement stmt) throws SQLException { this.stmt = stmt; this.rs = stmt.executeQuery(); @@ -53,25 +49,21 @@ public abstract class ResultSetIterator implements Iterator, Closeable { @Override public boolean hasNext() { - if (!didNext) { - hasNext = doNextQuietly(); - didNext = true; + try { + return !rs.isLast(); + } catch (SQLException e) { + throw new IllegalStateException("Fail to call ResultSet#isLast()", e); } - return hasNext; } @Override @CheckForNull public E next() { - if (!didNext) { - doNextQuietly(); - } - didNext = false; try { + rs.next(); return read(rs); } catch (SQLException e) { - // TODO add SQL request to context - throw new IllegalStateException("Fail to read result set row", e); + throw new IllegalStateException("Fail to read row of JDBC result set", e); } } @@ -87,12 +79,4 @@ public abstract class ResultSetIterator implements Iterator, Closeable { } protected abstract E read(ResultSet rs) throws SQLException; - - private boolean doNextQuietly() { - try { - return rs.next(); - } catch (SQLException e) { - throw new IllegalStateException("Fail to read row of JDBC result set", e); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java index 645130ffa58..972f7269b28 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java @@ -42,7 +42,8 @@ import java.util.Map; */ public class BulkIndexer implements Startable { - public static final long FLUSH_BYTE_SIZE = new ByteSizeValue(5, ByteSizeUnit.MB).bytes(); + private static final long FLUSH_BYTE_SIZE = new ByteSizeValue(5, ByteSizeUnit.MB).bytes(); + private static final String REFRESH_INTERVAL_SETTING = "index.refresh_interval"; private final EsClient client; private final String indexName; @@ -99,9 +100,9 @@ public class BulkIndexer implements Startable { } // deactivate periodical refresh - String refreshInterval = settingsResp.getSetting(indexName, "index.refresh_interval"); - largeInitialSettings.put("index.refresh_interval", refreshInterval); - bulkSettings.put("index.refresh_interval", "-1"); + String refreshInterval = settingsResp.getSetting(indexName, REFRESH_INTERVAL_SETTING); + largeInitialSettings.put(REFRESH_INTERVAL_SETTING, refreshInterval); + bulkSettings.put(REFRESH_INTERVAL_SETTING, "-1"); updateSettings(bulkSettings); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java index 12ce0396c27..6a3b7e2871a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java @@ -196,36 +196,36 @@ public class IssueDoc extends BaseDoc implements Issue { return getNullableField(IssueNormalizer.IssueField.FILE_PATH.field()); } - public void setKey(String s) { + public void setKey(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_KEY, s); } - public void setComponentUuid(String s) { + public void setComponentUuid(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID, s); } - public void setModuleUuid(String s) { + public void setModuleUuid(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID, s); } - public void setProjectUuid(String s) { + public void setProjectUuid(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID, s); } - public void setRuleKey(String s) { + public void setRuleKey(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_RULE_KEY, s); } - public void setLanguage(String s) { + public void setLanguage(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_LANGUAGE, s); } - public void setSeverity(String s) { + public void setSeverity(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY, s); setField(IssueIndexDefinition.FIELD_ISSUE_SEVERITY_VALUE, Severity.ALL.indexOf(s)); } - public void setMessage(String s) { + public void setMessage(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_MESSAGE, s); } @@ -237,63 +237,63 @@ public class IssueDoc extends BaseDoc implements Issue { setField(IssueIndexDefinition.FIELD_ISSUE_EFFORT, d); } - public void setStatus(String s) { + public void setStatus(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_STATUS, s); } - public void setResolution(String s) { + public void setResolution(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_RESOLUTION, s); } - public void setReporter(String s) { + public void setReporter(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_REPORTER, s); } - public void setAssignee(String s) { + public void setAssignee(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE, s); } - public void setCreationDate(Date d) { + public void setCreationDate(@Nullable Date d) { setField(IssueIndexDefinition.FIELD_ISSUE_CREATED_AT, d); } - public void setUpdateDate(Date d) { + public void setUpdateDate(@Nullable Date d) { setField(IssueIndexDefinition.FIELD_ISSUE_UPDATED_AT, d); } - public void setFuncCreationDate(Date d) { + public void setFuncCreationDate(@Nullable Date d) { setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT, d); } - public void setFuncUpdateDate(Date d) { + public void setFuncUpdateDate(@Nullable Date d) { setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_UPDATED_AT, d); } - public void setFuncCloseDate(Date d) { + public void setFuncCloseDate(@Nullable Date d) { setField(IssueIndexDefinition.FIELD_ISSUE_FUNC_CLOSED_AT, d); } - public void setAttributes(String s) { + public void setAttributes(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_ATTRIBUTES, s); } - public void setAuthorLogin(String s) { + public void setAuthorLogin(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_AUTHOR_LOGIN, s); } - public void setActionPlanKey(String s) { + public void setActionPlanKey(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_ACTION_PLAN, s); } - public void setDebt(Long l) { + public void setDebt(@Nullable Long l) { setField(IssueIndexDefinition.FIELD_ISSUE_DEBT, l); } - public void setFilePath(String s) { + public void setFilePath(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH, s); } - public void setModuleUuidPath(String s) { + public void setModuleUuidPath(@Nullable String s) { setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH, s); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/IndexDefinitionContextTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/IndexDefinitionContextTest.java new file mode 100644 index 00000000000..1b8b8bfe319 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/es/IndexDefinitionContextTest.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.es; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; + +public class IndexDefinitionContextTest { + + @Test + public void create_indices() throws Exception { + IndexDefinition.IndexDefinitionContext context = new IndexDefinition.IndexDefinitionContext(); + + context.create("issues"); + context.create("measures"); + assertThat(context.getIndices().keySet()).containsOnly("issues", "measures"); + } + + @Test + public void fail_to_create_twice_the_same_index() throws Exception { + IndexDefinition.IndexDefinitionContext context = new IndexDefinition.IndexDefinitionContext(); + + context.create("issues"); + try { + context.create("issues"); + fail(); + } catch (IllegalArgumentException ok) { + assertThat(ok).hasMessage("Index already exists: issues"); + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java index 68145a8b478..bcc1b481a18 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/BatchSession.java @@ -70,13 +70,13 @@ public class BatchSession extends DbSession { @Override public T selectOne(String statement) { reset(); - return (T) super.selectOne(statement); + return super.selectOne(statement); } @Override public T selectOne(String statement, Object parameter) { reset(); - return (T) super.selectOne(statement, parameter); + return super.selectOne(statement, parameter); } @Override -- 2.39.5