use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class AddBlockUpdateTest method testJavaBinCodec.
@Test
public void testJavaBinCodec() throws IOException {
//actually this test must be in other test class
SolrInputDocument topDocument = new SolrInputDocument();
topDocument.addField("parent_f1", "v1");
topDocument.addField("parent_f2", "v2");
int childsNum = atLeast(10);
for (int index = 0; index < childsNum; ++index) {
addChildren("child", topDocument, index, false);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
new JavaBinCodec().marshal(topDocument, os);
byte[] buffer = os.toByteArray();
//now read the Object back
InputStream is = new ByteArrayInputStream(buffer);
SolrInputDocument result = (SolrInputDocument) new JavaBinCodec().unmarshal(is);
assertEquals(2, result.size());
assertEquals("v1", result.getFieldValue("parent_f1"));
assertEquals("v2", result.getFieldValue("parent_f2"));
List<SolrInputDocument> resultChilds = result.getChildDocuments();
int resultChildsSize = resultChilds == null ? 0 : resultChilds.size();
assertEquals(childsNum, resultChildsSize);
for (int childIndex = 0; childIndex < childsNum; ++childIndex) {
SolrInputDocument child = resultChilds.get(childIndex);
for (int fieldNum = 0; fieldNum < childIndex; ++fieldNum) {
assertEquals(childIndex + "value" + fieldNum, child.getFieldValue(childIndex + "child" + fieldNum));
}
List<SolrInputDocument> grandChilds = child.getChildDocuments();
int grandChildsSize = grandChilds == null ? 0 : grandChilds.size();
assertEquals(childIndex * 2, grandChildsSize);
for (int grandIndex = 0; grandIndex < childIndex * 2; ++grandIndex) {
SolrInputDocument grandChild = grandChilds.get(grandIndex);
assertFalse(grandChild.hasChildDocuments());
for (int fieldNum = 0; fieldNum < grandIndex; ++fieldNum) {
assertEquals(grandIndex + "value" + fieldNum, grandChild.getFieldValue(grandIndex + "grand" + fieldNum));
}
}
}
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class StreamingBinaryResponseParser method processResponse.
@Override
public NamedList<Object> processResponse(InputStream body, String encoding) {
try {
JavaBinCodec codec = new JavaBinCodec() {
@Override
public SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {
SolrDocument doc = super.readSolrDocument(dis);
callback.streamSolrDocument(doc);
return null;
}
@Override
public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException {
SolrDocumentList solrDocs = new SolrDocumentList();
List list = (List) readVal(dis);
solrDocs.setNumFound((Long) list.get(0));
solrDocs.setStart((Long) list.get(1));
solrDocs.setMaxScore((Float) list.get(2));
callback.streamDocListInfo(solrDocs.getNumFound(), solrDocs.getStart(), solrDocs.getMaxScore());
// Read the Array
tagByte = dis.readByte();
if ((tagByte >>> 5) != (ARR >>> 5)) {
throw new RuntimeException("doclist must have an array");
}
int sz = readSize(dis);
for (int i = 0; i < sz; i++) {
// must be a SolrDocument
readVal(dis);
}
return solrDocs;
}
};
return (NamedList<Object>) codec.unmarshal(body);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "parsing error", e);
}
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestBinaryResponseWriter method testUUID.
/**
* Tests known types implementation by asserting correct encoding/decoding of UUIDField
*/
public void testUUID() throws Exception {
String s = UUID.randomUUID().toString().toLowerCase(Locale.ROOT);
assertU(adoc("id", "101", "uuid", s));
assertU(commit());
LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*");
SolrQueryResponse rsp = h.queryAndResponse(req.getParams().get(CommonParams.QT), req);
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writer.write(baos, req, rsp);
NamedList res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
SolrDocumentList docs = (SolrDocumentList) res.get("response");
for (Object doc : docs) {
SolrDocument document = (SolrDocument) doc;
assertEquals("Returned object must be a string", "java.lang.String", document.getFieldValue("uuid").getClass().getName());
assertEquals("Wrong UUID string returned", s, document.getFieldValue("uuid"));
}
req.close();
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestBinaryResponseWriter method testResolverSolrDocumentPartialFields.
public void testResolverSolrDocumentPartialFields() throws Exception {
LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*", "fl", "id,xxx,ddd_s");
SolrDocument in = new SolrDocument();
in.addField("id", 345);
in.addField("aaa_s", "aaa");
in.addField("bbb_s", "bbb");
in.addField("ccc_s", "ccc");
in.addField("ddd_s", "ddd");
in.addField("eee_s", "eee");
Resolver r = new Resolver(req, new SolrReturnFields(req));
Object o = r.resolve(in, new JavaBinCodec());
assertNotNull("obj is null", o);
assertTrue("obj is not doc", o instanceof SolrDocument);
SolrDocument out = new SolrDocument();
for (Map.Entry<String, Object> e : in) {
if (r.isWritable(e.getKey()))
out.put(e.getKey(), e.getValue());
}
assertTrue("id not found", out.getFieldNames().contains("id"));
assertTrue("ddd_s not found", out.getFieldNames().contains("ddd_s"));
assertEquals("Wrong number of fields found", 2, out.getFieldNames().size());
req.close();
}
use of org.apache.solr.common.util.JavaBinCodec in project lucene-solr by apache.
the class TestJavabinTupleStreamParser method testSolrDocumentList.
public void testSolrDocumentList() throws IOException {
SolrQueryResponse response = new SolrQueryResponse();
SolrDocumentList l = constructSolrDocList(response);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new JavaBinCodec().marshal(response.getValues(), baos);
byte[] bytes = serialize(response.getValues());
Object o = new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes));
List list = new ArrayList<>();
Map m = null;
JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);
while ((m = parser.next()) != null) {
list.add(m);
}
assertEquals(l.size(), list.size());
for (int i = 0; i < list.size(); i++) {
compareSolrDocument(l.get(i), new SolrDocument((Map<String, Object>) list.get(i)));
}
}
Aggregations