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

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

Introduction

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

Prototype

public JavaBinCodec() 

Source Link

Usage

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

License:Apache License

private String toSearchAfterToken(final Object[] searchAfter) {
    if (searchAfter == null) {
        return null;
    }//from w  ww.  j a  v  a2  s .c o  m

    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final JavaBinCodec codec = new JavaBinCodec();
        codec.marshal(searchAfter, baos);
        codec.close();

        final byte[] tokenBytes = baos.toByteArray();
        return Base64.getUrlEncoder().encodeToString(tokenBytes);
    } catch (IOException e) {
        throw new FormattedRuntimeException("Couldn't encode searchAfter paramaters to a token.", e);
    }
}

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

License:Apache License

private Object[] fromSearchAfterToken(final String searchAfterToken) {
    if (Strings.isNullOrEmpty(searchAfterToken)) {
        return null;
    }//w  w w  .  j  a  v  a2s  .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:edu.uci.ics.sourcerer.search.SourcererGateway.java

License:Open Source License

private Object getJavabinFromStream(InputStream is) {

    Object m1 = null;/*from ww  w . jav  a 2 s  . c o m*/
    try {
        m1 = new JavaBinCodec().unmarshal(is);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "IOE: " + e.getMessage());
    }

    return m1;
}

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

License:Apache License

@Override
public void encode(T value, OutputStream outStream) throws IOException {
    if (value == null) {
        throw new CoderException("cannot encode a null SolrDocument");
    }//from  w  w w.  java2  s.c  o m

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JavaBinCodec codec = new JavaBinCodec();
    codec.marshal(value, baos);

    byte[] bytes = baos.toByteArray();
    VarInt.encode(bytes.length, outStream);
    outStream.write(bytes);
}

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);
    }//  w w w  .j a  v a2 s  .c o  m

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

From source file:org.bigsolr.hadoop.SolrRecord.java

License:Apache License

@Override
public void write(DataOutput out) throws IOException {
    log.debug("SolrRecord -> write");

    JavaBinCodec codec = new JavaBinCodec();
    FastOutputStream os = FastOutputStream.wrap(DataOutputOutputStream.constructOutputStream(out));
    codec.init(os);/*from  w  w w. j av  a2  s. c om*/
    try {
        codec.writeVal(sd);
    } finally {
        os.flushBuffer();
    }
}

From source file:org.bigsolr.hadoop.SolrRecord.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    log.debug("SolrRecord -> readFields");

    JavaBinCodec codec = new JavaBinCodec();
    UnbufferedDataInputInputStream is = new UnbufferedDataInputInputStream(in);
    sd = (SolrDocument) codec.readVal(is);
}

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

License:Apache License

/**
 * Converts an UpdateRequest to a NamedList which can be serialized to the
 * given OutputStream in the javabin format
 *
 * @param updateRequest/*from  w  ww.  j  a v  a2 s.com*/
 *            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(final UpdateRequest updateRequest, final OutputStream os) throws IOException {
    final NamedList nl = new NamedList();
    final NamedList params = solrParamsToNamedList(updateRequest.getParams());
    if (updateRequest.getCommitWithin() != -1) {
        params.add("commitWithin", updateRequest.getCommitWithin());
    }
    Iterator<SolrInputDocument> docIter = null;

    if (updateRequest.getDocuments() != null) {
        docIter = updateRequest.getDocuments().iterator();
    }
    if (updateRequest.getDocIterator() != null) {
        docIter = updateRequest.getDocIterator();
    }

    nl.add("params", params);// 0: params
    nl.add("delById", updateRequest.getDeleteById());
    nl.add("delByQ", updateRequest.getDeleteQuery());
    nl.add("docs", docIter);
    final JavaBinCodec codec = new JavaBinCodec();
    codec.marshal(nl, os);
}

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/*from   ww w .  jav  a2s  . com*/
 *            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;

}

From source file:org.vootoo.client.netty.NettyUtil.java

License:Apache License

/**
 * @param bs/*w w w .  ja  v  a 2  s. c o  m*/
 * @return bs == null null
 * @throws IOException
 */
public static NamedList<Object> bytesToParams(ByteString bs) throws IOException {
    if (bs == null) {
        return null;
    }
    return (NamedList<Object>) new JavaBinCodec().unmarshal(bs.newInput());
}