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");
}
};
}
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);
}
}
}
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);
}
}
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);
}
Aggregations