org.apache.solr.common.util.JavaBinCodec Java Examples

The following examples show how to use org.apache.solr.common.util.JavaBinCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SolrSchemaFieldDao.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private List<LukeResponse> getLukeResponsesForCores(CloudSolrClient solrClient) {
  ZkStateReader zkStateReader = solrClient.getZkStateReader();
  Collection<Slice> activeSlices = zkStateReader.getClusterState().getCollection(solrClient.getDefaultCollection()).getActiveSlices();
  
  List<LukeResponse> lukeResponses = new ArrayList<>();
  for (Slice slice : activeSlices) {
    for (Replica replica : slice.getReplicas()) {
      try (CloseableHttpClient httpClient = HttpClientUtil.createClient(null)) {
        HttpGet request = new HttpGet(replica.getCoreUrl() + LUKE_REQUEST_URL_SUFFIX);
        HttpResponse response = httpClient.execute(request);
        @SuppressWarnings("resource") // JavaBinCodec implements Closeable, yet it can't be closed if it is used for unmarshalling only
        NamedList<Object> lukeData = (NamedList<Object>) new JavaBinCodec().unmarshal(response.getEntity().getContent());
        LukeResponse lukeResponse = new LukeResponse();
        lukeResponse.setResponse(lukeData);
        lukeResponses.add(lukeResponse);
      } catch (IOException e) {
        logger.error("Exception during getting luke responses", e);
      }
    }
  }
  return lukeResponses;
}
 
Example #2
Source File: TransactionLog.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public long writeDeleteByQuery(DeleteUpdateCommand cmd) {
  LogCodec codec = new LogCodec(resolver);
  try {
    checkWriteHeader(codec, null);

    MemOutputStream out = new MemOutputStream(new byte[20 + (cmd.query.length())]);
    codec.init(out);
    codec.writeTag(JavaBinCodec.ARR, 3);
    codec.writeInt(UpdateLog.DELETE_BY_QUERY);  // should just take one byte
    codec.writeLong(cmd.getVersion());
    codec.writeStr(cmd.query);

    synchronized (this) {
      long pos = fos.size();   // if we had flushed, this should be equal to channel.position()
      out.writeAll(fos);
      endRecord(pos);
      // fos.flushBuffer();  // flush later
      return pos;
    }
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }

}
 
Example #3
Source File: BinaryResponseWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * TODO -- there may be a way to do this without marshal at all...
 *
 * @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents become
 *         SolrDocuments, DocList becomes SolrDocumentList etc.
 *
 * @since solr 1.4
 */
@SuppressWarnings("unchecked")
public static NamedList<Object> getParsedResponse(SolrQueryRequest req, SolrQueryResponse rsp) {
  try {
    Resolver resolver = new Resolver(req, rsp.getReturnFields());

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
      jbc.setWritableDocFields(resolver).marshal(rsp.getValues(), out);
    }

    InputStream in = out.toInputStream();
    try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
      return (NamedList<Object>) jbc.unmarshal(in);
    }
  }
  catch (Exception ex) {
    throw new RuntimeException(ex);
  }
}
 
Example #4
Source File: BinaryResponseWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void writeResults(ResultContext ctx, JavaBinCodec codec) throws IOException {
  codec.writeTag(JavaBinCodec.SOLRDOCLST);
  List<Object> l = new ArrayList<>(4);
  l.add( ctx.getDocList().matches());
  l.add((long) ctx.getDocList().offset());
  
  Float maxScore = null;
  if (ctx.wantsScores()) {
    maxScore = ctx.getDocList().maxScore();
  }
  l.add(maxScore);
  l.add(ctx.getDocList().hitCountRelation() == TotalHits.Relation.EQUAL_TO);
  codec.writeArray(l);
  
  // this is a seprate function so that streaming responses can use just that part
  writeResultsBody( ctx, codec );
}
 
