--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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");
+ }
+ }
+
+}