SOLR-11380: V2request also converted to do streaming write

This commit is contained in:
Noble Paul 2017-11-02 17:39:20 +10:30
parent aa0286540f
commit 0e738a0369
5 changed files with 39 additions and 24 deletions

View File

@ -16,9 +16,6 @@
*/
package org.apache.solr.client.solrj;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
@ -27,6 +24,10 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import static java.util.Collections.unmodifiableSet;
/**
@ -155,6 +156,10 @@ public abstract class SolrRequest<T extends SolrResponse> implements Serializabl
public abstract Collection<ContentStream> getContentStreams() throws IOException;
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
return null;
}
/**
* Create a new SolrResponse to hold the response from the server
* @param client the {@link SolrClient} the request will be sent to

View File

@ -53,7 +53,7 @@ public class BinaryRequestWriter extends RequestWriter {
}
};
} else {
return null;
return req.getContentWriter("application/javabin");
}
}

View File

@ -70,7 +70,7 @@ public class RequestWriter {
}
};
}
return null;
return req.getContentWriter(ClientUtils.TEXT_XML);
}
public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException {

View File

@ -17,12 +17,10 @@
package org.apache.solr.client.solrj.request;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -33,7 +31,7 @@ import org.apache.solr.client.solrj.response.V2Response;
import org.apache.solr.common.MapWriter;
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.JavaBinCodec;
import org.apache.solr.common.util.Utils;
public class V2Request extends SolrRequest<V2Response> implements MapWriter {
@ -63,23 +61,34 @@ public class V2Request extends SolrRequest<V2Response> implements MapWriter {
}
@Override
public Collection<ContentStream> getContentStreams() throws IOException {
public RequestWriter.ContentWriter getContentWriter(String s) {
if (v2Calls.get() != null) v2Calls.get().incrementAndGet();
if (payload != null) {
return Collections.singleton(new ContentStreamBase() {
@Override
public InputStream getStream() throws IOException {
if(payload instanceof InputStream) return (InputStream) payload;
if (useBinary) return Utils.toJavabin(payload);
else return new ByteArrayInputStream(Utils.toJSON(payload));
if (payload == null) return null;
return new RequestWriter.ContentWriter() {
@Override
public void write(OutputStream os) throws IOException {
if (payload instanceof String) {
os.write(((String) payload).getBytes(StandardCharsets.UTF_8));
} else {
if (useBinary) {
new JavaBinCodec().marshal(payload, os);
} else {
byte[] b = Utils.toJSON(payload);
os.write(b);
}
}
}
@Override
public String getContentType() {
return useBinary ? "application/javabin" : "application/json";
}
});
}
@Override
public String getContentType() {
if (payload instanceof String) return "application/json";
return useBinary ? "application/javabin" : "application/json";
}
};
}
@Override
public Collection<ContentStream> getContentStreams() throws IOException {
return null;
}
@ -135,7 +144,7 @@ public class V2Request extends SolrRequest<V2Response> implements MapWriter {
*/
public Builder withPayload(String payload) {
if (payload != null) {
this.payload = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
this.payload = payload;
}
return this;
}

View File

@ -343,6 +343,7 @@ public class CommandOperation {
}
ArrayList<CommandOperation> ops = new ArrayList<>();
for (ContentStream stream : streams) {
if ("application/javabin".equals(stream.getContentType())) {
ops.addAll(parse(stream.getStream(), singletonCommands));
} else {