On Tue, Dec 5, 2017 at 2:27 PM, <[email protected]> wrote:

> Thanks a lot. I got it. In my case, I will always read the compressed byte
> array back from KV store, decompress and then read fields. So, in this
> case, "random access" means Cap'N will only create the object of that field
> from unpacked message without creating the temp objects of other fields, in
> other word, all other fields will still be the flat bytes without any
> managed objects created. Is that correct?
>

Yes. However, if you're reading *packed* messages, then packed bytes do
need to be unpacked upfront. They are unpacked into another ByteBuffer. No
message objects are created, but this does require reading through all the
bytes.

The memory mapping strategy I described does not work for packed messages.


> Moreover, another question is how to write message in packed format to a
> byte array. Because I have to allocate a ByteBuffer will enough capacity to
> store the message. But it is not possible to know the packed message size
> without packing it first. Currently, I have to allocate with its unpacked
> size (computeSerializedSizeInWords * 8), then use a tricky way to trim the
> tailing zeros. Do you know if there is any better way to do this?
>

The only way to know the packed size is to actually run the packing
algorithm. You could run the algorithm twice, once where you throw away the
data just to get the size, and then another time to save it. Or, you could
allocate successive buffers on-demand, and then assemble them into one big
buffer at the end. Or, if you're going to write to an OutputStream anyway,
write the bytes to the OutputStream as they are being packed, rather than
packing everything first and writing second.

-Kenton

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to