I am new to chronicle-map. I am trying to model an off heap map using chronicle-map where the key is a primitive short and the value is a primitive long array. The max size of the long array value is known for a given map. However I will have multiple maps of this kind each of which may have a different max size for the long array value. My question relates to the serialisation/deserialisation of the key and value.
From reading the documentation I understand that for the key I can use the value type ShortValue and reuse the instance of the implementation of that interface. Regarding the value I have found the page talking about DataAccess and SizedReader which gives an example for byte[] but I'm unsure how to adapt this to a long[]. One additional requirement I have is that I need to get and set values at arbitrary indices in the long array without paying the cost of a full serialisation/deserialisation of the entire value each time.
So my question is: how can I model the value type when constructing the map and what serialisation/deserialisation code do I need for a long[] array if the max size is known per map and I need to be able to read and write random indices without serialising/deserialising the entire value payload each time? Ideally the long[] would be encoded/decoded directly to/from off heap without undergoing an on heap intermediate conversion to a byte[] and also the chronicle-map code would not allocate at runtime. Thank you.
First, I recommend to use some kind of
LongListinterface abstraction instead oflong[], it will make it easier to deal with size variability, provide alternative flyweight implementations, etc.If you want to read/write just single elements in large lists, you should use advanced contexts API:
Notes:
writeLock()from the beginning, because otherwise readLock() is going to be acquired automatically when you callcontext.entry()method, and you won't be able to upgrade read lock to write lock later. Please readHashQueryContextjavadoc carefully.Data.bytes()formally returnsRandomDataInput, but you could be sure (it's specified inData.bytes()javadoc) that it's actually an instance ofBytesStore(that's combination ofRandomDataInputandRandomDataOutput).SizedReaderandSizedWriter(orDataAccess) are provided. Note that "bytes/element joint size" technique is used, the same as in the example given inSizedReaderandSizedWriterdoc section,PointListSizeMarshaller. You could base yourLongListMarshalleron that example class.ChecksumEntryjavadoc and the section about checksums in the doc. If you have a purely in-memory (not persisted) Chronicle Map, or turned checksums off, this call could be omitted.Implementation of single element read is similar.