简体   繁体   中英

Generating unique IDs for multiple sources

Consider N sources of data, each with a stream of events

Event{
    long id;
    Object data;
}

Some of the events within one stream might have the same id, as events might span across Updated, New etc. So we can see the following two streams:

<1, 2, 3, 1, 5, 2>
<3, 3, 4, 5, 4>

I would now like to combine these into one stream st each order id is definitely going to be unique.

The easy way would be to use a String instead of long and append source number, generating sth like:

<"1 - 1", "1 - 2", "1 - 3", "2-3", "2-3" ... >

Is there a more memory coimpact way/better approach?

Your String solution is fine and in fact quite common. If you're interested in making it more compact, you may want to use a tuple of integers.

Another common method used in distributed systems is to use range allocation: have a central (singleton) server which allocates ranges in which each client can name its IDs. Such server could allocate, for example, the range 0-99 to client1, 100-199 to client2 etc. When a client exhausts the range it was allocated, it contacts the server again to allocate a new range.

Depending on the ranges of your stream/event numbers, you could combine the two numbers into a single int or long, placing the stream number in the top so many bits and the event number in the bottom so many bits. For example:

public static int getCombinedNo(int streamNo, int eventNo) {
    if (streamNo >= (1 << 16))
      throw new IllegalArgumentException("Stream no too big");
    if (eventNo >= (1 << 16))
      throw new IllegalArgumentException("Event no too big");
    return (streamNo << 16) | eventNo;
}

This will only use 4 bytes per int as opposed to in the order of (say) 50-ish bytes for a typical String of the type you mention. (In this case, it also assumes that neither stream nor event number will exceed 65535.)

But: your string solution is also nice and clear. Is memory really that tight that you can't spare an extra 50 bytes per event?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM