Apache Lucene open-source search software
SOLR-9657: Use cache for templates
Noble Paul127bf9f77246
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/VariableResolver.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/VariableResolver.javaindex f255657baa6..51b5841aea6 100644--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/VariableResolver.java+++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/VariableResolver.java@@ -25,6 +25,8 @@ import java.util.function.Function;import java.util.regex.Matcher;import java.util.regex.Pattern;+import org.apache.solr.common.util.Cache;+import org.apache.solr.common.util.MapBackedCache;import org.apache.solr.update.processor.TemplateUpdateProcessorFactory;import static org.apache.solr.update.processor.TemplateUpdateProcessorFactory.Resolved;@@ -55,7 +57,7 @@ public class VariableResolver {.compile("^(\\w*?)\\((.*?)\\)$");private Map<String,Object> rootNamespace;private Map<String,Evaluator> evaluators;- private Map<String,Resolved> cache = new WeakHashMap<>();+ private Cache<String,Resolved> cache = new MapBackedCache<>(new WeakHashMap<>());private Function<String,Object> fun = this::resolve;public static final String FUNCTIONS_NAMESPACE = "dataimporter.functions.";diff --git a/solr/core/src/java/org/apache/solr/update/processor/TemplateUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/TemplateUpdateProcessorFactory.javaindex dbe61b42cc7..b791d3b4ffd 100644--- a/solr/core/src/java/org/apache/solr/update/processor/TemplateUpdateProcessorFactory.java+++ b/solr/core/src/java/org/apache/solr/update/processor/TemplateUpdateProcessorFactory.java@@ -20,18 +20,21 @@ package org.apache.solr.update.processor;import java.util.ArrayList;import java.util.Collections;import java.util.List;-import java.util.Map;import java.util.function.Function;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.common.util.Cache;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.update.AddUpdateCommand;+import org.apache.solr.util.ConcurrentLRUCache;+//Adds new fields to documents based on a template pattern specified via Template.field// request parameters (multi-valued) or 'field' value specified in initArgspublic class TemplateUpdateProcessorFactory extends SimpleUpdateProcessorFactory {+ private Cache<String, Resolved> templateCache = new ConcurrentLRUCache<>(1000, 800, 900, 10, false, false, null);@Overrideprotected void process(AddUpdateCommand cmd, SolrQueryRequest req, SolrQueryResponse rsp) {String[] vals = getParams("field");@@ -45,7 +48,7 @@ public class TemplateUpdateProcessorFactory extends SimpleUpdateProcessorFactoryString fName = val.substring(0, idx);String template = val.substring(idx + 1);- doc.addField(fName, replaceTokens(template, null, s -> {+ doc.addField(fName, replaceTokens(template, templateCache, s -> {Object v = doc.getFieldValue(s);return v == null ? "" : v;}));@@ -55,7 +58,7 @@ public class TemplateUpdateProcessorFactory extends SimpleUpdateProcessorFactory}- public static Resolved getResolved(String template, Map<String, Resolved> cache) {+ public static Resolved getResolved(String template, Cache<String, Resolved> cache) {Resolved r = cache == null ? null : cache.get(template);if (r == null) {r = new Resolved();@@ -74,7 +77,7 @@ public class TemplateUpdateProcessorFactory extends SimpleUpdateProcessorFactory/*** Get a list of variables embedded in the template string.*/- public static List<String> getVariables(String template, Map<String, Resolved> cache) {+ public static List<String> getVariables(String template, Cache<String, Resolved> cache) {Resolved r = getResolved(template, cache);if (r == null) {return Collections.emptyList();@@ -82,7 +85,7 @@ public class TemplateUpdateProcessorFactory extends SimpleUpdateProcessorFactoryreturn new ArrayList<>(r.variables);}- public static String replaceTokens(String template, Map<String, Resolved> cache, Function<String, Object> fun) {+ public static String replaceTokens(String template, Cache<String, Resolved> cache, Function<String, Object> fun) {if (template == null) {return null;}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Cache.java b/solr/solrj/src/java/org/apache/solr/common/util/Cache.javaindex 7be7f0cc700..2ec4d7e2965 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/Cache.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/Cache.java@@ -16,13 +16,31 @@*/package org.apache.solr.common.util;+import java.util.Objects;+import java.util.function.Function;+public interface Cache<K, V> {- public V put(K key, V val);+ V put(K key, V val);++ V get(K key);++ V remove(K key);- public V get(K key);+ void clear();- public V remove(K key);+ default V computeIfAbsent(K key,+ Function<? super K, ? extends V> mappingFunction) {+ Objects.requireNonNull(mappingFunction);+ V v;+ if ((v = get(key)) == null) {+ V newValue;+ if ((newValue = mappingFunction.apply(key)) != null) {+ put(key, newValue);+ return newValue;+ }+ }- public void clear();+ return v;+ }}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java b/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.javanew file mode 100644index 00000000000..1aba20bc8ae--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java@@ -0,0 +1,57 @@+/*+ * 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.+ */++package org.apache.solr.common.util;+++import java.util.Map;+import java.util.function.Function;++// A cache backed by a map+public class MapBackedCache<K, V> implements Cache<K, V> {++ private final Map<K, V> map;++ public MapBackedCache(Map<K, V> map) {+ this.map = map;+ }++ @Override+ public V put(K key, V val) {+ return map.put(key, val);+ }++ @Override+ public V get(K key) {+ return map.get(key);+ }++ @Override+ public V remove(K key) {+ return map.remove(key);+ }++ @Override+ public void clear() {+ map.clear();+ }++ @Override+ public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {+ return map.computeIfAbsent(key, mappingFunction);+ }+}diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.javaindex 96ddc8b223f..e2229c59f7f 100644--- a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java@@ -340,30 +340,7 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {assertTrue(l1.get(1).equals(l2.get(1)));assertFalse(l1.get(1) == l2.get(1));- JavaBinCodec.StringCache stringCache = new JavaBinCodec.StringCache(new Cache<JavaBinCodec.StringBytes, String>() {- private HashMap<JavaBinCodec.StringBytes, String> cache = new HashMap<>();-- @Override- public String put(JavaBinCodec.StringBytes key, String val) {- return cache.put(key, val);- }-- @Override- public String get(JavaBinCodec.StringBytes key) {- return cache.get(key);- }-- @Override- public String remove(JavaBinCodec.StringBytes key) {- return cache.remove(key);- }-- @Override- public void clear() {- cache.clear();-- }- });+ JavaBinCodec.StringCache stringCache = new JavaBinCodec.StringCache(new MapBackedCache<>(new HashMap<>()));m1 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b1));@@ -409,32 +386,7 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {Runtime.getRuntime().gc();printMem("before cache init");- Cache<JavaBinCodec.StringBytes, String> cache1 = new Cache<JavaBinCodec.StringBytes, String>() {- private HashMap<JavaBinCodec.StringBytes, String> cache = new HashMap<>();-- @Override- public String put(JavaBinCodec.StringBytes key, String val) {- l.add(key);- return cache.put(key, val);-- }-- @Override- public String get(JavaBinCodec.StringBytes key) {- return cache.get(key);- }-- @Override- public String remove(JavaBinCodec.StringBytes key) {- return cache.remove(key);- }-- @Override- public void clear() {- cache.clear();-- }- };+ Cache<JavaBinCodec.StringBytes, String> cache1 = new MapBackedCache<>(new HashMap<>()) ;final JavaBinCodec.StringCache STRING_CACHE = new JavaBinCodec.StringCache(cache1);// STRING_CACHE = new JavaBinCodec.StringCache(cache);