Skip to content

Commit ff60c85

Browse files
authored
SimpleOrderedMap MapWriter constructor (#3235)
* SimpleOrderedMap MapWriter constructor * new SimpleOrderedMap(MapWriter) ex: a SolrParams * deprecate SolrParams.toNamedList * optimize SolrParams.writeMap Bigger picture is avoiding NamedList. * comment for the array side
1 parent eadd49b commit ff60c85

6 files changed

Lines changed: 50 additions & 28 deletions

File tree

solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public IndexFetchResult doFetch(SolrParams solrParams, boolean forceReplication)
461461
if (currentIndexFetcher != null && currentIndexFetcher != pollingIndexFetcher) {
462462
currentIndexFetcher.destroy();
463463
}
464-
currentIndexFetcher = new IndexFetcher(solrParams.toNamedList(), this, core);
464+
currentIndexFetcher = new IndexFetcher(new SimpleOrderedMap<>(solrParams), this, core);
465465
} else {
466466
currentIndexFetcher = pollingIndexFetcher;
467467
}

solr/core/src/test/org/apache/solr/search/TestMinHashQParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.search.ConstantScoreQuery;
2323
import org.apache.lucene.search.Query;
2424
import org.apache.solr.SolrTestCaseJ4;
25+
import org.apache.solr.common.params.ModifiableSolrParams;
2526
import org.apache.solr.common.params.SolrParams;
2627
import org.apache.solr.common.util.NamedList;
2728
import org.apache.solr.request.SolrQueryRequest;
@@ -431,13 +432,12 @@ public void testBandsWrap() throws SyntaxError {
431432

432433
private SolrQueryRequest createRequest(String query) {
433434
SolrQueryRequest qr = req(query);
434-
NamedList<Object> par = qr.getParams().toNamedList();
435-
par.add("debug", "false");
436-
par.add("rows", "30");
437-
par.add("fl", "id,score");
435+
ModifiableSolrParams par = ModifiableSolrParams.of(qr.getParams());
436+
par.set("debug", "false");
437+
par.set("rows", "30");
438+
par.set("fl", "id,score");
438439
par.remove("qt");
439-
SolrParams newp = par.toSolrParams();
440-
qr.setParams(newp);
440+
qr.setParams(par);
441441
return qr;
442442
}
443443
}

solr/core/src/test/org/apache/solr/update/processor/RegexBoostProcessorTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.solr.SolrTestCaseJ4;
2020
import org.apache.solr.common.SolrInputDocument;
2121
import org.apache.solr.common.params.ModifiableSolrParams;
22+
import org.apache.solr.common.util.SimpleOrderedMap;
2223
import org.apache.solr.core.SolrCore;
2324
import org.apache.solr.request.SolrQueryRequest;
2425
import org.apache.solr.response.SolrQueryResponse;
@@ -32,7 +33,7 @@
3233
public class RegexBoostProcessorTest extends SolrTestCaseJ4 {
3334
private static RegexpBoostProcessor reProcessor;
3435
protected static SolrRequestParsers _parser;
35-
protected static ModifiableSolrParams parameters;
36+
protected static SimpleOrderedMap<String> config;
3637
private static RegexpBoostProcessorFactory factory;
3738
private SolrInputDocument document;
3839

@@ -43,13 +44,13 @@ public static void setUpBeforeClass() throws Exception {
4344
SolrCore core = h.getCore();
4445
_parser = new SolrRequestParsers(null);
4546
SolrQueryResponse resp = null;
46-
parameters = new ModifiableSolrParams();
47-
parameters.set(RegexpBoostProcessor.BOOST_FILENAME_PARAM, "regex-boost-processor-test.txt");
48-
parameters.set(RegexpBoostProcessor.INPUT_FIELD_PARAM, "url");
49-
parameters.set(RegexpBoostProcessor.BOOST_FIELD_PARAM, "urlboost");
47+
config = new SimpleOrderedMap<>();
48+
config.add(RegexpBoostProcessor.BOOST_FILENAME_PARAM, "regex-boost-processor-test.txt");
49+
config.add(RegexpBoostProcessor.INPUT_FIELD_PARAM, "url");
50+
config.add(RegexpBoostProcessor.BOOST_FIELD_PARAM, "urlboost");
5051
SolrQueryRequest req = _parser.buildRequestFrom(core, new ModifiableSolrParams(), null);
5152
factory = new RegexpBoostProcessorFactory();
52-
factory.init(parameters.toNamedList());
53+
factory.init(config);
5354
reProcessor = (RegexpBoostProcessor) factory.getInstance(req, resp, null);
5455
}
5556

@@ -58,7 +59,7 @@ public static void tearDownAfterClass() {
5859
// null static members for gc
5960
reProcessor = null;
6061
_parser = null;
61-
parameters = null;
62+
config = null;
6263
factory = null;
6364
}
6465

solr/modules/langid/src/test/org/apache/solr/update/processor/OpenNLPLangDetectUpdateProcessorFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
2121
import org.apache.solr.common.SolrInputDocument;
2222
import org.apache.solr.common.params.ModifiableSolrParams;
23+
import org.apache.solr.common.util.SimpleOrderedMap;
2324
import org.apache.solr.request.SolrQueryRequest;
2425
import org.junit.Test;
2526

@@ -41,7 +42,7 @@ protected OpenNLPLangDetectUpdateProcessor createLangIdProcessor(ModifiableSolrP
4142
}
4243
SolrQueryRequest req = _parser.buildRequestFrom(h.getCore(), new ModifiableSolrParams(), null);
4344
OpenNLPLangDetectUpdateProcessorFactory factory = new OpenNLPLangDetectUpdateProcessorFactory();
44-
factory.init(parameters.toNamedList());
45+
factory.init(new SimpleOrderedMap<>(parameters));
4546
factory.inform(h.getCore());
4647
return (OpenNLPLangDetectUpdateProcessor) factory.getInstance(req, resp, null);
4748
}

