1

我正在使用以下代码将 byte[] 写入编年史队列,

excerptAppender.writeBytes(b -> b.write(data));

如何从队列中读回相同的 byte[]。我发现了这样的东西,

excerptTailer.readBytes(b-> b.read(bytes));

但在这种情况下,我需要长度。我是否需要单独编写长度并读取相同的长度以创建字节 []。?

或者有没有办法让框架本身处理长度,这样我们就可以读起来像,

excerptTailer.readBytes();

我找不到太多关于此的文档。

从 github 得到这个样本,

    assertTrue(tailer.readBytes(b -> {
    long address = b.address(b.readPosition());
    Unsafe unsafe = UnsafeMemory.UNSAFE;
    int code = unsafe.getByte(address);
    address++;
    int num = unsafe.getInt(address);
    address += 4;
    long num2 = unsafe.getLong(address);
    address += 8;
    int length = unsafe.getByte(address);
    address++;
    byte[] bytes = new byte[length];
    unsafe.copyMemory(null, address, bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET, bytes.length);
    String text = new String(bytes, StandardCharsets.UTF_8);
    assertEquals("Hello World", text);
    // do something with values
}));

这是否推荐用于生产。?

4

1 回答 1

0

很抱歉回复这么老的帖子,以为有人可以从中受益。

您可以定义大约一次缓冲区大小(相当大),框架提供实际数据长度。看看下面对我有用的代码

  private byte[] readData() {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary("./temp/").build();
    ExcerptTailer tailer = queue.createTailer("my-single-tailer");
    byte[] data = null;
    Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(1024 * 128);
    boolean read = tailer.readBytes(bytes);
    if (read) {
      byte[] readData = bytes.underlyingObject().array();
      int len = (int) bytes.readRemaining();
      bytes.clear();
      data = Arrays.copyOf(readData, len);
    }
    return data;
  }

示例写入数据代码也将是

private void writeData(byte[] data) {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary("./temp/").rollCycle(RollCycles.HOURLY).build();
    ExcerptAppender appender = queue.acquireAppender();

    Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer(1024 * 128);
    bytes.ensureCapacity(data.length);

    ByteBuffer byteBuffer = bytes.underlyingObject();
    byteBuffer.put(data);
    bytes.readPositionRemaining(0, byteBuffer.position());
    appender.writeBytes(bytes);
    byteBuffer.clear();
  }
于 2019-11-07T12:01:08.307 回答