Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-17600.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: SOLR 17600 - Replace MapSerializable with MapWriter
type: changed
authors:
- name: Ivan Šarić
links:
- name: SOLR-17600
url: https://issues.apache.org/jira/browse/SOLR-17600
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.function.Function;
import org.apache.solr.cluster.placement.PlacementPluginFactory;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.NodeConfig;
import org.apache.solr.core.PluginInfo;
Expand Down Expand Up @@ -79,7 +80,7 @@ private static Map<String, Object> readPlugins(final NodeConfig cfg) {
pluginMap.put("class", p.className);

if (p.initArgs.size() > 0) {
Map<String, Object> config = p.initArgs.toMap(new HashMap<>());
Map<String, Object> config = new SimpleOrderedMap<>(p.initArgs);
Comment on lines -82 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, avoid NamedList/SimpleOrderedMap

How about p.initArgs.asMap(0)

pluginMap.put("config", config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.invoke.MethodHandles;
import java.util.HashMap;
import java.util.Locale;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
Expand All @@ -34,6 +33,7 @@
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.params.ModifiableSolrParams;
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.handler.component.ShardHandler;
import org.apache.solr.jersey.JacksonReflectMapWriter;
Expand Down Expand Up @@ -80,8 +80,7 @@ public void call(ClusterState state, ZkNodeProps message, NamedList<Object> resu
final ModifiableSolrParams coreApiParams = new ModifiableSolrParams();
coreApiParams.set(
CoreAdminParams.ACTION, CoreAdminParams.CoreAdminAction.INSTALLCOREDATA.toString());
typedMessage.toMap(new HashMap<>()).forEach((k, v) -> coreApiParams.set(k, v.toString()));

new SimpleOrderedMap<>(typedMessage).forEach((k, v) -> coreApiParams.set(k, v.toString()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typedMessage._forEachEntry here instead
(avoiding senseless intermediate data structure creation).

// Send the core-admin request to each replica in the slice
final ShardHandler shardHandler = ccc.newShardHandler();
shardRequestTracker.sliceCmd(clusterState, coreApiParams, null, installSlice, shardHandler);
Expand Down
12 changes: 6 additions & 6 deletions solr/core/src/java/org/apache/solr/core/ConfigOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import static org.apache.solr.common.util.Utils.toJSONString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.MapSerializable;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.util.StrUtils;
Expand All @@ -33,7 +34,7 @@
* This class encapsulates the config overlay json file. It is immutable and any edit operations
* performed on this gives a new copy of the object with the changed value
*/
public class ConfigOverlay implements MapSerializable {
public class ConfigOverlay implements MapWriter {
private final int version;
private final Map<String, Object> data;
private Map<String, Object> props;
Expand Down Expand Up @@ -233,10 +234,9 @@ public Map<String, Object> getUserProps() {
}

@Override
public Map<String, Object> toMap(Map<String, Object> map) {
map.put(ZNODEVER, version);
map.putAll(data);
return map;
public void writeMap(EntryWriter ew) throws IOException {
ew.put(ZNODEVER, version);
data.forEach(ew::putNoEx);
}

@SuppressWarnings({"unchecked"})
Expand Down
38 changes: 16 additions & 22 deletions solr/core/src/java/org/apache/solr/core/PluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
import static org.apache.solr.schema.FieldType.CLASS_NAME;

import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.ConfigNode;
import org.apache.solr.common.MapSerializable;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.util.DOMConfigNode;
import org.w3c.dom.Node;

/** An Object which represents a Plugin of any type */
public class PluginInfo implements MapSerializable {
public class PluginInfo implements MapWriter {
public final String name, className, type, pkgName;
public final ClassName cName;
public final NamedList<Object> initArgs;
Expand Down Expand Up @@ -193,27 +195,19 @@ public PluginInfo getChild(String type) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Map<String, Object> toMap(Map<String, Object> map) {
map.putAll(attributes);
Map m = map;
if (initArgs != null) m.putAll(initArgs.asMap(3));
if (children != null) {
for (PluginInfo child : children) {
Object old = m.get(child.name);
if (old == null) {
m.put(child.name, child.toMap(new LinkedHashMap<>()));
} else if (old instanceof List list) {
list.add(child.toMap(new LinkedHashMap<>()));
} else {
ArrayList l = new ArrayList();
l.add(old);
l.add(child.toMap(new LinkedHashMap<>()));
m.put(child.name, l);
}
}
@SuppressWarnings("unchecked")
public void writeMap(EntryWriter ew) throws IOException {
new NamedList<>(attributes).writeMap(ew);
if (initArgs != null) {
new SimpleOrderedMap<>(initArgs).writeMap(ew);
}
Comment on lines +200 to +203
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not create a new Map just to ultimately write it!

if (children == null || children.isEmpty()) {
return;
}

for (PluginInfo child : children) {
child.writeMap(ew);
}
return m;
}

/**
Expand Down
27 changes: 15 additions & 12 deletions solr/core/src/java/org/apache/solr/core/RequestParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import java.util.List;
import java.util.Map;
import org.apache.solr.cloud.ZkSolrResourceLoader;
import org.apache.solr.common.MapSerializable;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.params.MultiMapSolrParams;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.Utils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
Expand All @@ -39,7 +40,7 @@
* The class encapsulates the request time parameters . This is immutable and any changes performed
* returns a copy of the Object with the changed values
*/
public class RequestParams implements MapSerializable {
public class RequestParams implements MapWriter {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final Map<String, Object> data;
Expand Down Expand Up @@ -113,8 +114,9 @@ public int getZnodeVersion() {
}

@Override
public Map<String, Object> toMap(Map<String, Object> map) {
return getMapWithVersion(data, znodeVersion);
public void writeMap(EntryWriter ew) throws IOException {
ew.put(ConfigOverlay.ZNODEVER, znodeVersion);
data.forEach(ew::putNoEx);
}

public static Map<String, Object> getMapWithVersion(Map<String, Object> data, int znodeVersion) {
Expand All @@ -130,7 +132,9 @@ public RequestParams setParams(String name, ParamSet paramSet) {
Map p = (Map) deepCopy.get(NAME);
if (p == null) deepCopy.put(NAME, p = new LinkedHashMap<>());
if (paramSet == null) p.remove(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this if should have { } to match the else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that in Java conditionals should always use curly braces for clarity. This is the existing code that was not changed in my PR.

I am hesitant to change it as I am not sure if there is a convention regarding this practice in the project.

else p.put(name, paramSet.toMap(new LinkedHashMap<>()));
else {
p.put(name, new SimpleOrderedMap<>(paramSet));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one's fine

}
return new RequestParams(deepCopy, znodeVersion);
}

Expand Down Expand Up @@ -208,7 +212,7 @@ public byte[] toByteArray() {
public static final String INVARIANTS = "_invariants_";

@SuppressWarnings({"unchecked"})
public static class ParamSet implements MapSerializable {
public static class ParamSet implements MapWriter {
private final Map<String, Object> defaults, appends, invariants;
Map<String, VersionedParams> paramsMap;
public final Map<String, Long> meta;
Expand All @@ -235,12 +239,11 @@ public Long getVersion() {
}

@Override
public Map<String, Object> toMap(Map<String, Object> result) {
result.putAll(defaults);
if (appends != null) result.put(APPENDS, appends);
if (invariants != null) result.put(INVARIANTS, invariants);
if (meta != null) result.put("", meta);
return result;
public void writeMap(EntryWriter ew) throws IOException {
defaults.forEach(ew::putNoEx);
if (appends != null) ew.put(APPENDS, appends);
if (invariants != null) ew.put(INVARIANTS, invariants);
if (meta != null) ew.put("", meta);
}

@SuppressWarnings({"rawtypes"})
Expand Down
Loading