why do we need a serialize/deserialize for EmbeddedSolrServer? Why can't we just return the Namedlist directly?
On Tue, Jul 22, 2008 at 8:47 AM, <[EMAIL PROTECTED]> wrote: > Author: ryan > Date: Mon Jul 21 20:17:13 2008 > New Revision: 678624 > > URL: http://svn.apache.org/viewvc?rev=678624&view=rev > Log: > SOLR-641 -- use the binary parser for embeded solr server > > Modified: > > lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java > > Modified: > lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java > URL: > http://svn.apache.org/viewvc/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java?rev=678624&r1=678623&r2=678624&view=diff > ============================================================================== > --- > lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java > (original) > +++ > lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java > Mon Jul 21 20:17:13 2008 > @@ -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.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,13 +54,12 @@ > */ > 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 ) > { > @@ -67,7 +69,8 @@ > this.core = core; > this.multicore = null; > this.coreName = null; > - this.parser = init(); > + > + _parser = new SolrRequestParsers( null ); > } > > public EmbeddedSolrServer( MultiCore multicore, String coreName ) > @@ -82,20 +85,10 @@ > 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 > public NamedList<Object> request(SolrRequest request) throws > SolrServerException, IOException > { > @@ -118,9 +111,6 @@ > 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 @@ > } > > 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 @@ > } > > // 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 @@ > 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 ); > + } > + } > } > > > -- --Noble Paul
