Search in sources :

Example 6 with JavaBinCodec

use of org.apache.solr.common.util.JavaBinCodec 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)

Example 7 with JavaBinCodec

use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.

the class TestJavabinTupleStreamParser method serialize.

public static byte[] serialize(Object o) throws IOException {
    SolrQueryResponse response = new SolrQueryResponse();
    response.getValues().add("results", o);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new JavaBinCodec().marshal(response.getValues(), baos);
    return baos.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 8 with JavaBinCodec

use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.

the class TestPushWriter method testStandardResponse.

public void testStandardResponse() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(baos, UTF_8);
    PushWriter pw = new JSONWriter(osw, new LocalSolrQueryRequest(null, new ModifiableSolrParams()), new SolrQueryResponse());
    writeData(pw);
    osw.flush();
    log.info(new String(baos.toByteArray(), "UTF-8"));
    Map m = (Map) Utils.fromJSON(baos.toByteArray());
    checkValues(m);
    writeData(new JavaBinCodec(baos = new ByteArrayOutputStream(), null));
    m = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    checkValues(m);
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PushWriter(org.apache.solr.common.PushWriter) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 9 with JavaBinCodec

use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.

the class JavaBinUpdateRequestCodec method marshal.

/**
   * Converts an UpdateRequest to a NamedList which can be serialized to the given OutputStream in the javabin format
   *
   * @param updateRequest the UpdateRequest to be written out
   * @param os            the OutputStream to which the request is to be written
   *
   * @throws IOException in case of an exception during marshalling or writing to the stream
   */
public void marshal(UpdateRequest updateRequest, OutputStream os) throws IOException {
    NamedList nl = new NamedList();
    NamedList params = solrParamsToNamedList(updateRequest.getParams());
    if (updateRequest.getCommitWithin() != -1) {
        params.add("commitWithin", updateRequest.getCommitWithin());
    }
    Iterator<SolrInputDocument> docIter = null;
    if (updateRequest.getDocIterator() != null) {
        docIter = updateRequest.getDocIterator();
    }
    Map<SolrInputDocument, Map<String, Object>> docMap = updateRequest.getDocumentsMap();
    // 0: params
    nl.add("params", params);
    if (updateRequest.getDeleteByIdMap() != null) {
        nl.add("delByIdMap", updateRequest.getDeleteByIdMap());
    }
    nl.add("delByQ", updateRequest.getDeleteQuery());
    if (docMap != null) {
        nl.add("docsMap", docMap.entrySet().iterator());
    } else {
        if (updateRequest.getDocuments() != null) {
            docIter = updateRequest.getDocuments().iterator();
        }
        nl.add("docs", docIter);
    }
    JavaBinCodec codec = new JavaBinCodec();
    codec.marshal(nl, os);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) NamedList(org.apache.solr.common.util.NamedList) Map(java.util.Map) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 10 with JavaBinCodec

use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.

the class JavaBinUpdateRequestCodec method unmarshal.

/**
   * Reads a NamedList from the given InputStream, converts it into a SolrInputDocument and passes it to the given
   * StreamingUpdateHandler
   *
   * @param is      the InputStream from which to read
   * @param handler an instance of StreamingUpdateHandler to which SolrInputDocuments are streamed one by one
   *
   * @return the UpdateRequest
   *
   * @throws IOException in case of an exception while reading from the input stream or unmarshalling
   */
public UpdateRequest unmarshal(InputStream is, final StreamingUpdateHandler handler) throws IOException {
    final UpdateRequest updateRequest = new UpdateRequest();
    List<List<NamedList>> doclist;
    List<Entry<SolrInputDocument, Map<Object, Object>>> docMap;
    List<String> delById;
    Map<String, Map<String, Object>> delByIdMap;
    List<String> delByQ;
    final NamedList[] namedList = new NamedList[1];
    JavaBinCodec codec = new JavaBinCodec() {

        // NOTE: this only works because this is an anonymous inner class 
        // which will only ever be used on a single stream -- if this class 
        // is ever refactored, this will not work.
        private boolean seenOuterMostDocIterator = false;

        @Override
        public NamedList readNamedList(DataInputInputStream dis) throws IOException {
            int sz = readSize(dis);
            NamedList nl = new NamedList();
            if (namedList[0] == null) {
                namedList[0] = nl;
            }
            for (int i = 0; i < sz; i++) {
                String name = (String) readVal(dis);
                Object val = readVal(dis);
                nl.add(name, val);
            }
            return nl;
        }

        @Override
        public List readIterator(DataInputInputStream fis) throws IOException {
            // default behavior for reading any regular Iterator in the stream
            if (seenOuterMostDocIterator)
                return super.readIterator(fis);
            // special treatment for first outermost Iterator 
            // (the list of documents)
            seenOuterMostDocIterator = true;
            return readOuterMostDocIterator(fis);
        }

        private List readOuterMostDocIterator(DataInputInputStream fis) throws IOException {
            NamedList params = (NamedList) namedList[0].get("params");
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
            if (handler == null)
                return super.readIterator(fis);
            Integer commitWithin = null;
            Boolean overwrite = null;
            Object o = null;
            while (true) {
                if (o == null) {
                    o = readVal(fis);
                }
                if (o == END_OBJ) {
                    break;
                }
                SolrInputDocument sdoc = null;
                if (o instanceof List) {
                    sdoc = listToSolrInputDocument((List<NamedList>) o);
                } else if (o instanceof NamedList) {
                    UpdateRequest req = new UpdateRequest();
                    req.setParams(new ModifiableSolrParams(SolrParams.toSolrParams((NamedList) o)));
                    handler.update(null, req, null, null);
                } else if (o instanceof Map.Entry) {
                    sdoc = (SolrInputDocument) ((Map.Entry) o).getKey();
                    Map p = (Map) ((Map.Entry) o).getValue();
                    if (p != null) {
                        commitWithin = (Integer) p.get(UpdateRequest.COMMIT_WITHIN);
                        overwrite = (Boolean) p.get(UpdateRequest.OVERWRITE);
                    }
                } else {
                    sdoc = (SolrInputDocument) o;
                }
                // peek at the next object to see if we're at the end
                o = readVal(fis);
                if (o == END_OBJ) {
                    // indicate that we've hit the last doc in the batch, used to enable optimizations when doing replication
                    updateRequest.lastDocInBatch();
                }
                handler.update(sdoc, updateRequest, commitWithin, overwrite);
            }
            return Collections.EMPTY_LIST;
        }
    };
    codec.unmarshal(is);
    // must be loaded now
    if (updateRequest.getParams() == null) {
        NamedList params = (NamedList) namedList[0].get("params");
        if (params != null) {
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
        }
    }
    delById = (List<String>) namedList[0].get("delById");
    delByIdMap = (Map<String, Map<String, Object>>) namedList[0].get("delByIdMap");
    delByQ = (List<String>) namedList[0].get("delByQ");
    doclist = (List) namedList[0].get("docs");
    Object docsMapObj = namedList[0].get("docsMap");
    if (docsMapObj instanceof Map) {
        //SOLR-5762
        docMap = new ArrayList(((Map) docsMapObj).entrySet());
    } else {
        docMap = (List<Entry<SolrInputDocument, Map<Object, Object>>>) docsMapObj;
    }
    if (delById != null) {
        for (String s : delById) {
            updateRequest.deleteById(s);
        }
    }
    if (delByIdMap != null) {
        for (Map.Entry<String, Map<String, Object>> entry : delByIdMap.entrySet()) {
            Map<String, Object> params = entry.getValue();
            if (params != null) {
                Long version = (Long) params.get(UpdateRequest.VER);
                if (params.containsKey(ShardParams._ROUTE_))
                    updateRequest.deleteById(entry.getKey(), (String) params.get(ShardParams._ROUTE_));
                else
                    updateRequest.deleteById(entry.getKey(), version);
            } else {
                updateRequest.deleteById(entry.getKey());
            }
        }
    }
    if (delByQ != null) {
        for (String s : delByQ) {
            updateRequest.deleteByQuery(s);
        }
    }
    return updateRequest;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) Entry(java.util.Map.Entry) SolrInputDocument(org.apache.solr.common.SolrInputDocument) DataInputInputStream(org.apache.solr.common.util.DataInputInputStream) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Map(java.util.Map)

Aggregations

JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)16 NamedList (org.apache.solr.common.util.NamedList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 SolrDocument (org.apache.solr.common.SolrDocument)6 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SolrException (org.apache.solr.common.SolrException)5 IOException (java.io.IOException)4 List (java.util.List)4 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SolrCore (org.apache.solr.core.SolrCore)3 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)3 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 FieldType (org.apache.solr.schema.FieldType)3