Example #5
Source File: EmbeddedSolrServer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private JavaBinCodec createJavaBinCodec(final StreamingResponseCallback callback, final BinaryResponseWriter.Resolver resolver) {
  return new JavaBinCodec(resolver) {

    @Override
    public void writeSolrDocument(SolrDocument doc) {
      callback.streamSolrDocument(doc);
      //super.writeSolrDocument( doc, fields );
    }

    @Override
    public void writeSolrDocumentList(SolrDocumentList docs) throws IOException {
      if (docs.size() > 0) {
        SolrDocumentList tmp = new SolrDocumentList();
        tmp.setMaxScore(docs.getMaxScore());
        tmp.setNumFound(docs.getNumFound());
        tmp.setStart(docs.getStart());
        docs = tmp;
      }
      callback.streamDocListInfo(docs.getNumFound(), docs.getStart(), docs.getMaxScore());
      super.writeSolrDocumentList(docs);
    }

  };
}
 
Example #6
Source File: TestJavabinTupleStreamParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public void testSolrDocumentList() throws IOException {
  SolrQueryResponse response = new SolrQueryResponse();
  SolrDocumentList l = constructSolrDocList(response);
  try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    jbc.marshal(response.getValues(), baos);
  }
  byte[] bytes = serialize(response.getValues());
  try (JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.unmarshal(new ByteArrayInputStream(bytes));
  }
  List list = new ArrayList<>();
  Map m = null;
  try (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)));
  }

}
 
Example #7
Source File: ZkNodePropsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic() throws IOException {
  
  Map<String,Object> props = new HashMap<>();
  props.put("prop1", "value1");
  props.put("prop2", "value2");
  props.put("prop3", "value3");
  props.put("prop4", "value4");
  props.put("prop5", "value5");
  props.put("prop6", "value6");
  
  ZkNodeProps zkProps = new ZkNodeProps(props);
  byte[] bytes = Utils.toJSON(zkProps);
  ZkNodeProps props2 = ZkNodeProps.load(bytes);

  props.forEach((s, o) -> assertEquals(o, props2.get(s)));
  SimplePostTool.BAOS baos = new SimplePostTool.BAOS();
  try (JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.marshal(zkProps.getProperties(), baos);
  }
  bytes = baos.toByteArray();
  System.out.println("BIN size : " + bytes.length);
  ZkNodeProps props3 = ZkNodeProps.load(bytes);
  props.forEach((s, o) -> assertEquals(o, props3.get(s)));
}
 
Example #8
Source File: TestUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testBinaryCommands() throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (final JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.marshal((MapWriter) ew -> {
      ew.put("set-user", fromJSONString("{x:y}"));
      ew.put("set-user", fromJSONString("{x:y,x1:y1}"));
      ew.put("single", asList(fromJSONString("[{x:y,x1:y1},{x2:y2}]"), fromJSONString( "{x2:y2}")));
      ew.put("multi", asList(fromJSONString("{x:y,x1:y1}"), fromJSONString( "{x2:y2}")));
    }, baos);
  }

  ContentStream stream = new ContentStreamBase.ByteArrayStream(baos.toByteArray(),null, "application/javabin");
  @SuppressWarnings({"rawtypes"})
  List<CommandOperation> commands = CommandOperation.readCommands(Collections.singletonList(stream), new NamedList(), Collections.singleton("single"));

  assertEquals(5, commands.size());
}
 
Example #9
Source File: TestJavabinTupleStreamParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static byte[] serialize(Object o) throws IOException {
  SolrQueryResponse response = new SolrQueryResponse();
  response.getValues().add("results", o);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.marshal(response.getValues(), baos);
  }
  return baos.toByteArray();
}
 
Example #10
Source File: TestBinaryResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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();

}
 
Example #11
Source File: ExportTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void end() throws IOException {
  codec.writeTag(JavaBinCodec.END);
  codec.close();
  fos.flush();
  fos.close();

}
 
Example #12
Source File: ExportTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws IOException {
  fos = new FileOutputStream(info.out);
  if(info.out.endsWith(".json.gz") || info.out.endsWith(".json.")) fos = new GZIPOutputStream(fos);
  if (info.bufferSize > 0) {
    fos = new BufferedOutputStream(fos, info.bufferSize);
  }
  codec = new JavaBinCodec(fos, null);
  codec.writeTag(JavaBinCodec.NAMED_LST, 2);
  codec.writeStr("params");
  codec.writeNamedList(new NamedList<>());
  codec.writeStr("docs");
  codec.writeTag(JavaBinCodec.ITERATOR);

}
 
