Search in sources :

Example 1 with JavaBinUpdateRequestCodec

use of org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec in project lucene-solr by apache.

the class BinaryRequestWriter method getContentStream.

@Override
public ContentStream getContentStream(final UpdateRequest request) throws IOException {
    final BAOS baos = new BAOS();
    new JavaBinUpdateRequestCodec().marshal(request, baos);
    return new ContentStream() {

        @Override
        public String getName() {
            return null;
        }

        @Override
        public String getSourceInfo() {
            return "javabin";
        }

        @Override
        public String getContentType() {
            return "application/javabin";
        }

        @Override
        public // size if we know it, otherwise null
        Long getSize() {
            return new Long(baos.size());
        }

        @Override
        public InputStream getStream() {
            return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
        }

        @Override
        public Reader getReader() {
            throw new RuntimeException("No reader available . this is a binarystream");
        }
    };
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec)

Example 2 with JavaBinUpdateRequestCodec

use of org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec in project lucene-solr by apache.

the class JavabinLoader method parseAndLoadDocs.

private void parseAndLoadDocs(final SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, final UpdateRequestProcessor processor) throws IOException {
    UpdateRequest update = null;
    JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {

        private AddUpdateCommand addCmd = null;

        @Override
        public void update(SolrInputDocument document, UpdateRequest updateRequest, Integer commitWithin, Boolean overwrite) {
            if (document == null) {
                // Perhaps commit from the parameters
                try {
                    RequestHandlerUtils.handleCommit(req, processor, updateRequest.getParams(), false);
                    RequestHandlerUtils.handleRollback(req, processor, updateRequest.getParams(), false);
                } catch (IOException e) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR handling commit/rollback");
                }
                return;
            }
            if (addCmd == null) {
                addCmd = getAddCommand(req, updateRequest.getParams());
            }
            addCmd.solrDoc = document;
            if (commitWithin != null) {
                addCmd.commitWithin = commitWithin;
            }
            if (overwrite != null) {
                addCmd.overwrite = overwrite;
            }
            if (updateRequest.isLastDocInBatch()) {
                // this is a hint to downstream code that indicates we've sent the last doc in a batch
                addCmd.isLastDocInBatch = true;
            }
            try {
                processor.processAdd(addCmd);
                addCmd.clear();
            } catch (IOException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR adding document " + document, e);
            }
        }
    };
    FastInputStream in = FastInputStream.wrap(stream);
    for (; ; ) {
        try {
            update = new JavaBinUpdateRequestCodec().unmarshal(in, handler);
        } catch (EOFException e) {
            // this is expected
            break;
        }
        if (update.getDeleteByIdMap() != null || update.getDeleteQuery() != null) {
            delete(req, update, processor);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) EOFException(java.io.EOFException) IOException(java.io.IOException) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec) SolrException(org.apache.solr.common.SolrException) FastInputStream(org.apache.solr.common.util.FastInputStream)

Example 3 with JavaBinUpdateRequestCodec

use of org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec in project lucene-solr by apache.

the class BinaryRequestWriter method write.

@Override
public void write(SolrRequest request, OutputStream os) throws IOException {
    if (request instanceof UpdateRequest) {
        UpdateRequest updateRequest = (UpdateRequest) request;
        new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec)

Example 4 with JavaBinUpdateRequestCodec

use of org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec in project lucene-solr by apache.

the class JavabinLoaderTest method doTestLastDocInBatchFlag.

protected void doTestLastDocInBatchFlag(int numDocsInBatch) throws Exception {
    List<SolrInputDocument> batch = new ArrayList<>(numDocsInBatch);
    for (int d = 0; d < numDocsInBatch; d++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", String.valueOf(d));
        batch.add(doc);
    }
    UpdateRequest updateRequest = new UpdateRequest();
    if (batch.size() > 1) {
        updateRequest.add(batch);
    } else {
        updateRequest.add(batch.get(0));
    }
    // client-side SolrJ would do this ...
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    (new JavaBinUpdateRequestCodec()).marshal(updateRequest, os);
    // need to override the processAdd method b/c JavabinLoader calls
    // clear on the addCmd after it is passed on to the handler ... a simple clone will suffice for this test
    BufferingRequestProcessor mockUpdateProcessor = new BufferingRequestProcessor(null) {

        @Override
        public void processAdd(AddUpdateCommand cmd) throws IOException {
            addCommands.add((AddUpdateCommand) cmd.clone());
        }
    };
    SolrQueryRequest req = req();
    (new JavabinLoader()).load(req, new SolrQueryResponse(), new ContentStreamBase.ByteArrayStream(os.toByteArray(), "test"), mockUpdateProcessor);
    req.close();
    assertTrue(mockUpdateProcessor.addCommands.size() == numDocsInBatch);
    for (int i = 0; i < numDocsInBatch - 1; i++) // not last doc in batch
    assertFalse(mockUpdateProcessor.addCommands.get(i).isLastDocInBatch);
    // last doc should have the flag set
    assertTrue(mockUpdateProcessor.addCommands.get(batch.size() - 1).isLastDocInBatch);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Aggregations

JavaBinUpdateRequestCodec (org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec)4 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SolrException (org.apache.solr.common.SolrException)1 ContentStream (org.apache.solr.common.util.ContentStream)1 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)1 FastInputStream (org.apache.solr.common.util.FastInputStream)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)1 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)1