From 5de63322098e21438e734dc918040dc8d78122ac Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Mon, 22 Oct 2018 11:14:41 +1100 Subject: [PATCH] SOLR-12892: MapWriter to use CharSequence instead of String --- solr/CHANGES.txt | 2 ++ .../cloud/autoscaling/AutoScalingHandler.java | 4 ++-- .../apache/solr/update/TransactionLog.java | 4 ++-- .../solrj/cloud/autoscaling/PolicyHelper.java | 4 ++-- .../solrj/cloud/autoscaling/ReplicaInfo.java | 2 +- .../solr/common/ConditionalMapWriter.java | 20 ++++++++-------- .../org/apache/solr/common/MapWriter.java | 24 +++++++++---------- .../apache/solr/common/util/JavaBinCodec.java | 16 ++++++------- .../solr/common/util/JsonTextWriter.java | 6 ++--- .../org/apache/solr/common/util/Utils.java | 8 +++---- .../solrj/cloud/autoscaling/TestPolicy.java | 8 +++---- 11 files changed, 50 insertions(+), 48 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index d7cf95cc982..342d68c9bb0 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -214,6 +214,8 @@ Improvements * SOLR-10981: Support for stream.url or stream.file pointing to gzipped data. It's detected by either a content encoding header or file extension. (Andrew Lundgren via David Smiley, Jan Høydahl) +* SOLR-12892: MapWriter to use CharSequence instead of String (noble) + ================== 7.5.0 ================== Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java index 899c5cd4288..1ef6c26c384 100644 --- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java +++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoScalingHandler.java @@ -124,8 +124,8 @@ public class AutoScalingHandler extends RequestHandlerBase implements Permission autoScalingConf.writeMap(new MapWriter.EntryWriter() { @Override - public MapWriter.EntryWriter put(String k, Object v) throws IOException { - rsp.getValues().add(k, v); + public MapWriter.EntryWriter put(CharSequence k, Object v) { + rsp.getValues().add(k.toString(), v); return this; } }); diff --git a/solr/core/src/java/org/apache/solr/update/TransactionLog.java b/solr/core/src/java/org/apache/solr/update/TransactionLog.java index 9bb08788b9c..b11f76a780f 100644 --- a/solr/core/src/java/org/apache/solr/update/TransactionLog.java +++ b/solr/core/src/java/org/apache/solr/update/TransactionLog.java @@ -107,14 +107,14 @@ public class TransactionLog implements Closeable { } @Override - public void writeExternString(String s) throws IOException { + public void writeExternString(CharSequence s) throws IOException { if (s == null) { writeTag(NULL); return; } // no need to synchronize globalStringMap - it's only updated before the first record is written to the log - Integer idx = globalStringMap.get(s); + Integer idx = globalStringMap.get(s.toString()); if (idx == null) { // write a normal string writeStr(s); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/PolicyHelper.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/PolicyHelper.java index 70bc4139642..c661d952c37 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/PolicyHelper.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/PolicyHelper.java @@ -203,8 +203,8 @@ public class PolicyHelper { public static MapWriter getDiagnostics(Policy.Session session) { List sorted = session.getSortedNodes(); - Set alreadyWritten = new HashSet<>(); - BiPredicate p = dedupeKeyPredicate(alreadyWritten) + Set alreadyWritten = new HashSet<>(); + BiPredicate p = dedupeKeyPredicate(alreadyWritten) .and(ConditionalMapWriter.NON_NULL_VAL) .and((s, o) -> !(o instanceof Map) || !((Map) o).isEmpty()); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/ReplicaInfo.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/ReplicaInfo.java index ca83ad403fc..9c5cf316a2f 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/ReplicaInfo.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/ReplicaInfo.java @@ -93,7 +93,7 @@ public class ReplicaInfo implements MapWriter { @Override public void writeMap(EntryWriter ew) throws IOException { - BiPredicate p = dedupeKeyPredicate(new HashSet<>()) + BiPredicate p = dedupeKeyPredicate(new HashSet<>()) .and(NON_NULL_VAL); ew.put(name, (MapWriter) ew1 -> { ew1.put(ZkStateReader.CORE_NAME_PROP, core, p) diff --git a/solr/solrj/src/java/org/apache/solr/common/ConditionalMapWriter.java b/solr/solrj/src/java/org/apache/solr/common/ConditionalMapWriter.java index d351fc2789d..1b6b03e3dac 100644 --- a/solr/solrj/src/java/org/apache/solr/common/ConditionalMapWriter.java +++ b/solr/solrj/src/java/org/apache/solr/common/ConditionalMapWriter.java @@ -23,9 +23,9 @@ import java.util.function.BiPredicate; public class ConditionalMapWriter implements MapWriter { private final MapWriter delegate; - private final BiPredicate predicate; + private final BiPredicate predicate; - public ConditionalMapWriter(MapWriter delegate, BiPredicate predicate) { + public ConditionalMapWriter(MapWriter delegate, BiPredicate predicate) { this.delegate = delegate; this.predicate = predicate; } @@ -38,33 +38,33 @@ public class ConditionalMapWriter implements MapWriter { } @Override - public EntryWriter put(String k, Object v) throws IOException { + public EntryWriter put(CharSequence k, Object v) throws IOException { if (predicate.test(k, v)) delegate.put(k, v); return this; } @Override - public EntryWriter put(String k, int v) throws IOException { + public EntryWriter put(CharSequence k, int v) throws IOException { return put(k, Integer.valueOf(v)); } @Override - public EntryWriter put(String k, long v) throws IOException { + public EntryWriter put(CharSequence k, long v) throws IOException { return put(k, Long.valueOf(v)); } @Override - public EntryWriter put(String k, float v) throws IOException { + public EntryWriter put(CharSequence k, float v) throws IOException { return put(k, Float.valueOf(v)); } @Override - public EntryWriter put(String k, double v) throws IOException { + public EntryWriter put(CharSequence k, double v) throws IOException { return put(k, Double.valueOf(v)); } @Override - public EntryWriter put(String k, boolean v) throws IOException { + public EntryWriter put(CharSequence k, boolean v) throws IOException { return put(k, Boolean.valueOf(v)); } } @@ -74,9 +74,9 @@ public class ConditionalMapWriter implements MapWriter { if(delegate!=null) delegate.writeMap(new EntryWriterWrapper(ew)); } - public static BiPredicate dedupeKeyPredicate(Set keys) { + public static BiPredicate dedupeKeyPredicate(Set keys) { return (k, v) -> keys.add(k); } - public static final BiPredicate NON_NULL_VAL = (s, o) -> o != null; + public static final BiPredicate NON_NULL_VAL = (s, o) -> o != null; } diff --git a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java index 9edd630d0d6..d2f14d8480b 100644 --- a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java +++ b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java @@ -44,7 +44,7 @@ public interface MapWriter extends MapSerializable { try { writeMap(new EntryWriter() { @Override - public EntryWriter put(String k, Object v) { + public EntryWriter put(CharSequence k, Object v) { if (v instanceof MapWriter) v = ((MapWriter) v).toMap(new LinkedHashMap<>()); if (v instanceof IteratorWriter) v = ((IteratorWriter) v).toList(new ArrayList<>()); if (v instanceof Iterable) { @@ -66,7 +66,7 @@ public interface MapWriter extends MapSerializable { } v = map; } - map.put(k, v); + map.put(k==null? null : k.toString(), v); // note: It'd be nice to assert that there is no previous value at 'k' but it's possible the passed in // map is already populated and the intention is to overwrite. return this; @@ -139,8 +139,8 @@ public interface MapWriter extends MapSerializable { * @param k The key * @param v The value can be any supported object */ - EntryWriter put(String k, Object v) throws IOException; - default EntryWriter putNoEx(String k, Object v) { + EntryWriter put(CharSequence k, Object v) throws IOException; + default EntryWriter putNoEx(CharSequence k, Object v) { try { put(k,v); } catch (IOException e) { @@ -149,45 +149,45 @@ public interface MapWriter extends MapSerializable { return this; } - default EntryWriter put(String k, Object v, BiPredicate p) throws IOException { + default EntryWriter put(CharSequence k, Object v, BiPredicate p) throws IOException { if (p.test(k,v)) put(k, v); return this; } - default EntryWriter putIfNotNull(String k, Object v) throws IOException { + default EntryWriter putIfNotNull(CharSequence k, Object v) throws IOException { if(v != null) put(k,v); return this; } - default EntryWriter putStringIfNotNull(String k, Object v) throws IOException { + default EntryWriter putStringIfNotNull(CharSequence k, Object v) throws IOException { if(v != null) put(k,String.valueOf(v)); return this; } - default EntryWriter put(String k, int v) throws IOException { + default EntryWriter put(CharSequence k, int v) throws IOException { put(k, (Integer) v); return this; } - default EntryWriter put(String k, long v) throws IOException { + default EntryWriter put(CharSequence k, long v) throws IOException { put(k, (Long) v); return this; } - default EntryWriter put(String k, float v) throws IOException { + default EntryWriter put(CharSequence k, float v) throws IOException { put(k, (Float) v); return this; } - default EntryWriter put(String k, double v) throws IOException { + default EntryWriter put(CharSequence k, double v) throws IOException { put(k, (Double) v); return this; } - default EntryWriter put(String k, boolean v) throws IOException { + default EntryWriter put(CharSequence k, boolean v) throws IOException { put(k, (Boolean) v); return this; } diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java index 8f86e4cac11..4435e189af0 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java @@ -405,42 +405,42 @@ public class JavaBinCodec implements PushWriter { private final MapWriter.EntryWriter ew = new MapWriter.EntryWriter() { @Override - public MapWriter.EntryWriter put(String k, Object v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, Object v) throws IOException { writeExternString(k); JavaBinCodec.this.writeVal(v); return this; } @Override - public MapWriter.EntryWriter put(String k, int v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, int v) throws IOException { writeExternString(k); JavaBinCodec.this.writeInt(v); return this; } @Override - public MapWriter.EntryWriter put(String k, long v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, long v) throws IOException { writeExternString(k); JavaBinCodec.this.writeLong(v); return this; } @Override - public MapWriter.EntryWriter put(String k, float v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, float v) throws IOException { writeExternString(k); JavaBinCodec.this.writeFloat(v); return this; } @Override - public MapWriter.EntryWriter put(String k, double v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, double v) throws IOException { writeExternString(k); JavaBinCodec.this.writeDouble(v); return this; } @Override - public MapWriter.EntryWriter put(String k, boolean v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, boolean v) throws IOException { writeExternString(k); writeBoolean(v); return this; @@ -1062,7 +1062,7 @@ public class JavaBinCodec implements PushWriter { private Map stringsMap; private List stringsList; - public void writeExternString(String s) throws IOException { + public void writeExternString(CharSequence s) throws IOException { if (s == null) { writeTag(NULL); return; @@ -1073,7 +1073,7 @@ public class JavaBinCodec implements PushWriter { if (idx == 0) { writeStr(s); if (stringsMap == null) stringsMap = new HashMap<>(); - stringsMap.put(s, ++stringsCount); + stringsMap.put(s.toString(), ++stringsCount); } } diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JsonTextWriter.java b/solr/solrj/src/java/org/apache/solr/common/util/JsonTextWriter.java index e176bd16e9a..ed8c79776f0 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JsonTextWriter.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JsonTextWriter.java @@ -165,15 +165,15 @@ public interface JsonTextWriter extends TextWriter { boolean isFirst = true; @Override - public MapWriter.EntryWriter put(String k, Object v) throws IOException { + public MapWriter.EntryWriter put(CharSequence k, Object v) throws IOException { if (isFirst) { isFirst = false; } else { JsonTextWriter.this.writeMapSeparator(); } JsonTextWriter.this.indent(); - JsonTextWriter.this.writeKey(k, true); - writeVal(k, v); + JsonTextWriter.this.writeKey(k.toString(), true); + writeVal(k.toString(), v); return this; } }); diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java index 095ef57c01e..bdfd03eea88 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java @@ -123,7 +123,7 @@ public class Utils { try { m.writeMap(new MapWriter.EntryWriter() { @Override - public MapWriter.EntryWriter put(String k, Object v) { + public MapWriter.EntryWriter put(CharSequence k, Object v) { fun.accept(k, v); return this; } @@ -471,8 +471,8 @@ public class Utils { } - static class MapWriterEntry extends AbstractMap.SimpleEntry implements MapWriter, Map.Entry { - MapWriterEntry(String key, V value) { + static class MapWriterEntry extends AbstractMap.SimpleEntry implements MapWriter, Map.Entry { + MapWriterEntry(CharSequence key, V value) { super(key, value); } @@ -495,7 +495,7 @@ public class Utils { ((MapWriter) obj).writeMap(new MapWriter.EntryWriter() { int count = -1; @Override - public MapWriter.EntryWriter put(String k, Object v) { + public MapWriter.EntryWriter put(CharSequence k, Object v) { if (result[0] != null) return this; if (idx < 0) { if (k.equals(key)) result[0] = v; diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java index 64ba3575c33..7b325676794 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java @@ -2661,8 +2661,8 @@ public class TestPolicy extends SolrTestCaseJ4 { Set writtenKeys = new HashSet<>(); policy.writeMap(new MapWriter.EntryWriter() { @Override - public MapWriter.EntryWriter put(String k, Object v) throws IOException { - writtenKeys.add(k); + public MapWriter.EntryWriter put(CharSequence k, Object v) throws IOException { + writtenKeys.add(k.toString()); return this; } }); @@ -2679,8 +2679,8 @@ public class TestPolicy extends SolrTestCaseJ4 { assertEquals(policy.getClusterPreferences().size(), defaultPreferences.size()); policy.writeMap(new MapWriter.EntryWriter() { @Override - public MapWriter.EntryWriter put(String k, Object v) throws IOException { - writtenKeys.add(k); + public MapWriter.EntryWriter put(CharSequence k, Object v) throws IOException { + writtenKeys.add(k.toString()); return this; } });