简体   繁体   English

使用编年史地图和byte []或字节缓冲区

[英]Using chronicle map and byte[] or byte buffer

I am trying to create a Chronicle map with a Long key and a variable sized byte[], a serialized Java BitSet to be precise. 我正在尝试使用长键和可变大小的byte [](准确地说是序列化的Java BitSet)创建Chronicle映射。 I can create a map at using Values interface, but the size of the array in it is fixed. 我可以使用Values接口创建一个映射,但是其中的数组大小是固定的。

So, I am looking to use byte[] or Bytebuffer since they are dynamically sized, thus saving memory. 因此,由于它们具有动态大小,因此我希望使用byte []或Bytebuffer,从而节省了内存。 Is this a supported use case? 这是受支持的用例吗? Is there an example of using chronicle map with byte[] or ByteBuffer value classes? 有没有使用带有byte []或ByteBuffer值类的历史记录映射的示例? The following code fails 以下代码失败

ChronicleMap<Long, ByteBuffer> map = ChronicleMap
    .of(Long.class, ByteBuffer.class)
    .name("shard_map")
    .averageValueSize(1000)
    .entries(1_000_000)
    .create();

ByteBuffer ts2 = ByteBuffer.allocateDirect(10);
ts2.putInt(10);
map.put(1L, ts2);
System.out.println(map.get(1L).getInt());

with the error: 与错误:

Exception in thread "main" java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:506)
    at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:361)

I have tried creating the value object with Values.newHeapInstance, but that fails with the error: 我尝试使用Values.newHeapInstance创建值对象,但失败并显示以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: class java.nio.ByteBuffer is not an interface nor a generated class native or heap class
    at net.openhft.chronicle.values.ValueModel.notValueInterfaceOfImpl(ValueModel.java:68)
    at net.openhft.chronicle.values.ValueModel.lambda$acquire$0(ValueModel.java:64)
    at net.openhft.chronicle.values.ValueModel.doSomethingForInterfaceOr(ValueModel.java:85)
    at net.openhft.chronicle.values.ValueModel.acquire(ValueModel.java:63)
    at net.openhft.chronicle.values.Values.heapClassFor(Values.java:68)
    at net.openhft.chronicle.values.Values.newHeapInstance(Values.java:37)

The documentation says that byte[] and ByteBuffer are supported out of the box, but I couldn't find a working example for it, so decided to ask here. 该文档说,开箱即用地支持byte []和ByteBuffer,但是我找不到适用的示例,因此决定在此处询问。

Your test works on my machine (ends without error), but prints unexpected output "0". 您的测试可在我的机器上运行(结束无错误),但输出意外的输出“ 0”。 That's because you forgot to call ts2.flip() , the code should look like: 这是因为您忘记了调用ts2.flip() ,代码应如下所示:

ts2.putInt(10);
ts2.flip();
map.put(1L, ts2);

With this change, the snippet prints "10". 进行此更改后,代码段将打印“ 10”。

Out of the box, Chronicle Map serializes ByteBuffer's contents between ByteBuffer's position and limit. Chronicle Map开箱即用,在ByteBuffer的位置和限制之间序列化ByteBuffer的内容。 You could override this in your custom serializer, and write the whole buffer. 您可以在自定义序列化程序中覆盖它,然后写入整个缓冲区。

BTW, in your case I don't recommend to use small direct buffer just to put a value into Chronicle Map (ts2). 顺便说一句,在您的情况下,我建议您不要使用小型直接缓冲区,而只是将一个值放入Chronicle Map(ts2)中。 Use an ordinary heap buffer for this. 为此使用普通的堆缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM