use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestSubQueryTransformer method testJustJohnJavabin.
@SuppressWarnings("unchecked")
@Test
public void testJustJohnJavabin() throws Exception {
final SolrQueryRequest johnTwoFL = req(johnAndNancyParams);
ModifiableSolrParams params = new ModifiableSolrParams(johnTwoFL.getParams());
params.set("q", "name_s:john");
params.set("wt", "javabin");
johnTwoFL.setParams(params);
final NamedList<Object> unmarshalled;
{
SolrCore core = johnTwoFL.getCore();
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
SolrQueryResponse response = h.queryAndResponse(johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);
BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
responseWriter.write(bytes, johnTwoFL, response);
unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes.toByteArray()));
johnTwoFL.close();
SolrRequestInfo.clearRequestInfo();
}
SolrDocumentList resultDocs = (SolrDocumentList) (unmarshalled.get("response"));
{
Map<String, String> engText = new HashMap<>();
engText.put("text_t", "These guys develop stuff");
Map<String, String> engId = new HashMap<>();
engId.put("text_t", "These guys develop stuff");
engId.put("dept_id_s_dv", "Engineering");
for (int docNum : new int[] { 0, peopleMultiplier - 1 }) {
SolrDocument employeeDoc = resultDocs.get(docNum);
assertEquals("john", employeeDoc.getFieldValue("name_s_dv"));
for (String subResult : new String[] { "depts", "depts_i" }) {
SolrDocumentList subDoc = (SolrDocumentList) employeeDoc.getFieldValue(subResult);
for (int deptNum : new int[] { 0, deptMultiplier - 1 }) {
SolrDocument deptDoc = subDoc.get(deptNum);
Object expectedDept = (subResult.equals("depts") ? engText : engId);
assertTrue("" + expectedDept + " equals to " + deptDoc, expectedDept.equals(deptDoc));
}
}
}
}
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestJavabinTupleStreamParser method serialize.
public static byte[] serialize(Object o) throws IOException {
SolrQueryResponse response = new SolrQueryResponse();
response.getValues().add("results", o);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new JavaBinCodec().marshal(response.getValues(), baos);
return baos.toByteArray();
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestPushWriter method testStandardResponse.
public void testStandardResponse() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos, UTF_8);
PushWriter pw = new JSONWriter(osw, new LocalSolrQueryRequest(null, new ModifiableSolrParams()), new SolrQueryResponse());
writeData(pw);
osw.flush();
log.info(new String(baos.toByteArray(), "UTF-8"));
Map m = (Map) Utils.fromJSON(baos.toByteArray());
checkValues(m);
writeData(new JavaBinCodec(baos = new ByteArrayOutputStream(), null));
m = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
checkValues(m);
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class JavaBinUpdateRequestCodec method marshal.
/**
* Converts an UpdateRequest to a NamedList which can be serialized to the given OutputStream in the javabin format
*
* @param updateRequest 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(UpdateRequest updateRequest, OutputStream os) throws IOException {
NamedList nl = new NamedList();
NamedList params = solrParamsToNamedList(updateRequest.getParams());
if (updateRequest.getCommitWithin() != -1) {
params.add("commitWithin", updateRequest.getCommitWithin());
}
Iterator<SolrInputDocument> docIter = null;
if (updateRequest.getDocIterator() != null) {
docIter = updateRequest.getDocIterator();
}
Map<SolrInputDocument, Map<String, Object>> docMap = updateRequest.getDocumentsMap();
// 0: params
nl.add("params", params);
if (updateRequest.getDeleteByIdMap() != null) {
nl.add("delByIdMap", updateRequest.getDeleteByIdMap());
}
nl.add("delByQ", updateRequest.getDeleteQuery());
if (docMap != null) {
nl.add("docsMap", docMap.entrySet().iterator());
} else {
if (updateRequest.getDocuments() != null) {
docIter = updateRequest.getDocuments().iterator();
}
nl.add("docs", docIter);
}
JavaBinCodec codec = new JavaBinCodec();
codec.marshal(nl, os);
}
use of org.apache.solr.common.util.JavaBinCodec 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;
}
Aggregations