Search code examples
chroniclechronicle-map

Chronicle Map support for arrays of primitives?


Suppose I have smth like:

 public interface ITest {
    long[] getDataArray();

    void setDataArray(long[] data);
  }

In this case because of long[] looks like I cannot use

Values.newHeapInstance(ITest.class);

(I am getting field type class [Lnet.openhft.chronicle.core.values.LongValue; is not supported: not a primitive, enum, CharSequence or another value interface Exception)

Sounds strange am I missing smth? What is the best approach to work with such objects? Implement own serialization?


Solution

  • Chronicle Values are designed as constant-sized structures. If you need to store some variable-sized fields e. g. CharSequences (including Strings) or arrays, you should specify the maximum size they might be, this maximum size will always be allocated for the field, so if you store shorter CharSequences or arrays, you waste some memory space as unreclmained.

    If that is what you need, e. g. if all your arrays are actually of the same length and you wouldn't waste any space, or array lengths vary marginally, or you are OK to waste some space sometimes for other benefits that value interfaces as Chronicle Map's keys or values provide, you can have array fields in a value interface as follows:

    public interface ITest {
        @Array(length=CONSTANT_OR_MAX_ARRAY_LENGTH)
        long getDataAt(int index);
        void setDataAt(int index, long data);
    }
    

    See https://github.com/OpenHFT/Chronicle-Values#array-fields