SOLR-12892: MapWriter to use CharSequence instead of String

This commit is contained in:
Noble Paul 2018-10-22 11:14:41 +11:00
parent a7c9c9d8ce
commit 5de6332209
11 changed files with 50 additions and 48 deletions

View File

@ -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.

View File

@ -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;
}
});

View File

@ -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);

View File

@ -203,8 +203,8 @@ public class PolicyHelper {
public static MapWriter getDiagnostics(Policy.Session session) {
List<Row> sorted = session.getSortedNodes();
Set<String> alreadyWritten = new HashSet<>();
BiPredicate<String, Object> p = dedupeKeyPredicate(alreadyWritten)
Set<CharSequence> alreadyWritten = new HashSet<>();
BiPredicate<CharSequence, Object> p = dedupeKeyPredicate(alreadyWritten)
.and(ConditionalMapWriter.NON_NULL_VAL)
.and((s, o) -> !(o instanceof Map) || !((Map) o).isEmpty());

View File

@ -93,7 +93,7 @@ public class ReplicaInfo implements MapWriter {
@Override
public void writeMap(EntryWriter ew) throws IOException {
BiPredicate<String, Object> p = dedupeKeyPredicate(new HashSet<>())
BiPredicate<CharSequence, Object> p = dedupeKeyPredicate(new HashSet<>())
.and(NON_NULL_VAL);
ew.put(name, (MapWriter) ew1 -> {
ew1.put(ZkStateReader.CORE_NAME_PROP, core, p)

View File

@ -23,9 +23,9 @@ import java.util.function.BiPredicate;
public class ConditionalMapWriter implements MapWriter {
private final MapWriter delegate;
private final BiPredicate<String, Object> predicate;
private final BiPredicate<CharSequence, Object> predicate;
public ConditionalMapWriter(MapWriter delegate, BiPredicate<String, Object> predicate) {
public ConditionalMapWriter(MapWriter delegate, BiPredicate<CharSequence, Object> 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<String, Object> dedupeKeyPredicate(Set<String> keys) {
public static BiPredicate<CharSequence, Object> dedupeKeyPredicate(Set<CharSequence> keys) {
return (k, v) -> keys.add(k);
}
public static final BiPredicate<String, Object> NON_NULL_VAL = (s, o) -> o != null;
public static final BiPredicate<CharSequence, Object> NON_NULL_VAL = (s, o) -> o != null;
}

View File

@ -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<String, Object> p) throws IOException {
default EntryWriter put(CharSequence k, Object v, BiPredicate<CharSequence, Object> 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;
}

View File

@ -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<String, Integer> stringsMap;
private List<String> 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);
}
}

View File

@ -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;
}
});

View File

@ -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<V> extends AbstractMap.SimpleEntry<String, V> implements MapWriter, Map.Entry<String, V> {
MapWriterEntry(String key, V value) {
static class MapWriterEntry<V> extends AbstractMap.SimpleEntry<CharSequence, V> implements MapWriter, Map.Entry<CharSequence, V> {
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;

View File

@ -2661,8 +2661,8 @@ public class TestPolicy extends SolrTestCaseJ4 {
Set<String> 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;
}
});