VB_
VB_

Reputation: 45692

Vertx: pass initial data to verticle on it deployment

How can I pass copy of int[][] array to verticle on it deployment?

I have a ServerVerticle from which deploy 5-10 ServiceVerticles.

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

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

The only in-memory solution I see - pass copy of int[][] to each ServiceVerticle 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 IMap.

Upvotes: 0

Views: 215

Answers (1)

Paulo Lopes
Paulo Lopes

Reputation: 5801

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:

Buffer.buffer()
  .appendInt(1).appendInt(2).appendInt(3)
  .appendInt(4).appendInt(5).appendInt(6);

(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);
}

Upvotes: 1

Related Questions