@Test public void bucket() { String key = "research.megaphone.1"; UUID uuid = UuidUtil.parseOrThrow("15b9729c-51ea-4ddb-b516-652befe78062"); long partPer = 1_000_000; long countEnabled = BucketingUtil.bucket(key, uuid, partPer); assertEquals(243315, countEnabled); }
public static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus) { MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } digest.update(key.getBytes()); digest.update(".".getBytes()); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); digest.update(byteBuffer.array()); return new BigInteger(Arrays.copyOfRange(digest.digest(), 0, 8)).mod(BigInteger.valueOf(modulus)).longValue(); }
BucketingUtil { public static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus) { MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } digest.update(key.getBytes()); digest.update(".".getBytes()); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); digest.update(byteBuffer.array()); return new BigInteger(Arrays.copyOfRange(digest.digest(), 0, 8)).mod(BigInteger.valueOf(modulus)).longValue(); } }
BucketingUtil { public static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus) { MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } digest.update(key.getBytes()); digest.update(".".getBytes()); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); digest.update(byteBuffer.array()); return new BigInteger(Arrays.copyOfRange(digest.digest(), 0, 8)).mod(BigInteger.valueOf(modulus)).longValue(); } private  BucketingUtil(); }
BucketingUtil { public static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus) { MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } digest.update(key.getBytes()); digest.update(".".getBytes()); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); digest.update(byteBuffer.array()); return new BigInteger(Arrays.copyOfRange(digest.digest(), 0, 8)).mod(BigInteger.valueOf(modulus)).longValue(); } private  BucketingUtil(); static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus); }
BucketingUtil { public static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus) { MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } digest.update(key.getBytes()); digest.update(".".getBytes()); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.putLong(uuid.getMostSignificantBits()); byteBuffer.putLong(uuid.getLeastSignificantBits()); digest.update(byteBuffer.array()); return new BigInteger(Arrays.copyOfRange(digest.digest(), 0, 8)).mod(BigInteger.valueOf(modulus)).longValue(); } private  BucketingUtil(); static long bucket(@NonNull String key, @NonNull UUID uuid, long modulus);  }