SOLR-641 -- use the binary parser for embeded solr server

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@678624 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-07-22 03:17:13 +00:00
parent 3b7621142e
commit 8854b2b627
1 changed files with 36 additions and 27 deletions

View File

@ -17,6 +17,8 @@
package org.apache.solr.client.solrj.embedded;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
@ -25,15 +27,16 @@ import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.BinaryResponseParser;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.MultiCore;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.BinaryResponseWriter;
import org.apache.solr.request.QueryResponseWriter;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
@ -51,14 +54,13 @@ import org.apache.solr.servlet.SolrRequestParsers;
*/
public class EmbeddedSolrServer extends SolrServer
{
protected ModifiableSolrParams _invariantParams;
protected ResponseParser _processor;
protected final MultiCore multicore; // either multicore
protected final SolrCore core; // or single core
protected final SolrRequestParsers parser;
protected final String coreName; // use MultiCore registry
private final SolrRequestParsers _parser;
public EmbeddedSolrServer( SolrCore core )
{
if ( core == null ) {
@ -67,7 +69,8 @@ public class EmbeddedSolrServer extends SolrServer
this.core = core;
this.multicore = null;
this.coreName = null;
this.parser = init();
_parser = new SolrRequestParsers( null );
}
public EmbeddedSolrServer( MultiCore multicore, String coreName )
@ -82,18 +85,8 @@ public class EmbeddedSolrServer extends SolrServer
if( c == null ) {
throw new RuntimeException( "Unknown core: "+coreName );
}
this.parser = init();
}
private SolrRequestParsers init()
{
_processor = new XMLResponseParser();
_invariantParams = new ModifiableSolrParams();
_invariantParams.set( CommonParams.WT, _processor.getWriterType() );
_invariantParams.set( CommonParams.VERSION, "2.2" );
return new SolrRequestParsers( null );
_parser = new SolrRequestParsers( null );
}
@Override
@ -118,9 +111,6 @@ public class EmbeddedSolrServer extends SolrServer
if( params == null ) {
params = new ModifiableSolrParams();
}
if( _invariantParams != null ) {
params = new DefaultSolrParams( _invariantParams, params );
}
// Extract the handler from the path or params
SolrRequestHandler handler = core.getRequestHandler( path );
@ -145,7 +135,7 @@ public class EmbeddedSolrServer extends SolrServer
}
try {
SolrQueryRequest req = parser.buildRequestFrom( core, params, request.getContentStreams() );
SolrQueryRequest req = _parser.buildRequestFrom( core, params, request.getContentStreams() );
req.getContext().put( "path", path );
SolrQueryResponse rsp = new SolrQueryResponse();
core.execute( handler, req, rsp );
@ -154,13 +144,9 @@ public class EmbeddedSolrServer extends SolrServer
}
// Now write it out
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
StringWriter out = new StringWriter();
responseWriter.write(out, req, rsp);
// TODO: writers might be able to output binary someday
NamedList<Object> normalized = getParsedResponse(req, rsp);
req.close();
return _processor.processResponse( new StringReader( out.toString() ) );
return normalized;
}
catch( IOException iox ) {
throw iox;
@ -169,4 +155,27 @@ public class EmbeddedSolrServer extends SolrServer
throw new SolrServerException( ex );
}
}
/**
* TODO -- in the future, this could perhaps transform the NamedList without serializing it
* then parsing it from the serialized form.
*
* @param req
* @param rsp
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents
* become SolrDocuments, DocList becomes SolrDocumentList etc.
*/
public NamedList<Object> getParsedResponse( SolrQueryRequest req, SolrQueryResponse rsp )
{
try {
BinaryResponseWriter writer = new BinaryResponseWriter();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
writer.write( bos, req, rsp );
BinaryResponseParser parser = new BinaryResponseParser();
return parser.processResponse( new ByteArrayInputStream( bos.toByteArray() ), "UTF-8" );
}
catch( Exception ex ) {
throw new RuntimeException( ex );
}
}
}