我使用以下代码将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
}));
这是推荐用于生产吗?
答案 0 :(得分: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();
}