Apache Lucene open-source search software
SOLR-6365: specify appends, defaults, invariants outside of the request handler
Noble Paul367df46c7daf
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9b99fc305d7..a9e3dcc6fe5 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -119,6 +119,9 @@ New Features* SOLR-4580: Support for protecting content in ZooKeeper. (Per Steffensen, Mark Miller)+* SOLR-6365: specify appends, defaults, invariants outside of the request handler.+ (Noble Paul, Erik Hatcher, shalin)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/core/ParamSet.java b/solr/core/src/java/org/apache/solr/core/ParamSet.javanew file mode 100644index 00000000000..b997242fa4f--- /dev/null+++ b/solr/core/src/java/org/apache/solr/core/ParamSet.java@@ -0,0 +1,117 @@+package org.apache.solr.core;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.StrUtils;++import java.util.Collections;+import java.util.HashSet;+import java.util.List;+import java.util.Map;+import java.util.Set;+/**+ * An Object which represents a paramSet tag+ *+ */+public class ParamSet {+ public static final String TYPE = "paramSet";+ public final String name;+ public final Set<String> paths;+ public final NamedList defaults,invariants,appends;++ public ParamSet(PluginInfo p) {+ this.name = p.attributes.get("name");+ Set<String> paths = null;+ String pathStr = p.attributes.get("path");+ if(pathStr!=null) {+ paths = Collections.unmodifiableSet(new HashSet<>(StrUtils.splitSmart(pathStr, ',')));+ }+ this.paths = paths;+ NamedList nl = (NamedList) p.initArgs.get(PluginInfo.DEFAULTS);+ defaults = nl == null ? null: nl.getImmutableCopy();+ nl = (NamedList) p.initArgs.get(PluginInfo.INVARIANTS);+ invariants = nl == null ? null: nl.getImmutableCopy();+ nl = (NamedList) p.initArgs.get(PluginInfo.APPENDS);+ appends = nl == null ? null: nl.getImmutableCopy();+ }++ public boolean matchPath(String name) {+ if(paths == null) return false;+ if(paths.contains(name)) return true;++ for (String path : paths) {+ if(matchPath(path,name)) return true;+ }++ return false;+ }++ private static boolean matchPath(String path, String name){+ List<String> pathSplit = StrUtils.splitSmart(path, '/');+ List<String> nameSplit = StrUtils.splitSmart(name, '/');+ for (int i = 0; i < nameSplit.size(); i++) {+ String s = nameSplit.get(i);+ String ps = pathSplit.size()>i ? pathSplit.get(i) :null;+ if(ps == null) return false;+ if(s.equals(ps)) continue;+ if("*".equals(ps) && nameSplit.size()==i+1) return true;+ if("**".equals(ps)) return true;+ return false;+ }+ return true;++ }++ public void apply(NamedList initArgs) {+ merge(defaults, (NamedList) initArgs.get(PluginInfo.DEFAULTS), initArgs, PluginInfo.DEFAULTS, false);+ merge((NamedList) initArgs.get(PluginInfo.INVARIANTS), invariants, initArgs, PluginInfo.INVARIANTS, false);+ merge((NamedList) initArgs.get(PluginInfo.APPENDS), appends, initArgs, PluginInfo.APPENDS, true);+ }++ private static void merge(NamedList first, NamedList second, NamedList sink, String name, boolean appends) {+ if(first == null && second == null) return;+ if(first == null) first = new NamedList();+ NamedList nl = first.clone();+ if(appends) {+ nl.addAll(second);+ } else {+ Set<String> a = new HashSet<>();+ Set<String> b = new HashSet<>();+ for (Object o : first) {+ Map.Entry<String,Object> e = (Map.Entry) o;+ a.add(e.getKey() );+ }+ if(second!=null) {+ for (Object o : second) {+ Map.Entry<String, Object> e = (Map.Entry) o;+ b.add(e.getKey());+ }+ }+ for (String s : b) {+ if (a.contains(s)) continue;+ for (Object v : second.getAll(s)) nl.add(s, v);+ }+ }+ if(sink.indexOf(name,0) >-1) {+ sink.setVal(sink.indexOf(name, 0), nl);+ } else {+ sink.add(name,nl);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/core/PluginInfo.java b/solr/core/src/java/org/apache/solr/core/PluginInfo.javaindex 19bbf9054e1..110412b771d 100644--- a/solr/core/src/java/org/apache/solr/core/PluginInfo.java+++ b/solr/core/src/java/org/apache/solr/core/PluginInfo.java@@ -111,4 +111,7 @@ public class PluginInfo {"str","int","long","float","double"));+ public static final String DEFAULTS = "defaults";+ public static final String APPENDS = "appends";+ public static final String INVARIANTS = "invariants";}diff --git a/solr/core/src/java/org/apache/solr/core/RequestHandlers.java b/solr/core/src/java/org/apache/solr/core/RequestHandlers.javaindex ce6de177dcf..4c8068efff1 100644--- a/solr/core/src/java/org/apache/solr/core/RequestHandlers.java+++ b/solr/core/src/java/org/apache/solr/core/RequestHandlers.java@@ -31,6 +31,7 @@ import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.common.util.StrUtils;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;@@ -178,8 +179,9 @@ public final class RequestHandlers {for (Map.Entry<PluginInfo,SolrRequestHandler> entry : handlers.entrySet()) {PluginInfo info = entry.getKey();SolrRequestHandler requestHandler = entry.getValue();+ info = applyParamSet(config, info);if (requestHandler instanceof PluginInfoInitialized) {- ((PluginInfoInitialized) requestHandler).init(info);+ ((PluginInfoInitialized) requestHandler).init(info);} else{requestHandler.init(info.initArgs);}@@ -190,7 +192,28 @@ public final class RequestHandlers {if(get("") == null)log.warn("no default request handler is registered (either '/select' or 'standard')");}-++ private PluginInfo applyParamSet(SolrConfig config, PluginInfo info) {+ List<ParamSet> paramSets= new ArrayList<>();+ String p = info.attributes.get("paramSet");+ if(p!=null) {+ for (String paramSet : StrUtils.splitSmart(p, ',')) {+ if(config.getParamSets().containsKey(paramSet)) paramSets.add(config.getParamSets().get(paramSet));+ else log.warn("INVALID paramSet {} in requestHandler {}", paramSet, info.toString());+ }+ }+ for (ParamSet paramSet : config.getParamSets().values()) {+ if(paramSet.matchPath(info.name)) paramSets.add(paramSet);+ }+ if(!paramSets.isEmpty()){+ info = new PluginInfo(info.type, info.attributes, info.initArgs.clone(), info.children);+ for (ParamSet paramSet : paramSets) {+ paramSet.apply(info.initArgs);+ }+ }+ return info;+ }+/*** The <code>LazyRequestHandlerWrapper</code> wraps any {@link SolrRequestHandler}.diff --git a/solr/core/src/java/org/apache/solr/core/SolrConfig.java b/solr/core/src/java/org/apache/solr/core/SolrConfig.javaindex 3ab2a711094..17dabd39674 100644--- a/solr/core/src/java/org/apache/solr/core/SolrConfig.java+++ b/solr/core/src/java/org/apache/solr/core/SolrConfig.java@@ -23,6 +23,8 @@ import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.util.Version;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.StrUtils;import org.apache.solr.handler.component.SearchComponent;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.QueryResponseWriter;@@ -57,10 +59,13 @@ import java.util.ArrayList;import java.util.Collections;import java.util.EnumSet;import java.util.HashMap;+import java.util.HashSet;+import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.Locale;import java.util.Map;+import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;@@ -81,7 +86,7 @@ public class SolrConfig extends Config {public static final String DEFAULT_CONF_FILE = "solrconfig.xml";- static enum PluginOpts {+ static enum PluginOpts {MULTI_OK,REQUIRE_NAME,REQUIRE_CLASS,@@ -305,12 +310,27 @@ public class SolrConfig extends Config {"requestDispatcher/@handleSelect", true );addHttpRequestToContext = getBool(- "requestDispatcher/requestParsers/@addHttpRequestToContext", false );+ "requestDispatcher/requestParsers/@addHttpRequestToContext", false );++ loadPluginInfo(ParamSet.class,ParamSet.TYPE);+ List<PluginInfo> paramSetInfos = pluginStore.get(ParamSet.class.getName()) ;+ if(paramSetInfos!=null){+ Map<String,ParamSet> paramSets = new HashMap<>();+ for (PluginInfo p : paramSetInfos) {+ ParamSet paramSet = new ParamSet(p);+ paramSets.put(paramSet.name == null ? String.valueOf(paramSet.hashCode()) : paramSet.name , paramSet );+ }+ this.paramSets = Collections.unmodifiableMap(paramSets);++ }solrRequestParsers = new SolrRequestParsers(this);Config.log.info("Loaded SolrConfig: " + name);}-+ private Map<String,ParamSet> paramSets = Collections.emptyMap();+ public Map<String, ParamSet> getParamSets() {+ return paramSets;+ }protected UpdateHandlerInfo loadUpdatehandlerInfo() {return new UpdateHandlerInfo(get("updateHandler/@class",null),getInt("updateHandler/autoCommit/maxDocs",-1),@@ -607,4 +627,6 @@ public class SolrConfig extends Config {public boolean isEnableRemoteStreams() {return enableRemoteStreams;}++}diff --git a/solr/core/src/java/org/apache/solr/handler/DumpRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/DumpRequestHandler.javaindex f0f144b4941..773af54198b 100644--- a/solr/core/src/java/org/apache/solr/handler/DumpRequestHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/DumpRequestHandler.java@@ -35,6 +35,21 @@ public class DumpRequestHandler extends RequestHandlerBase{// Show paramsrsp.add( "params", req.getParams().toNamedList() );+ String[] returnParams = req.getParams().getParams("param");+ if(returnParams !=null) {+ NamedList params = (NamedList) rsp.getValues().get("params");+ for (String returnParam : returnParams) {+ String[] vals = req.getParams().getParams(returnParam);+ if(vals != null){+ for (String val : vals) {+ params.add(returnParam,val);+ }+ }++ }+ }++ if(Boolean.TRUE.equals( req.getParams().getBool("initArgs"))) rsp.add("initArgs", initArgs);// Write the streams...if( req.getContentStreams() != null ) {diff --git a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.javaindex dcad4022711..18e2ae50a47 100644--- a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java+++ b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java@@ -48,6 +48,7 @@ import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.StrUtils;+import org.apache.solr.core.ParamSet;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.component.HighlightComponent;import org.apache.solr.handler.component.ResponseBuilder;@@ -124,6 +125,17 @@ public class SolrPluginUtils {*/public static void setDefaults(SolrQueryRequest req, SolrParams defaults,SolrParams appends, SolrParams invariants) {+ String paramSetNames = req.getParams().get(ParamSet.TYPE);+ if(paramSetNames !=null){+ for (String name : StrUtils.splitSmart(paramSetNames,',')) {+ ParamSet paramSet = req.getCore().getSolrConfig().getParamSets().get(name);+ if(paramSet!=null){+ if(paramSet.defaults != null) defaults = SolrParams.wrapDefaults(SolrParams.toSolrParams(paramSet.defaults) , defaults);+ if(paramSet.invariants != null) invariants = SolrParams.wrapDefaults(invariants, SolrParams.toSolrParams(paramSet.invariants));+ if(paramSet.appends != null) appends = SolrParams.wrapAppended(appends, SolrParams.toSolrParams(paramSet.appends));+ }+ }+ }SolrParams p = req.getParams();p = SolrParams.wrapDefaults(p, defaults);diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-paramset.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-paramset.xmlnew file mode 100644index 00000000000..be09f04b192--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-paramset.xml@@ -0,0 +1,58 @@+<?xml version="1.0" ?>++<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You under the Apache License, Version 2.0+ (the "License"); you may not use this file except in compliance with+ the License. You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->++<!-- a basic solrconfig that tests can use when they want simple minimal solrconfig/schema+ DO NOT ADD THINGS TO THIS CONFIG! -->+<config>+ <luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>+ <dataDir>${solr.data.dir:}</dataDir>+ <xi:include href="solrconfig.snippet.randomindexconfig.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>+ <directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.RAMDirectoryFactory}"/>+ <requestHandler name="standard" class="solr.StandardRequestHandler"/>+ <requestHandler name="/update" class="solr.UpdateRequestHandler"/>+ <paramSet name="a" path="/dump3,/root/*,/root1/**">+ <lst name="defaults">+ <str name="a">A</str>+ </lst>+ <lst name="invariants">+ <str name="b">B</str>+ </lst>+ <lst name="appends">+ <str name="c">C</str>+ </lst>+ </paramSet>+ <requestHandler name="/dump3" class="DumpRequestHandler"/>+ <requestHandler name="/dump4" class="DumpRequestHandler"/>+ <requestHandler name="/root/dump5" class="DumpRequestHandler"/>+ <requestHandler name="/root1/anotherlevel/dump6" class="DumpRequestHandler"/>+ <requestHandler name="/dump1" class="DumpRequestHandler" paramSet="a"/>+ <requestHandler name="/dump2" class="DumpRequestHandler" paramSet="a">+ <lst name="defaults">+ <str name="a">A1</str>+ </lst>+ <lst name="invariants">+ <str name="b">B1</str>+ </lst>+ <lst name="appends">+ <str name="c">C1</str>+ </lst>+ </requestHandler>+++</config>diff --git a/solr/core/src/test/org/apache/solr/core/TestParamSet.java b/solr/core/src/test/org/apache/solr/core/TestParamSet.javanew file mode 100644index 00000000000..dceedc9ce8e--- /dev/null+++ b/solr/core/src/test/org/apache/solr/core/TestParamSet.java@@ -0,0 +1,80 @@+package org.apache.solr.core;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.request.SolrRequestHandler;+import org.apache.solr.response.SolrQueryResponse;+import org.junit.BeforeClass;+import org.junit.Test;++import java.util.Arrays;++public class TestParamSet extends SolrTestCaseJ4 {+ @BeforeClass+ public static void beforeClass() throws Exception {+ initCore("solrconfig-paramset.xml","schema.xml");+ }+ @Test+ public void testComponentWithParamSet(){++ for (String s : Arrays.asList("/dump1", "/dump3","/root/dump5" , "/root1/anotherlevel/dump6")) {+ SolrRequestHandler handler = h.getCore().getRequestHandler(s);+ SolrQueryResponse rsp = new SolrQueryResponse();+ handler.handleRequest(req("initArgs", "true"), rsp);+ NamedList nl = (NamedList) rsp.getValues().get("initArgs");+ NamedList def = (NamedList) nl.get(PluginInfo.DEFAULTS);+ assertEquals("A", def.get("a"));+ def = (NamedList) nl.get(PluginInfo.INVARIANTS);+ assertEquals("B", def.get("b"));+ def = (NamedList) nl.get(PluginInfo.APPENDS);+ assertEquals("C", def.get("c"));+ }+ }++ @Test+ public void testComponentWithParamSetRequestParam(){+ for (String s : Arrays.asList("/dump4")) {+ SolrRequestHandler handler = h.getCore().getRequestHandler(s);+ SolrQueryResponse rsp = new SolrQueryResponse();+ handler.handleRequest(req("param", "a","param","b" ,"param","c", "paramSet","a"), rsp);+ NamedList def = (NamedList) rsp.getValues().get("params");+ assertEquals("A", def.get("a"));+ assertEquals("B", def.get("b"));+ assertEquals("C", def.get("c"));+ }+ }+ @Test+ public void testComponentWithConflictingParamSet(){+ SolrRequestHandler handler = h.getCore().getRequestHandler("/dump2");+ SolrQueryResponse rsp = new SolrQueryResponse();+ handler.handleRequest(req("initArgs", "true"), rsp);+ NamedList nl = (NamedList) rsp.getValues().get("initArgs");+ NamedList def = (NamedList) nl.get(PluginInfo.DEFAULTS);+ assertEquals("A" ,def.get("a"));+ def = (NamedList) nl.get(PluginInfo.INVARIANTS);+ assertEquals("B1" ,def.get("b"));+ def = (NamedList) nl.get(PluginInfo.APPENDS);+ assertEquals(Arrays.asList("C1","C") ,def.getAll("c"));+ }+++++}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.javaindex 9c1430325a7..489bda4725f 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java@@ -21,6 +21,7 @@ import java.io.Serializable;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;+import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.Map;@@ -384,6 +385,11 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entryreturn sb.toString();}+ public NamedList getImmutableCopy() {+ NamedList copy = clone();+ return new NamedList<>( Collections.unmodifiableList(copy.nvPairs));+ }+/**** Helper class implementing Map.Entry<String, T> to store the key-value