Example #13
Source File: JavaBinCodecCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(T value, OutputStream outStream) throws IOException {
  if (value == null) {
    throw new CoderException("cannot encode a null SolrDocument");
  }

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  JavaBinCodec codec = new JavaBinCodec();
  codec.marshal(value, baos);

  byte[] bytes = baos.toByteArray();
  VarInt.encode(bytes.length, outStream);
  outStream.write(bytes);
}
 
Example #14
Source File: JavabinLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void handleMultiStream(SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, UpdateRequestProcessor processor)
    throws IOException {
  FastInputStream in = FastInputStream.wrap(stream);
  SolrParams old = req.getParams();
  try (JavaBinCodec jbc = new JavaBinCodec() {
    SolrParams params;
    AddUpdateCommand addCmd = null;

    @Override
    public List<Object> readIterator(DataInputInputStream fis) throws IOException {
      while (true) {
        Object o = readVal(fis);
        if (o == END_OBJ) break;
        if (o instanceof NamedList) {
          params = ((NamedList) o).toSolrParams();
        } else {
          try {
            if (o instanceof byte[]) {
              if (params != null) req.setParams(params);
              byte[] buf = (byte[]) o;
              contentStreamLoader.load(req, rsp, new ContentStreamBase.ByteArrayStream(buf, null), processor);
            } else {
              throw new RuntimeException("unsupported type ");
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          } finally {
            params = null;
            req.setParams(old);
          }
        }
      }
      return Collections.emptyList();
    }

  }) {
    jbc.unmarshal(in);
  }
}
 
Example #15
Source File: JavaBinCodecCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(InputStream inStream) throws IOException {
  DataInputStream in = new DataInputStream(inStream);

  int len = VarInt.decodeInt(in);
  if (len < 0) {
    throw new CoderException("Invalid encoded SolrDocument length: " + len);
  }

  JavaBinCodec codec = new JavaBinCodec();
  return (T) codec.unmarshal(new BoundedInputStream(in, len));
}
 
Example #16
Source File: SolrInputDocumentWritable.java    From examples with Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutput out) throws IOException {
  JavaBinCodec codec = new JavaBinCodec();
  FastOutputStream daos = FastOutputStream.wrap(DataOutputOutputStream.constructOutputStream(out));
  codec.init(daos);
  try {
    codec.writeVal(sid);
  } finally {
    daos.flushBuffer();
  }
}
 
Example #17
Source File: CursorMark.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a Base64 encoded serialized representation of the sort values 
 * encapsulated by this object, for use in cursor requests.
 *
 * @see #parseSerializedTotem
 */
public String getSerializedTotem() {
  if (null == this.values) {
    return CURSOR_MARK_START;
  }

  final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
  final ArrayList<Object> marshalledValues = new ArrayList<>(values.size()+1);
  for (int i = 0; i < schemaFields.size(); i++) {
    SchemaField fld = schemaFields.get(i);
    Object safeValue = values.get(i);
    if (null != fld) {
      FieldType type = fld.getType();
      safeValue = type.marshalSortValue(safeValue);
    }
    marshalledValues.add(safeValue);
  }

  // TODO: we could also encode info about the SortSpec for error checking:
  // the type/name/dir from the SortFields (or a hashCode to act as a checksum) 
  // could help provide more validation beyond just the number of clauses.

  try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {
    jbc.marshal(marshalledValues, out);
    byte[] rawData = out.toByteArray();
    return Base64.byteArrayToBase64(rawData, 0, rawData.length);
  } catch (Exception ex) {
    throw new SolrException(ErrorCode.SERVER_ERROR,
                            "Unable to format search after totem", ex);
  }
}
 
Example #18
Source File: HdfsTransactionLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public long writeCommit(CommitUpdateCommand cmd) {
  LogCodec codec = new LogCodec(resolver);
  synchronized (this) {
    try {
      long pos = fos.size();   // if we had flushed, this should be equal to channel.position()

      if (pos == 0) {
        writeLogHeader(codec);
        pos = fos.size();
      }
      
      codec.init(fos);
      codec.writeTag(JavaBinCodec.ARR, 3);
      codec.writeInt(UpdateLog.COMMIT);  // should just take one byte
      codec.writeLong(cmd.getVersion());
      codec.writeStr(END_MESSAGE);  // ensure these bytes are (almost) last in the file

      endRecord(pos);
      
      ensureFlushed();  // flush since this will be the last record in a log fill

      // now the commit command is written we will never write to this log again
      closeOutput();

      //assert fos.size() == channel.size();

      return pos;
    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
    }
  }
}
 
