Example usage for org.apache.solr.common.util JavaBinCodec unmarshal

List of usage examples for org.apache.solr.common.util JavaBinCodec unmarshal

Introduction

In this page you can find the example usage for org.apache.solr.common.util JavaBinCodec unmarshal.

Prototype

public Object unmarshal(InputStream is) throws IOException 

Source Link

Usage

From source file:com.b2international.index.es.EsDocumentSearcher.java

License:Apache License

private Object[] fromSearchAfterToken(final String searchAfterToken) {
    if (Strings.isNullOrEmpty(searchAfterToken)) {
        return null;
    }/*from   w  w w . ja  v  a  2s . c  o m*/

    final byte[] decodedToken = Base64.getUrlDecoder().decode(searchAfterToken);

    try (final ByteArrayInputStream bais = new ByteArrayInputStream(decodedToken)) {
        JavaBinCodec codec = new JavaBinCodec();
        List<Object> obj = (List<Object>) codec.unmarshal(bais);
        codec.close();

        return obj.toArray();
    } catch (final IOException e) {
        throw new FormattedRuntimeException("Couldn't decode searchAfter token.", e);
    }
}

From source file:org.apache.beam.sdk.io.solr.JavaBinCodecCoder.java

License:Apache License

@Override
public T decode(InputStream inStream) throws IOException {
    DataInputStream in = new DataInputStream(inStream);

    int len = VarInt.decodeInt(in);
    if (len < 0) {
        throw new CoderException("Invalid encoded SolrDocument length: " + len);
    }/*from w w  w. j av  a  2s  . c  o  m*/

    JavaBinCodec codec = new JavaBinCodec();
    return (T) codec.unmarshal(new BoundedInputStream(in, len));
}

From source file:org.codelibs.elasticsearch.solr.solr.JavaBinUpdateRequestCodec.java

License:Apache License

/**
 * Reads a NamedList from the given InputStream, converts it into a
 * SolrInputDocument and passes it to the given StreamingUpdateHandler
 *
 * @param is//  www .  j  av  a2 s  .  co m
 *            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(final InputStream is, final StreamingUpdateHandler handler) throws IOException {
    final UpdateRequest updateRequest = new UpdateRequest();
    List<Object> doclist; // mocksolrplugin: changed to Object
    List<String> delById;
    List<String> delByQ;
    final NamedList[] namedList = new NamedList[1];
    final 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(final DataInputInputStream dis) throws IOException {
            final int sz = readSize(dis);
            final NamedList nl = new NamedList();
            if (namedList[0] == null) {
                namedList[0] = nl;
            }
            for (int i = 0; i < sz; i++) {
                final String name = (String) readVal(dis);
                final Object val = readVal(dis);
                nl.add(name, val);
            }
            return nl;
        }

        @Override
        public List readIterator(final 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(final DataInputInputStream fis) throws IOException {
            final NamedList params = (NamedList) namedList[0].getVal(0);
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
            if (handler == null) {
                return super.readIterator(fis);
            }
            while (true) {
                final Object o = readVal(fis);
                if (o == END_OBJ) {
                    break;
                }
                SolrInputDocument sdoc = null;
                if (o instanceof List) {
                    sdoc = JavaBinUpdateRequestCodec.this.listToSolrInputDocument((List<NamedList>) o);
                } else if (o instanceof NamedList) {
                    final UpdateRequest req = new UpdateRequest();
                    req.setParams(new ModifiableSolrParams(SolrParams.toSolrParams((NamedList) o)));
                    handler.update(null, req);
                } else {
                    sdoc = (SolrInputDocument) o;
                }
                handler.update(sdoc, updateRequest);
            }
            return Collections.EMPTY_LIST;
        }
    };

    codec.unmarshal(is);

    // NOTE: if the update request contains only delete commands the params
    // must be loaded now
    if (updateRequest.getParams() == null) {
        final NamedList params = (NamedList) namedList[0].get("params");
        if (params != null) {
            updateRequest.setParams(new ModifiableSolrParams(SolrParams.toSolrParams(params)));
        }
    }
    delById = (List<String>) namedList[0].get("delById");
    delByQ = (List<String>) namedList[0].get("delByQ");
    doclist = (List) namedList[0].get("docs");

    if (doclist != null && !doclist.isEmpty()) {
        final List<SolrInputDocument> solrInputDocs = new ArrayList<SolrInputDocument>();
        for (final Object o : doclist) {
            if (o instanceof List) {
                solrInputDocs.add(listToSolrInputDocument((List<NamedList>) o));
            } else {
                solrInputDocs.add((SolrInputDocument) o);
            }
        }
        updateRequest.add(solrInputDocs);
    }
    if (delById != null) {
        for (final String s : delById) {
            updateRequest.deleteById(s);
        }
    }
    if (delByQ != null) {
        for (final String s : delByQ) {
            updateRequest.deleteByQuery(s);
        }
    }
    return updateRequest;

}