2

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?

leventov
  • 14,760
  • 11
  • 69
  • 98

1 Answers1

1

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

leventov
  • 14,760
  • 11
  • 69
  • 98
  • Thanks Roman, that is what I was looking for. Actually it is a bit strange that I was not able just google that info. – Konstantin Kulagin Apr 08 '16 at 14:40
  • Quick follow up question: if I have an interface with fixed size array - is Map smart enough to calculate its size same as it does for LongValue for example? So I would not have to create a 'dummy' sample object and pass it to Builder? – Konstantin Kulagin Apr 08 '16 at 15:34
  • @KonstantinKulagin, yes, Chronicle Map knows about value interfaces and you don't need to specify value size yourself if the value type is a value interface. – leventov Apr 08 '16 at 16:26
  • @leventov is there any way to specify the array length at run time? – jayesh Jul 13 '17 at 13:08
  • @jayesh this is not possible using Chronicle-Values, but you create [custom serializers](https://github.com/OpenHFT/Chronicle-Map#custom-serializers) to achieve exactly that, just with more boilerplate code. – leventov Jul 14 '17 at 17:05
  • @leventov Thanks..Is it possible to update the array at a specific index and retrieving the value from an array at given index without serializing/deserializing the whole array using custom serializer? I am trying to use IntValue as a key and variable size array of double as value. Which custom serialization interface best suits this use case? – jayesh Jul 15 '17 at 08:36
  • @jayesh yes, this is possible. You should use [`DataAccess` and `SizedReader`](https://github.com/OpenHFT/Chronicle-Map#dataaccess-and-sizedreader) interfaces. Ser/deser "of the whole value" will still happen, but in fact it is just updates of some pointers in the heap, not copying data from off-heap Chronicle Map's memory and back. Also note that you should *disable* entry checksums: https://github.com/OpenHFT/Chronicle-Map#entry-checksums – leventov Jul 17 '17 at 11:45