solr/solrj/src/java/org/apache/solr/common/params/SolrParams.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,17 @@ public String get(String param, String def) {
6969

7070
@Override
7171
public void writeMap(EntryWriter ew) throws IOException {
72-
// TODO don't call toNamedList; more efficiently implement here
73-
// note: multiple values, if present, are a String[] under 1 key
74-
toNamedList()
75-
.forEach(
76-
(k, v) -> {
77-
if (v == null || "".equals(v)) return;
78-
try {
79-
ew.put(k, v);
80-
} catch (IOException e) {
81-
throw new RuntimeException("Error serializing", e);
82-
}
83-
});
72+
for (Entry<String, String[]> entry : this) {
73+
String[] value = entry.getValue();
74+
// if only one value, don't wrap in an array
75+
if (value.length == 1) {
76+
assert value[0] != null;
77+
ew.put(entry.getKey(), value[0]);
78+
} else if (value.length > 1) {
79+
// values shouldn't be null; not bothering to assert it
80+
ew.put(entry.getKey(), value);
81+
}
82+
}
8483
}
8584

8685
/** Returns an Iterator of {@code Map.Entry} providing a multi-map view. Treat it as read-only. */
@@ -434,7 +433,10 @@ public static SolrParams wrapAppended(SolrParams params, SolrParams defaults) {
434433
/**
435434
* Convert this to a NamedList of unique keys with either String or String[] values depending on
436435
* how many values there are for the parameter.
436+
*
437+
* @deprecated see {@link SimpleOrderedMap#SimpleOrderedMap(MapWriter)}
437438
*/
439+
@Deprecated
438440
public NamedList<Object> toNamedList() {
439441
final SimpleOrderedMap<Object> result = new SimpleOrderedMap<>();
440442

solr/solrj/src/java/org/apache/solr/common/util/SimpleOrderedMap.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.solr.common.util;
1818

19+
import java.io.IOException;
1920
import java.util.AbstractMap;
2021
import java.util.AbstractSet;
2122
import java.util.ArrayList;
@@ -24,6 +25,8 @@
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Set;
28+
import org.apache.solr.common.MapWriter;
29+
import org.apache.solr.common.params.SolrParams;
2730

2831
/**
2932
* <code>SimpleOrderedMap</code> is a {@link NamedList} where access by key is more important than
@@ -67,7 +70,22 @@ public SimpleOrderedMap(Map.Entry<String, T>[] nameValuePairs) {
6770
super(nameValuePairs);
6871
}
6972

70-
// TODO override asShallowMap in Solr 10
73+
/** Can convert a {@link SolrParams} and other things. */
74+
public SimpleOrderedMap(MapWriter mapWriter) {
75+
try {
76+
mapWriter.writeMap(
77+
new EntryWriter() {
78+
@SuppressWarnings("unchecked")
79+
@Override
80+
public EntryWriter put(CharSequence k, Object v) throws IOException {
81+
SimpleOrderedMap.this.add(k.toString(), (T) v);
82+
return this;
83+
}
84+
});
85+
} catch (IOException e) {
86+
throw new RuntimeException(e); // impossible?
87+
}
88+
}
7189

7290
@Override
7391
public SimpleOrderedMap<T> clone() {

0 commit comments

Comments
 (0)