SOLR-973 -- CommonsHttpSolrServer writes the xml directly to the server

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@747790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-02-25 14:13:00 +00:00
parent 3afcd7a175
commit 47487967fc
4 changed files with 166 additions and 43 deletions

View File

@ -193,6 +193,9 @@ Optimizations
7. SOLR-921: SolrResourceLoader must cache short class name vs fully qualified classname 7. SOLR-921: SolrResourceLoader must cache short class name vs fully qualified classname
(Noble Paul, hossman via shalin) (Noble Paul, hossman via shalin)
8. SOLR-973: CommonsHttpSolrServer writes the xml directly to the server.
(Noble Paul via shalin)
Bug Fixes Bug Fixes
---------------------- ----------------------
1. SOLR-774: Fixed logging level display (Sean Timm via Otis Gospodnetic) 1. SOLR-774: Fixed logging level display (Sean Timm via Otis Gospodnetic)

View File

@ -43,39 +43,9 @@ public class BinaryRequestWriter extends RequestWriter {
isNull(updateRequest.getDeleteById()) && isNull(updateRequest.getDeleteById()) &&
isNull(updateRequest.getDeleteQuery())) { isNull(updateRequest.getDeleteQuery())) {
return null; return null;
} }
List<ContentStream> l = new ArrayList<ContentStream>();
final BAOS baos = new BAOS(); l.add(new LazyContentStream(updateRequest));
new JavaBinUpdateRequestCodec().marshal(updateRequest, baos);
List<ContentStream> l = new ArrayList<ContentStream>(1);
l.add(new ContentStream() {
public String getName() {
return null;
}
public String getSourceInfo() {
return "javabin";
}
public String getContentType() {
return "application/octet-stream";
}
public Long getSize() // size if we know it, otherwise null
{
return new Long(baos.size());
}
public InputStream getStream() throws IOException {
return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
}
public Reader getReader() throws IOException {
throw new RuntimeException("No reader available . this is a binarystream");
}
});
return l; return l;
} else { } else {
return super.getContentStreams(req); return super.getContentStreams(req);
@ -83,11 +53,50 @@ public class BinaryRequestWriter extends RequestWriter {
} }
private boolean isNull(List l) {
return l == null || l.isEmpty(); public String getUpdateContentType() {
return "application/octet-stream";
} }
/* public ContentStream getContentStream(final UpdateRequest request) throws IOException {
final BAOS baos = new BAOS();
new JavaBinUpdateRequestCodec().marshal(request, baos);
return new ContentStream() {
public String getName() {
return null;
}
public String getSourceInfo() {
return "javabin";
}
public String getContentType() {
return "application/octet-stream";
}
public Long getSize() // size if we know it, otherwise null
{
return new Long(baos.size());
}
public InputStream getStream() throws IOException {
return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
}
public Reader getReader() throws IOException {
throw new RuntimeException("No reader available . this is a binarystream");
}
};
}
public void write(SolrRequest request, OutputStream os) throws IOException {
if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request;
new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
}
}/*
* A hack to get access to the protected internal buffer and avoid an additional copy * A hack to get access to the protected internal buffer and avoid an additional copy
*/ */
class BAOS extends ByteArrayOutputStream { class BAOS extends ByteArrayOutputStream {

View File

@ -41,6 +41,7 @@ import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.PartBase; import org.apache.commons.httpclient.methods.multipart.PartBase;
@ -350,17 +351,39 @@ public class CommonsHttpSolrServer extends SolrServer
} }
// It is has one stream, it is the post body, put the params in the URL // It is has one stream, it is the post body, put the params in the URL
else { else {
String pstr = ClientUtils.toQueryString( params, false ); String pstr = ClientUtils.toQueryString(params, false);
PostMethod post = new PostMethod( url+pstr ); PostMethod post = new PostMethod(url + pstr);
// Single stream as body // Single stream as body
// Using a loop just to get the first one // Using a loop just to get the first one
for( ContentStream content : streams ) { final ContentStream[] contentStream = new ContentStream[1];
post.setRequestEntity( for (ContentStream content : streams) {
new InputStreamRequestEntity( content.getStream(), content.getContentType()) contentStream[0] = content;
);
break; break;
} }
if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
post.setRequestEntity(new RequestEntity() {
public long getContentLength() {
return -1;
}
public String getContentType() {
return contentStream[0].getContentType();
}
public boolean isRepeatable() {
return false;
}
public void writeRequest(OutputStream outputStream) throws IOException {
((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream);
}
}
);
} else {
post.setRequestEntity(new InputStreamRequestEntity(contentStream[0].getStream(), contentStream[0].getContentType()));
}
method = post; method = post;
} }
} }

View File

@ -18,26 +18,114 @@
package org.apache.solr.client.solrj.request; package org.apache.solr.client.solrj.request;
import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import java.io.IOException; import java.io.*;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
/** /**
* A RequestWriter is used to write requests to Solr. * A RequestWriter is used to write requests to Solr.
* <p/> * <p/>
* A subclass can override the methods in this class to supply a custom format in which a request can be sent. * A subclass can override the methods in this class to supply a custom format in which a request can be sent.
* @since solr 1.4 *
* @version $Id$ * @version $Id$
* @since solr 1.4
*/ */
public class RequestWriter { public class RequestWriter {
public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException { public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException {
if (req instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) req;
if (isEmpty(updateRequest)) return null;
List<ContentStream> l = new ArrayList<ContentStream>();
l.add(new LazyContentStream(updateRequest));
return l;
}
return req.getContentStreams(); return req.getContentStreams();
} }
private boolean isEmpty(UpdateRequest updateRequest) {
return isNull(updateRequest.getDocuments()) &&
isNull(updateRequest.getDeleteById()) &&
isNull(updateRequest.getDeleteQuery());
}
public String getPath(SolrRequest req) { public String getPath(SolrRequest req) {
return req.getPath(); return req.getPath();
} }
public ContentStream getContentStream(UpdateRequest req) throws IOException {
return new ContentStreamBase.StringStream(req.getXML());
}
public void write(SolrRequest request, OutputStream os) throws IOException {
if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request;
OutputStreamWriter writer = new OutputStreamWriter(os);
updateRequest.writeXML(writer);
writer.flush();
}
}
public String getUpdateContentType() {
return ClientUtils.TEXT_XML;
}
public class LazyContentStream implements ContentStream {
ContentStream contentStream = null;
UpdateRequest req = null;
public LazyContentStream(UpdateRequest req) {
this.req = req;
}
private ContentStream getDelegate() {
if (contentStream == null) {
try {
contentStream = getContentStream(req);
} catch (IOException e) {
throw new RuntimeException("Unable to write xml into a stream", e);
}
}
return contentStream;
}
public String getName() {
return getDelegate().getName();
}
public String getSourceInfo() {
return getDelegate().getSourceInfo();
}
public String getContentType() {
return getUpdateContentType();
}
public Long getSize() {
return getDelegate().getSize();
}
public InputStream getStream() throws IOException {
return getDelegate().getStream();
}
public Reader getReader() throws IOException {
return getDelegate().getReader();
}
public void writeTo(OutputStream os) throws IOException {
write(req, os);
}
}
protected boolean isNull(List l) {
return l == null || l.isEmpty();
}
} }