Search in sources :

Example 1 with JavabinTupleStreamParser

use of org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser in project lucene-solr by apache.

the class TestJavabinTupleStreamParser method testSimple.

public void testSimple() throws IOException {
    List<Map<String, Object>> l = new ArrayList();
    l.add(Utils.makeMap("id", 1, "f", 1.0f, "s", "Some str 1"));
    l.add(Utils.makeMap("id", 2, "f", 2.0f, "s", "Some str 2"));
    l.add(Utils.makeMap("id", 3, "f", 1.0f, "s", "Some str 3"));
    l.add(Utils.makeMap("EOF", true, "RESPONSE_TIME", 206, "sleepMillis", 1000));
    Iterator<Map<String, Object>> iterator = l.iterator();
    TupleStream tupleStream = new TupleStream() {

        @Override
        public void setStreamContext(StreamContext context) {
        }

        @Override
        public List<TupleStream> children() {
            return null;
        }

        @Override
        public void open() throws IOException {
        }

        @Override
        public void close() throws IOException {
        }

        @Override
        public Tuple read() throws IOException {
            if (iterator.hasNext())
                return new Tuple(iterator.next());
            else
                return null;
        }

        @Override
        public StreamComparator getStreamSort() {
            return null;
        }

        @Override
        public Explanation toExplanation(StreamFactory factory) throws IOException {
            return new StreamExplanation(getStreamNodeId().toString()).withFunctionName("Dummy").withImplementingClass(this.getClass().getName()).withExpressionType(Explanation.ExpressionType.STREAM_SOURCE).withExpression("--non-expressible--");
        }
    };
    byte[] bytes = serialize(tupleStream);
    JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), true);
    Map m = parser.next();
    assertEquals(1L, m.get("id"));
    assertEquals(1.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(2L, m.get("id"));
    assertEquals(2.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(3L, m.get("id"));
    assertEquals(1.0, (Double) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(Boolean.TRUE, m.get("EOF"));
    parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);
    m = parser.next();
    assertEquals(1, m.get("id"));
    assertEquals(1.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(2, m.get("id"));
    assertEquals(2.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(3, m.get("id"));
    assertEquals(1.0, (Float) m.get("f"), 0.01);
    m = parser.next();
    assertEquals(Boolean.TRUE, m.get("EOF"));
}
Also used : StreamExplanation(org.apache.solr.client.solrj.io.stream.expr.StreamExplanation) TupleStream(org.apache.solr.client.solrj.io.stream.TupleStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamContext(org.apache.solr.client.solrj.io.stream.StreamContext) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple) JavabinTupleStreamParser(org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser)

Example 2 with JavabinTupleStreamParser

use of org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser in project lucene-solr by apache.

the class TestJavabinTupleStreamParser method testKnown.

public void testKnown() throws IOException {
    String payload = "{\n" + "  \"responseHeader\":{\n" + "    \"zkConnected\":true,\n" + "    \"status\":0,\n" + "    \"QTime\":46},\n" + "  \"response\":{\n" + "    \"numFound\":2,\n" + "    \"start\":0,\n" + "    \"docs\":[\n" + "      {\n" + "        \"id\":\"2\",\n" + "        \"a_s\":\"hello2\",\n" + "        \"a_i\":2,\n" + "        \"a_f\":0.0},\n" + "      {\n" + "        \"id\":\"3\",\n" + "        \"a_s\":\"hello3\",\n" + "        \"a_i\":3,\n" + "        \"a_f\":3.0}]}}";
    SimpleOrderedMap nl = convert2OrderedMap((Map) Utils.fromJSONString(payload));
    byte[] bytes = serialize(nl);
    JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), true);
    Map<String, Object> map = parser.next();
    assertEquals("2", map.get("id"));
    map = parser.next();
    assertEquals("3", map.get("id"));
    System.out.println();
    map = parser.next();
    assertNull(map);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) JavabinTupleStreamParser(org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser)

Example 3 with JavabinTupleStreamParser

use of org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser 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

ByteArrayInputStream (java.io.ByteArrayInputStream)3 JavabinTupleStreamParser (org.apache.solr.client.solrj.io.stream.JavabinTupleStreamParser)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 List (java.util.List)1 Tuple (org.apache.solr.client.solrj.io.Tuple)1 StreamContext (org.apache.solr.client.solrj.io.stream.StreamContext)1 TupleStream (org.apache.solr.client.solrj.io.stream.TupleStream)1 StreamExplanation (org.apache.solr.client.solrj.io.stream.expr.StreamExplanation)1 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)1 SmileWriterTest.constructSolrDocList (org.apache.solr.response.SmileWriterTest.constructSolrDocList)1