simple-binary-encoding icon indicating copy to clipboard operation
simple-binary-encoding copied to clipboard

Add `copy()` to message decoders

Open ratcashdev opened this issue 1 year ago • 3 comments
trafficstars

Flyweights are mutable by nature. However, sometimes it is quite handy to make them 'frozen' or immutable.

For this reason, we could be adding a method like copy() like below to the SBE (JAVA) generator template and to agrona's MessageDecoderFlyweight interface:

public <T extends MessageDecoderFlyweight> T copy() {
    int version = this.actingVersion();
    int blockLength = this.actingBlockLength();
    sbeSkip();
    final int length = this.encodedLength();
    sbeRewind();
    final var dst = new ExpandableArrayBuffer(length);
    dst.putBytes(0, this.buffer(), this.offset(), length);
    return new (class).wrap(dst, 0, blockLength, version);
  }

Volunteering for a PR, if this gets accepted.

ratcashdev avatar Jan 15 '24 11:01 ratcashdev

This is a copy of the decoder and the underlying buffer. To be more in keeping with the SBE design it would makes sense to provide a decoder and buffer as a destination.

We have an upcoming feature to have DTOs created from codecs that may be more appropriate.

mjpt777 avatar Jan 18 '24 14:01 mjpt777

To be more in keeping with the SBE design it would makes sense to provide a decoder and buffer as a destination.

.. or have both options available. Assuming such a copy(...) method was exposed by the MessageDecoderFlyweight interface, it would make creating copies on interface level a tad difficult - it would mean new instances would need to be created through reflection, cough, cough.

ad DTO: sounds interesting. Is there an issue for this?

ratcashdev avatar Jan 18 '24 16:01 ratcashdev

This will also be done for Java. https://github.com/real-logic/simple-binary-encoding/pull/957

mjpt777 avatar Jan 18 '24 17:01 mjpt777