From: Simon Brandhof Date: Fri, 26 Feb 2016 19:44:23 +0000 (+0100) Subject: SONAR-7330 delete ES script "ListUpdate" X-Git-Tag: 5.5-M6~48 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=82b5d427f529affe6ca198ac92c62273946609ec;p=sonarqube.git SONAR-7330 delete ES script "ListUpdate" --- diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java index 6696bd8886c..d2312154872 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java @@ -65,9 +65,6 @@ public class ProcessProperties { */ public static final String ENABLE_STOP_COMMAND = "sonar.enableStopCommand"; - // Constants declared by the ES plugin ListUpdate (see sonar-search) - // that are used by sonar-server - public static final String ES_PLUGIN_LISTUPDATE_SCRIPT_NAME = "listUpdate"; public static final String ES_PLUGIN_LISTUPDATE_ID_FIELD = "idField"; public static final String ES_PLUGIN_LISTUPDATE_ID_VALUE = "idValue"; public static final String ES_PLUGIN_LISTUPDATE_FIELD = "field"; diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java b/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java index 839fb1f3051..db634f5c587 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java +++ b/server/sonar-search/src/main/java/org/sonar/search/SearchSettings.java @@ -19,6 +19,11 @@ */ package org.sonar.search; +import java.io.File; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.TreeSet; import org.apache.commons.lang.StringUtils; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.ImmutableSettings; @@ -28,13 +33,6 @@ import org.slf4j.LoggerFactory; import org.sonar.process.MessageException; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; -import org.sonar.search.script.ListUpdate; - -import java.io.File; -import java.util.Arrays; -import java.util.LinkedHashSet; -import java.util.Set; -import java.util.TreeSet; class SearchSettings { @@ -62,7 +60,6 @@ class SearchSettings { ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder(); configureFileSystem(builder); configureIndexDefaults(builder); - configurePlugins(builder); configureNetwork(builder); configureCluster(builder); configureMarvel(builder); @@ -104,13 +101,6 @@ class SearchSettings { builder.put("path.logs", logDir.getAbsolutePath()); } - private void configurePlugins(ImmutableSettings.Builder builder) { - builder - .put("script.default_lang", "native") - .put(String.format("script.native.%s.type", ProcessProperties.ES_PLUGIN_LISTUPDATE_SCRIPT_NAME), - ListUpdate.UpdateListScriptFactory.class.getName()); - } - private void configureNetwork(ImmutableSettings.Builder builder) { // the following properties can't be null as default values are defined by app process String host = props.nonNullValue(ProcessProperties.SEARCH_HOST); diff --git a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java b/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java deleted file mode 100644 index 0b05cae8a41..00000000000 --- a/server/sonar-search/src/main/java/org/sonar/search/script/ListUpdate.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.search.script; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.xcontent.support.XContentMapValues; -import org.elasticsearch.script.AbstractExecutableScript; -import org.elasticsearch.script.ExecutableScript; -import org.elasticsearch.script.NativeScriptFactory; -import org.sonar.process.ProcessProperties; - -public class ListUpdate extends AbstractExecutableScript { - - public static class UpdateListScriptFactory implements NativeScriptFactory { - @Override - public ExecutableScript newScript(@Nullable Map params) { - String idField = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD), null); - String idValue = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE), null); - String field = XContentMapValues.nodeStringValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD), null); - Map value = null; - if (idField == null) { - throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD)); - } - if (idValue == null) { - throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE)); - } - if (field == null) { - throw new IllegalStateException(String.format("Missing '%s' parameter", ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD)); - } - - // NULL case is deletion of nested item - if (params.containsKey(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE)) { - Object obj = params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE); - if (obj != null) { - value = XContentMapValues.nodeMapValue(params.get(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE), "Update item"); - } - } - - return new ListUpdate(idField, idValue, field, value); - } - } - - private final String idField; - private final String idValue; - private final String field; - private final Map value; - private Map ctx; - - public ListUpdate(String idField, String idValue, String field, @Nullable Map value) { - this.idField = idField; - this.idValue = idValue; - this.field = field; - this.value = value; - } - - @Override - public void setNextVar(String name, Object value) { - if ("ctx".equals(name)) { - ctx = (Map) value; - } - } - - @Override - public Object unwrap(Object value) { - return value; - } - - @Override - public Object run() { - try { - // Get the Document's source from ctx - Map source = XContentMapValues.nodeMapValue(ctx.get("_source"), "source from context"); - - // Get the Object for list update - Object fieldValue = source.get(field); - - if (fieldValue == null && value != null) { - // 0. The field does not exist (this is a upsert then) - List values = new ArrayList<>(1); - values.add(value); - source.put(field, values); - } else if (!XContentMapValues.isArray(fieldValue) && value != null) { - // 1. The field is not yet a list - Map currentFieldValue = XContentMapValues.nodeMapValue(fieldValue, "current FieldValue"); - if (XContentMapValues.nodeStringValue(currentFieldValue.get(idField), null).equals(idValue)) { - source.put(field, value); - } else { - List values = new ArrayList<>(2); - values.add(fieldValue); - values.add(value); - source.put(field, values); - } - } else { - // 3. field is a list - Collection items = (Collection) fieldValue; - Object target = null; - for (Object item : items) { - Map fields = (Map) item; - String itemIdValue = XContentMapValues.nodeStringValue(fields.get(idField), null); - if (itemIdValue != null && itemIdValue.equals(idValue)) { - target = item; - break; - } - } - if (target != null) { - items.remove(target); - } - - // Supporting the update by NULL = deletion case - if (value != null) { - items.add(value); - } - source.put(field, items); - } - } catch (Exception e) { - throw new IllegalStateException("failed to execute listUpdate script", e); - } - return null; - - } -} diff --git a/server/sonar-search/src/main/java/org/sonar/search/script/package-info.java b/server/sonar-search/src/main/java/org/sonar/search/script/package-info.java deleted file mode 100644 index 889d7282632..00000000000 --- a/server/sonar-search/src/main/java/org/sonar/search/script/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.search.script; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java b/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java deleted file mode 100644 index cb83aaad54a..00000000000 --- a/server/sonar-search/src/test/java/org/sonar/search/script/UpdateListScriptTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.search.script; - -import com.google.common.collect.ImmutableMap; -import org.elasticsearch.script.ExecutableScript; -import org.junit.Before; -import org.junit.Test; -import org.sonar.process.ProcessProperties; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; - -public class UpdateListScriptTest { - - ListUpdate.UpdateListScriptFactory factory; - - @Before - public void setUp() { - factory = new ListUpdate.UpdateListScriptFactory(); - } - - @Test - public void fail_missing_attributes_field() { - Map params = new HashMap<>(); - - // Missing everything - try { - factory.newScript(params); - fail(); - } catch (Exception e) { - assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter"); - } - - // Missing ID_VALUE - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "test"); - try { - factory.newScript(params); - fail(); - } catch (Exception e) { - assertThat(e.getMessage()).isEqualTo("Missing 'idValue' parameter"); - } - - // Missing FIELD - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "test"); - try { - factory.newScript(params); - fail(); - } catch (Exception e) { - assertThat(e.getMessage()).isEqualTo("Missing 'field' parameter"); - } - - // Has all required attributes and Null Value - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, "test"); - ExecutableScript script = factory.newScript(params); - assertThat(script).isNotNull(); - - // Has all required attributes and VALUE of wrong type - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, new Integer(52)); - try { - factory.newScript(params); - fail(); - } catch (Exception e) { - - } - - // Has all required attributes and Proper VALUE - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, ImmutableMap.of("key", "value")); - script = factory.newScript(params); - assertThat(script).isNotNull(); - - // Missing ID_FIELD - params.remove(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD); - try { - factory.newScript(params); - fail(); - } catch (Exception e) { - assertThat(e.getMessage()).isEqualTo("Missing 'idField' parameter"); - } - } - - @Test - public void update_list() { - - String listField = "listField"; - Collection> mapFields; - Map source = new HashMap<>(); - source.put("field1", "value1"); - - // 0 Create list when field does not exists - Map params = new HashMap<>(); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "A")); - - ExecutableScript script = factory.newScript(params); - script.setNextVar("ctx", ImmutableMap.of("_source", source)); - script.run(); - - mapFields = (Collection) source.get(listField); - System.out.println("source = " + source); - assertThat(mapFields).hasSize(1); - - // Add item to existing list - params = new HashMap<>(); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "B")); - script = factory.newScript(params); - script.setNextVar("ctx", ImmutableMap.of("_source", source)); - script.run(); - mapFields = (Collection) source.get(listField); - assertThat(mapFields).hasSize(2); - - // updated first item in list - params = new HashMap<>(); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "1", "value", "a")); - script = factory.newScript(params); - script.setNextVar("ctx", ImmutableMap.of("_source", source)); - script.run(); - mapFields = (Collection) source.get(listField); - assertThat(mapFields).hasSize(2); - - // updated second item in list - params = new HashMap<>(); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "2"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, mapOf("key", "2", "value", "b")); - script = factory.newScript(params); - script.setNextVar("ctx", ImmutableMap.of("_source", source)); - script.run(); - mapFields = (Collection) source.get(listField); - assertThat(mapFields).hasSize(2); - - // delete first item - params = new HashMap<>(); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_FIELD, listField); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_FIELD, "key"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_ID_VALUE, "1"); - params.put(ProcessProperties.ES_PLUGIN_LISTUPDATE_VALUE, null); - script = factory.newScript(params); - script.setNextVar("ctx", ImmutableMap.of("_source", source)); - script.run(); - mapFields = (Collection) source.get(listField); - assertThat(mapFields).hasSize(1); - } - - private Map mapOf(String k, String v, String k1, String v1) { - Map map = new HashMap<>(); - map.put(k, v); - map.put(k1, v1); - return map; - } -}