Search in sources :

Example 1 with JavaBinCodec

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));
            }
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) Test(org.junit.Test)

Example 2 with JavaBinCodec

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);
    }
}
Also used : DataInputInputStream(org.apache.solr.common.util.DataInputInputStream) SolrDocument(org.apache.solr.common.SolrDocument) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SolrDocumentList(org.apache.solr.common.SolrDocumentList) NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 3 with JavaBinCodec

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();
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) NamedList(org.apache.solr.common.util.NamedList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 4 with JavaBinCodec

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();
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) Resolver(org.apache.solr.response.BinaryResponseWriter.Resolver) Map(java.util.Map) SolrReturnFields(org.apache.solr.search.SolrReturnFields) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 5 with JavaBinCodec

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)));
    }
}
Also used : ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) SmileWriterTest.constructSolrDocList(org.apache.solr.response.SmileWriterTest.constructSolrDocList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) JavabinTupleStreamParser(org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser)

Aggregations

JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)20 NamedList (org.apache.solr.common.util.NamedList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 Map (java.util.Map)6 SolrDocument (org.apache.solr.common.SolrDocument)6 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 SolrException (org.apache.solr.common.SolrException)5 InputStream (java.io.InputStream)4 List (java.util.List)4 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)4 SolrInputDocument (org.apache.solr.common.SolrInputDocument)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 ArrayList (java.util.ArrayList)3 SolrCore (org.apache.solr.core.SolrCore)3 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)3 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 FieldType (org.apache.solr.schema.FieldType)3