Example #19
Source File: JavaBinUpdateRequestCodec.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
@SuppressWarnings({"unchecked"})
public void marshal(UpdateRequest updateRequest, OutputStream os) throws IOException {
  @SuppressWarnings({"rawtypes"})
  NamedList nl = new NamedList();
  @SuppressWarnings({"rawtypes"})
  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();

  nl.add("params", params);// 0: 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);
  }
  try (JavaBinCodec codec = new JavaBinCodec()) {
    codec.marshal(nl, os);
  }
}
 
Example #20
Source File: TransactionLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(Object o, JavaBinCodec codec) throws IOException {
  if (o instanceof BytesRef) {
    BytesRef br = (BytesRef) o;
    codec.writeByteArray(br.bytes, br.offset, br.length);
    return null;
  }
  // Fallback: we have no idea how to serialize this.  Be noisy to prevent insidious bugs
  throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
          "TransactionLog doesn't know how to serialize " + o.getClass() + "; try implementing ObjectResolver?");
}
 
Example #21
Source File: BinaryResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void writeResultsBody( ResultContext res, JavaBinCodec codec ) throws IOException {
  codec.writeTag(JavaBinCodec.ARR, res.getDocList().size());
  Iterator<SolrDocument> docStreamer = res.getProcessedDocuments();
  while (docStreamer.hasNext()) {
    SolrDocument doc = docStreamer.next();
    codec.writeSolrDocument(doc);
  }
}
 
Example #22
Source File: MultiContentWriterRequest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
  return new RequestWriter.ContentWriter() {
    @Override
    @SuppressWarnings({"unchecked"})
    public void write(OutputStream os) throws IOException {
      new JavaBinCodec().marshal((IteratorWriter) iw -> {
        while (payload.hasNext()) {
          @SuppressWarnings({"rawtypes"})
          Pair<NamedList, Object> next = payload.next();

          if (next.second() instanceof ByteBuffer || next.second() instanceof byte[]) {
            @SuppressWarnings({"rawtypes"})
            NamedList params = next.first();
            if(params.get(ASSUME_CONTENT_TYPE) == null){
              String detectedType = detect(next.second());
              if(detectedType==null){
                throw new RuntimeException("Unknown content type");
              }
              params.add(ASSUME_CONTENT_TYPE, detectedType);
            }
            iw.add(params);
            iw.add(next.second());
          }  else {
            throw new RuntimeException("payload value must be byte[] or ByteBuffer");
          }
        }
      }, os);
    }

    @Override
    public String getContentType() {
      return "application/javabin";
    }
  };
}
 
Example #23
Source File: BinaryResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void serialize(SolrQueryResponse response,Resolver resolver, String f) throws IOException {
  try (JavaBinCodec jbc = new JavaBinCodec(resolver); FileOutputStream fos = new FileOutputStream(f)) {
    jbc.setWritableDocFields(resolver).marshal(response.getValues(), fos);
    fos.flush();
  }

}
 
Example #24
Source File: BinaryResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException {
  Resolver resolver = new Resolver(req, response.getReturnFields());
  if (req.getParams().getBool(CommonParams.OMIT_HEADER, false)) response.removeResponseHeader();
  try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {
    jbc.setWritableDocFields(resolver).marshal(response.getValues(), out);
  }
}
 
Example #25
Source File: RawValueTransformerFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(Object o, JavaBinCodec codec) throws IOException {
  ObjectResolver orig = codec.getResolver();
  if(orig != null) {
    codec.writeVal(orig.resolve(val, codec));
    return null;
  }
  return val.toString();
}
 
