Search in sources :

Example 1 with BinaryQueryResponseWriter

use of org.apache.solr.response.BinaryQueryResponseWriter in project lucene-solr by apache.

the class TestHarness method query.

/**
   * Processes a "query" using a user constructed SolrQueryRequest, and closes the request at the end.
   *
   * @param handler the name of the request handler to process the request
   * @param req the Query to process, will be closed.
   * @return The XML response to the query
   * @exception Exception any exception in the response.
   * @exception IOException if there is a problem writing the XML
   * @see LocalSolrQueryRequest
   */
public String query(String handler, SolrQueryRequest req) throws Exception {
    try {
        SolrCore core = req.getCore();
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        core.execute(core.getRequestHandler(handler), req, rsp);
        if (rsp.getException() != null) {
            throw rsp.getException();
        }
        QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
        if (responseWriter instanceof BinaryQueryResponseWriter) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(32000);
            BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) responseWriter;
            writer.write(byteArrayOutputStream, req, rsp);
            return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
        } else {
            StringWriter sw = new StringWriter(32000);
            responseWriter.write(sw, req, rsp);
            return sw.toString();
        }
    } finally {
        req.close();
        SolrRequestInfo.clearRequestInfo();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) StringWriter(java.io.StringWriter) SolrCore(org.apache.solr.core.SolrCore) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter)

Example 2 with BinaryQueryResponseWriter

use of org.apache.solr.response.BinaryQueryResponseWriter in project lucene-solr by apache.

the class TestWriterPerf method doPerf.

void doPerf(String writerName, SolrQueryRequest req, int encIter, int decIter) throws Exception {
    SolrQueryResponse rsp = getResponse(req);
    QueryResponseWriter w = h.getCore().getQueryResponseWriter(writerName);
    ByteArrayOutputStream out = null;
    System.gc();
    RTimer timer = new RTimer();
    for (int i = 0; i < encIter; i++) {
        if (w instanceof BinaryQueryResponseWriter) {
            BinaryQueryResponseWriter binWriter = (BinaryQueryResponseWriter) w;
            out = new ByteArrayOutputStream();
            binWriter.write(out, req, rsp);
            out.close();
        } else {
            out = new ByteArrayOutputStream();
            // to be fair, from my previous tests, much of the performance will be sucked up
            // by java's UTF-8 encoding/decoding, not the actual writing
            Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            w.write(writer, req, rsp);
            writer.close();
        }
    }
    double encodeTime = timer.getTime();
    byte[] arr = out.toByteArray();
    timer = new RTimer();
    writerName = writerName.intern();
    for (int i = 0; i < decIter; i++) {
        ResponseParser rp = null;
        if (writerName == "xml") {
            rp = new XMLResponseParser();
        } else if (writerName == "javabin") {
            rp = new BinaryResponseParser();
        } else {
            break;
        }
        ByteArrayInputStream in = new ByteArrayInputStream(arr);
        rp.processResponse(in, "UTF-8");
    }
    double decodeTime = timer.getTime();
    log.info("writer " + writerName + ", size=" + out.size() + ", encodeRate=" + (encIter * 1000L / encodeTime) + ", decodeRate=" + (decIter * 1000L / decodeTime));
    req.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ResponseParser(org.apache.solr.client.solrj.ResponseParser) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RTimer(org.apache.solr.util.RTimer) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) OutputStreamWriter(java.io.OutputStreamWriter) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with BinaryQueryResponseWriter

use of org.apache.solr.response.BinaryQueryResponseWriter in project lucene-solr by apache.

the class TestSubQueryTransformer method testJustJohnJavabin.

@SuppressWarnings("unchecked")
@Test
public void testJustJohnJavabin() throws Exception {
    final SolrQueryRequest johnTwoFL = req(johnAndNancyParams);
    ModifiableSolrParams params = new ModifiableSolrParams(johnTwoFL.getParams());
    params.set("q", "name_s:john");
    params.set("wt", "javabin");
    johnTwoFL.setParams(params);
    final NamedList<Object> unmarshalled;
    {
        SolrCore core = johnTwoFL.getCore();
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
        SolrQueryResponse response = h.queryAndResponse(johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);
        BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        responseWriter.write(bytes, johnTwoFL, response);
        unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes.toByteArray()));
        johnTwoFL.close();
        SolrRequestInfo.clearRequestInfo();
    }
    SolrDocumentList resultDocs = (SolrDocumentList) (unmarshalled.get("response"));
    {
        Map<String, String> engText = new HashMap<>();
        engText.put("text_t", "These guys develop stuff");
        Map<String, String> engId = new HashMap<>();
        engId.put("text_t", "These guys develop stuff");
        engId.put("dept_id_s_dv", "Engineering");
        for (int docNum : new int[] { 0, peopleMultiplier - 1 }) {
            SolrDocument employeeDoc = resultDocs.get(docNum);
            assertEquals("john", employeeDoc.getFieldValue("name_s_dv"));
            for (String subResult : new String[] { "depts", "depts_i" }) {
                SolrDocumentList subDoc = (SolrDocumentList) employeeDoc.getFieldValue(subResult);
                for (int deptNum : new int[] { 0, deptMultiplier - 1 }) {
                    SolrDocument deptDoc = subDoc.get(deptNum);
                    Object expectedDept = (subResult.equals("depts") ? engText : engId);
                    assertTrue("" + expectedDept + " equals to " + deptDoc, expectedDept.equals(deptDoc));
                }
            }
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

BinaryQueryResponseWriter (org.apache.solr.response.BinaryQueryResponseWriter)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SolrCore (org.apache.solr.core.SolrCore)2 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)2 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 ResponseParser (org.apache.solr.client.solrj.ResponseParser)1 BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)1 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)1