]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5666 SONAR-5697 Add missing DeleteByQuery
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Nov 2014 09:10:03 +0000 (10:10 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Nov 2014 09:10:11 +0000 (10:10 +0100)
server/sonar-server/src/main/java/org/sonar/server/search/SearchClient.java
server/sonar-server/src/main/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilder.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilderTest.java [new file with mode: 0644]

index 8383658bb548ccbad676f0a5c876fdc9c33f4557..a05d88aabd4a92034bc648a2f6d83cff7f1a8a3e 100644 (file)
@@ -179,8 +179,7 @@ public class SearchClient extends TransportClient implements Startable {
 
   @Override
   public DeleteByQueryRequestBuilder prepareDeleteByQuery(String... indices) {
-    // TODO
-    return new DeleteByQueryRequestBuilder(this).setIndices(indices);
+    return new ProxyDeleteByQueryRequestBuilder(this, profiling).setIndices(indices);
   }
 
   // ****************************************************************************************************************
diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilder.java
new file mode 100644 (file)
index 0000000..5201b6a
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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.search.request;
+
+import org.apache.commons.lang.StringUtils;
+import org.elasticsearch.action.ListenableActionFuture;
+import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
+import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
+import org.elasticsearch.common.unit.TimeValue;
+import org.sonar.core.profiling.Profiling;
+import org.sonar.core.profiling.StopWatch;
+import org.sonar.server.search.SearchClient;
+
+public class ProxyDeleteByQueryRequestBuilder extends DeleteByQueryRequestBuilder {
+
+  private final Profiling profiling;
+
+  public ProxyDeleteByQueryRequestBuilder(SearchClient client, Profiling profiling) {
+    super(client);
+    this.profiling = profiling;
+  }
+
+  @Override
+  public DeleteByQueryResponse get() {
+    StopWatch fullProfile = profiling.start("delete by query", Profiling.Level.FULL);
+    try {
+      return super.execute().actionGet();
+    } catch (Exception e) {
+      throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
+    } finally {
+      if (profiling.isProfilingEnabled(Profiling.Level.BASIC)) {
+        fullProfile.stop("%s", toString());
+      }
+    }
+  }
+
+  @Override
+  public DeleteByQueryResponse get(TimeValue timeout) {
+    throw new IllegalStateException("Not yet implemented");
+  }
+
+  @Override
+  public DeleteByQueryResponse get(String timeout) {
+    throw new IllegalStateException("Not yet implemented");
+  }
+
+  @Override
+  public ListenableActionFuture<DeleteByQueryResponse> execute() {
+    throw new UnsupportedOperationException("execute() should not be called as it's used for asynchronous");
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder message = new StringBuilder();
+    message.append("ES delete by query request");
+    if (request.indices().length > 0) {
+      message.append(String.format(" on indices '%s'", StringUtils.join(request.indices(), ",")));
+    }
+    return message.toString();
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilderTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/request/ProxyDeleteByQueryRequestBuilderTest.java
new file mode 100644 (file)
index 0000000..ab0e622
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * 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.search.request;
+
+import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
+import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.RuleTesting;
+import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.search.IndexDefinition;
+import org.sonar.server.search.SearchClient;
+import org.sonar.server.tester.ServerTester;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class ProxyDeleteByQueryRequestBuilderTest {
+
+  @ClassRule
+  public static ServerTester tester = new ServerTester();
+
+  DbSession dbSession;
+
+  SearchClient searchClient;
+
+  @Before
+  public void setUp() throws Exception {
+    tester.clearDbAndIndexes();
+    dbSession = tester.get(DbClient.class).openSession(false);
+    searchClient = tester.get(SearchClient.class);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    dbSession.close();
+  }
+
+  @Test
+  public void bulk() {
+    tester.get(RuleDao.class).insert(dbSession, RuleTesting.newXooX1());
+    dbSession.commit();
+
+    DeleteByQueryRequestBuilder requestBuilder = searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName())
+      .setQuery(QueryBuilders.matchAllQuery());
+
+    DeleteByQueryResponse response = requestBuilder.get();
+    assertThat(response.iterator()).hasSize(1);
+  }
+
+  @Test
+  public void to_string() {
+    assertThat(searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName()).toString()).isEqualTo("ES delete by query request on indices 'rules'");
+    assertThat(searchClient.prepareDeleteByQuery().toString()).isEqualTo("ES delete by query request");
+  }
+
+  @Test
+  public void fail_to_bulk_bad_query() throws Exception {
+    try {
+        searchClient.prepareDeleteByQuery(IndexDefinition.RULE.getIndexName()).get();
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalStateException.class);
+      assertThat(e.getMessage()).contains("Fail to execute ES delete by query request on indices 'rules'");
+    }
+  }
+
+  @Test
+  public void get_with_string_timeout_is_not_yet_implemented() throws Exception {
+    try {
+      searchClient.prepareDeleteByQuery().get("1");
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
+    }
+  }
+
+  @Test
+  public void get_with_time_value_timeout_is_not_yet_implemented() throws Exception {
+    try {
+      searchClient.prepareDeleteByQuery().get(TimeValue.timeValueMinutes(1));
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
+    }
+  }
+
+  @Test
+  public void execute_should_throw_an_unsupported_operation_exception() throws Exception {
+    try {
+      searchClient.prepareDeleteByQuery().execute();
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");
+    }
+  }
+
+}