Example #26
Source File: CdcrTransactionLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public long writeDeleteByQuery(DeleteUpdateCommand cmd) {
  LogCodec codec = new LogCodec(resolver);
  try {
    checkWriteHeader(codec, null);

    MemOutputStream out = new MemOutputStream(new byte[20 + (cmd.query.length())]);
    codec.init(out);
    codec.writeTag(JavaBinCodec.ARR, 4);
    codec.writeInt(UpdateLog.DELETE_BY_QUERY);  // should just take one byte
    codec.writeLong(cmd.getVersion());
    codec.writeStr(cmd.query);
    if (cmd.getReq().getParamString().contains(CdcrUpdateProcessor.CDCR_UPDATE)) {
      // if the update is received via cdcr source; add extra boolean entry
      // CdcrReplicator.isTargetCluster() checks that particular boolean to accept or discard the update
      // to forward to its own target cluster
      codec.writePrimitive(true);
    } else {
      codec.writePrimitive(false);
    }
    synchronized (this) {
      long pos = fos.size();   // if we had flushed, this should be equal to channel.position()
      out.writeAll(fos);
      endRecord(pos);
      // fos.flushBuffer();  // flush later
      return pos;
    }
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }
}
 
Example #27
Source File: V2Request.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public RequestWriter.ContentWriter getContentWriter(String s) {
  if (v2Calls.get() != null) v2Calls.get().incrementAndGet();
  if (payload == null) return null;
  if (payload instanceof String) {
    return new RequestWriter.StringPayloadContentWriter((String) payload, JSON_MIME);
  }
  return new RequestWriter.ContentWriter() {
    @Override
    public void write(OutputStream os) throws IOException {
      if (payload instanceof ByteBuffer) {
        ByteBuffer b = (ByteBuffer) payload;
        os.write(b.array(), b.arrayOffset(), b.limit());
        return;
      }
      if (payload instanceof InputStream) {
        IOUtils.copy((InputStream) payload, os);
        return;
      }
      if (useBinary) {
        new JavaBinCodec().marshal(payload, os);
      } else {
        Utils.writeJson(payload, os, false);
      }
    }

    @Override
    public String getContentType() {
      if (mimeType != null) return mimeType;
      return useBinary ? JAVABIN_MIME : JSON_MIME;
    }
  };
}
 
Example #28
Source File: TestUpdateRequestCodec.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void testStreamableInputDocFormat() throws IOException {
  @SuppressWarnings({"rawtypes"})
  Map m = Utils.makeMap("id","1","desc" ,"The desc 1");
  m.put(CHILDDOC, (MapWriter) ew -> {
    ew.put("id","1.1");
    ew.put("desc" ,"The desc 1.1");
    ew.put(CHILDDOC, (IteratorWriter) iw -> {
      iw.add(Utils.makeMap("id", "1.1.1","desc","The desc 1.1.1"));
      iw.add((MapWriter) ew1 -> {
        ew1.put("id", "1.1.2");
        ew1.put("desc", "The desc 1.1.2");
      });
    });
  });
  MapWriter m2 = ew -> {
    ew.put("id", "2");
    ew.put("des", "The desc 2");
  };

  @SuppressWarnings({"rawtypes"})
  List l = new ArrayList();
  l.add(m);
  l.add(m2);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  new JavaBinCodec().marshal(l.iterator(), baos);

  List<SolrInputDocument>  l2 = new ArrayList<>();

  new JavaBinUpdateRequestCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()), (document, req, commitWithin, override) -> l2.add(document));

 assertEquals(l2.get(0).getChildDocuments().size(), 1);

 Object o = Utils.fromJSONString(Utils.writeJson(l.get(0), new StringWriter(), true).toString());
 Object cdoc = Utils.getObjectByPath(o, false, CHILDDOC);
 assertEquals(Utils.writeJson(cdoc, new StringWriter(), true).toString(),
     Utils.writeJson(l2.get(0).getChildDocuments().get(0) ,new StringWriter(), true).toString());

}
 
Example #29
Source File: ZkNodeProps.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Create Replica from json string that is typically stored in zookeeper.
 */
@SuppressWarnings({"unchecked"})
public static ZkNodeProps load(byte[] bytes) {
  Map<String, Object> props = null;
  if (bytes[0] == 2) {
    try (JavaBinCodec jbc = new JavaBinCodec()) {
      props = (Map<String, Object>) jbc.unmarshal(bytes);
    } catch (IOException e) {
      throw new RuntimeException("Unable to parse javabin content");
    }
  } else {
    props = (Map<String, Object>) Utils.fromJSON(bytes);
  }
  return new ZkNodeProps(props);
}
 
Example #30
Source File: TestSubQueryTransformer.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@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);

  try (JavaBinCodec jbc = new JavaBinCodec()) {
    unmarshalled = (NamedList<Object>) jbc.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));
        }
    }
  }
}