mirror of https://github.com/apache/lucene.git
SOLR-12455: Use the new serializarion in V2 and schema requests
This commit is contained in:
parent
a06256ccee
commit
b7e9fb43d0
|
@ -19,7 +19,6 @@ package org.apache.solr.client.solrj.request;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -68,17 +67,13 @@ public class V2Request extends SolrRequest<V2Response> implements MapWriter {
|
|||
if (payload instanceof String) {
|
||||
return new RequestWriter.StringPayloadContentWriter((String) payload, JSON_MIME);
|
||||
}
|
||||
if (payload instanceof Map) {
|
||||
payload = Utils.getDeepCopy((Map) payload, 5);
|
||||
}
|
||||
return new RequestWriter.ContentWriter() {
|
||||
@Override
|
||||
public void write(OutputStream os) throws IOException {
|
||||
if (useBinary) {
|
||||
new JavaBinCodec().marshal(payload, os);
|
||||
} else {
|
||||
byte[] b = Utils.toJSON(payload);
|
||||
os.write(b);
|
||||
Utils.toJSON(payload, os, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,13 +16,9 @@
|
|||
*/
|
||||
package org.apache.solr.client.solrj.request.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.solr.client.solrj.SolrRequest;
|
||||
import org.apache.solr.client.solrj.SolrResponse;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.common.util.ContentStream;
|
||||
|
||||
public abstract class AbstractSchemaRequest<T extends SolrResponse> extends SolrRequest<T> {
|
||||
private SolrParams params = null;
|
||||
|
@ -41,8 +37,4 @@ public abstract class AbstractSchemaRequest<T extends SolrResponse> extends Solr
|
|||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ContentStream> getContentStreams() throws IOException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,10 +18,6 @@ package org.apache.solr.client.solrj.request.schema;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -31,11 +27,8 @@ import org.apache.solr.client.solrj.request.RequestWriter;
|
|||
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.common.util.ContentStream;
|
||||
import org.apache.solr.common.util.ContentStreamBase;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
import org.noggit.CharArr;
|
||||
import org.noggit.JSONWriter;
|
||||
import org.apache.solr.common.util.Utils;
|
||||
|
||||
/**
|
||||
* <p>This class offers access to the operations exposed by the Solr Schema API.</p>
|
||||
|
@ -709,23 +702,13 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
|
|||
|
||||
protected abstract NamedList<Object> getRequestParameters();
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Collection<ContentStream> getContentStreams() throws IOException {
|
||||
CharArr json = new CharArr();
|
||||
new SchemaRequestJSONWriter(json).write(getRequestParameters());
|
||||
return Collections.singletonList(new ContentStreamBase.StringStream(json.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
|
||||
return new RequestWriter.ContentWriter() {
|
||||
@Override
|
||||
public void write(OutputStream os) throws IOException {
|
||||
//TODO : find a way to do streaming write
|
||||
CharArr json = new CharArr();
|
||||
new SchemaRequestJSONWriter(json).write(getRequestParameters());
|
||||
os.write(json.toString().getBytes(StandardCharsets.UTF_8));
|
||||
Utils.toJSON(getRequestParameters(),
|
||||
os, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -794,62 +777,4 @@ public class SchemaRequest extends AbstractSchemaRequest<SchemaResponse> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple extension of the noggit JSONWriter used to be write objects
|
||||
* of type {@link NamedList}.
|
||||
* Writing of objects of the type {@link NamedList} is done in very much
|
||||
* the same way as for a map.
|
||||
* <p>
|
||||
* This writer is particularly useful when doing multiple update requests.
|
||||
* In update Schema API there can be done multiple add operations of the same
|
||||
* type (e.g. : add-field-type), they are grouped in an associative array, even though
|
||||
* this can't be done normally in JSON. For such a use-case, the {@link NamedList}
|
||||
* objects are particularly useful because they can group key-value mappings
|
||||
* having the same values for the keys (unlike maps).
|
||||
*/
|
||||
private static class SchemaRequestJSONWriter extends JSONWriter {
|
||||
public SchemaRequestJSONWriter(CharArr out, int indentSize) {
|
||||
super(out, indentSize);
|
||||
}
|
||||
|
||||
public SchemaRequestJSONWriter(CharArr out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
public void write(Object o) {
|
||||
if (o instanceof NamedList) {
|
||||
write((NamedList) o);
|
||||
} else super.write(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #write(Map)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void write(NamedList<?> val) {
|
||||
this.startObject();
|
||||
int sz = val.size();
|
||||
boolean first = true;
|
||||
Iterator i$ = val.iterator();
|
||||
|
||||
while (i$.hasNext()) {
|
||||
Map.Entry<String, ?> entry = (Map.Entry<String, ?>) i$.next();
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
this.writeValueSeparator();
|
||||
}
|
||||
|
||||
if (sz > 1) {
|
||||
this.indent();
|
||||
}
|
||||
|
||||
this.writeString(entry.getKey());
|
||||
this.writeNameSeparator();
|
||||
this.write(entry.getValue());
|
||||
}
|
||||
|
||||
this.endObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ package org.apache.solr.common.util;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
@ -63,6 +65,7 @@ import org.noggit.ObjectBuilder;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static java.util.Collections.unmodifiableList;
|
||||
import static java.util.Collections.unmodifiableSet;
|
||||
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
||||
|
@ -135,6 +138,15 @@ public class Utils {
|
|||
return mutable ? result : result instanceof Set ? unmodifiableSet((Set) result) : unmodifiableList((List) result);
|
||||
}
|
||||
|
||||
public static void toJSON(Object o, OutputStream os, boolean indent) throws IOException {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(os, UTF_8);
|
||||
new SolrJSONWriter(writer)
|
||||
.setIndent(indent)
|
||||
.writeObj(o)
|
||||
.close();
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public static byte[] toJSON(Object o) {
|
||||
if(o == null) return new byte[0];
|
||||
CharArr out = new CharArr();
|
||||
|
|
|
@ -56,6 +56,7 @@ import org.apache.solr.common.util.JsonTextWriter;
|
|||
import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.common.util.ObjectCache;
|
||||
import org.apache.solr.common.util.Pair;
|
||||
import org.apache.solr.common.util.SolrJSONWriter;
|
||||
import org.apache.solr.common.util.TimeSource;
|
||||
import org.apache.solr.common.util.Utils;
|
||||
import org.apache.solr.common.util.ValidatingJsonMap;
|
||||
|
@ -2526,14 +2527,18 @@ public void testUtilizeNodeFailure2() throws Exception {
|
|||
StringWriter writer = new StringWriter();
|
||||
NamedList<Object> val = new NamedList<>();
|
||||
val.add("violations", violations);
|
||||
new SolrJSONWriter(writer)
|
||||
.writeObj(val)
|
||||
.close();
|
||||
JSONWriter.write (writer, true, JsonTextWriter.JSON_NL_MAP, val);
|
||||
|
||||
Object root = Utils.fromJSONString(writer.toString());
|
||||
assertEquals(2l,
|
||||
Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/NRT"));
|
||||
Utils.getObjectByPath(root, true, "violations[0]/violation/replica/NRT"));
|
||||
assertEquals(0l,
|
||||
Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/PULL"));
|
||||
Utils.getObjectByPath(root, true, "violations[0]/violation/replica/PULL"));
|
||||
assertEquals(0l,
|
||||
Utils.getObjectByPath(Utils.fromJSONString(writer.toString()), true,"violations[0]/violation/replica/TLOG"));
|
||||
Utils.getObjectByPath(root, true, "violations[0]/violation/replica/TLOG"));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue