V_B V_B - 6 months ago 24
Java Question

Vertx: pass initial data to verticle on it deployment

How can I pass copy of

array to verticle on it deployment?

I have a
from which deploy 5-10

Each of
must use the same shared data structure -
Map<Integer, Short[]>
which can be 100-2000 Mb.

Problem - I can't create
map with array as a value.

The only in-memory solution I see - pass copy of
to each
on it deployment and keep 5-10 copies of data.

P.S. This data structure must have as fast as possible lookup, so I dislike cluster-wide solutions like Hazelcast


While there isn't much freedom in the types you can use in a LocalMap you can use Buffers. A buffer is an optimized byte array and you can quickly adapt it to your use case. Using a Buffer also means you will have a compact in memory representation so you can save memory and any operations will be fast.

You only need to write a transformation from a 2D plane to a 1D line. For example say that you have the following array (2 x 3):

int[][] data = new int[] {
  new int[] {1, 2, 3},
  new int[] {4, 5, 6},

If you transform it to a buffer:


(You can later just use the byte representation, this is just to illustrate how it works).

Now you can refer to any x, y by doing:

int getInt(int x, int y) {
  // transform from 2D to 1D
  int pos = y * LENGTH + x;
  // Where LENGTH is the the example: 3

  // For safety assert on length
  if (pos > buffer.length()) throw new ArrayIndexOutOfBoundsException();
  // TODO: assert on min, max (x, y)

  return buffer.getInt(pos);