Search in sources :

Example 1 with DataInputInputStream

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

the class StreamingBinaryResponseParser method processResponse.

@Override
public NamedList<Object> processResponse(InputStream body, String encoding) {
    try {
        JavaBinCodec codec = new JavaBinCodec() {

            @Override
            public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
                SolrDocument doc = super.readSolrDocument(dis);
                callback.streamSolrDocument(doc);
                return null;
            }

            @Override
            public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException {
                SolrDocumentList solrDocs = new SolrDocumentList();
                List list = (List) readVal(dis);
                solrDocs.setNumFound((Long) list.get(0));
                solrDocs.setStart((Long) list.get(1));
                solrDocs.setMaxScore((Float) list.get(2));
                callback.streamDocListInfo(solrDocs.getNumFound(), solrDocs.getStart(), solrDocs.getMaxScore());
                // Read the Array
                tagByte = dis.readByte();
                if ((tagByte >>> 5) != (ARR >>> 5)) {
                    throw new RuntimeException("doclist must have an array");
                }
                int sz = readSize(dis);
                for (int i = 0; i < sz; i++) {
                    // must be a SolrDocument
                    readVal(dis);
                }
                return solrDocs;
            }
        };
        return (NamedList<Object>) codec.unmarshal(body);
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);
    }
}
Also used : DataInputInputStream(org.apache.solr.common.util.DataInputInputStream) SolrDocument(org.apache.solr.common.SolrDocument) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SolrDocumentList(org.apache.solr.common.SolrDocumentList) NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 2 with DataInputInputStream

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

List (java.util.List)2 DataInputInputStream (org.apache.solr.common.util.DataInputInputStream)2 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)2 NamedList (org.apache.solr.common.util.NamedList)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrException (org.apache.solr.common.SolrException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1