Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID shortening

Tags:

java

uuid

redis

I need to calculate the daily number of unique users of an app.

The only way I can uniquely identify a user is via their UUID (this is externally supplied so I am forced to use it).

I know that my daily user counts are a couple of million users.

I'd like to use a bitset in Redis to do a population count, but in order for this to work, I'd need a way of narrowing my UUID so that it could comfortably fit into a long. I am aware of the potential for collisions but I am not concerned about precise numbers.

Has anyone done this in Java before? What I am after is how I could convert my UUID into something that could fit into a long.

like image 327
seedhead Avatar asked Feb 20 '23 20:02

seedhead


2 Answers

There are two methods on the UUID object which might benefit you.

getLeastSignificantBits() and getMostSignificateBits(). Both return a long. Take one of these longs as your answer (or some kind of combination if you care.)

like image 189
Starkey Avatar answered Feb 26 '23 10:02

Starkey


you could generate a hash of your uuids that generates ints or longs and use those for your population count.

have a look a `redis.clients.util.MurmurHash' in the jedis redis library. you can find it at https://github.com/xetorthio/jedis

*edit: sample

        UUID uuid = UUID.randomUUID();
        ByteBuffer buf = ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
        buf.flip();
        int useMe= MurmurHash.hash(buf, 123);
like image 24
Jonas Adler Avatar answered Feb 26 '23 11:02

Jonas Adler