Class RandomHelpers
java.lang.Object
com.apple.foundationdb.async.common.RandomHelpers
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,U> CompletableFuture <List<U>> forEach(SplittableRandom splittableRandom, Iterable<T> items, BiFunction<T, SplittableRandom, CompletableFuture<U>> body, int parallelism, Executor executor) Applies an asynchronous body to each item, giving every invocation its own independentSplittableRandomsplit fromsplittableRandomso the work stays reproducible under deterministic randomness.static SplittableRandomReturns aSplittableRandomseeded deterministically from a primary key.static SplittableRandomSeed aSplittableRandomdeterministically from a UUID identity (such as a task id), using all 128 bits into the seed.static UUIDrandomUuid(Tuple primaryKey, boolean deterministicRandomness) Returns aUUIDfor a record: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the primary key's encoded bytes when deterministic randomness is requested, or a randomUUID.randomUUID()otherwise.static UUIDrandomUuid(SplittableRandom random) Derives a type-4 (random)UUIDfrom the givenSplittableRandom, so that UUID generation can be made reproducible by seeding the source.static UUIDrandomUuid(SplittableRandom random, boolean deterministicRandomness) Returns a freshUUID: a reproducible one derived fromrandomwhen deterministic randomness is requested, or a true randomUUID.randomUUID()otherwise.static UUIDrandomUuid(UUID identity, boolean deterministicRandomness) Returns aUUID: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the given UUID identity's bytes when deterministic randomness is requested, or a randomUUID.randomUUID()otherwise.static doublesplitMixDouble(long x) Returns high-quality double hash code between0and1.
-
Method Details
-
random
Returns aSplittableRandomseeded deterministically from a primary key. Distinct keys return distinct streams. Seeds the random deterministically from a record's primary key using 64 bits of entropy avoiding clashes.- Parameters:
primaryKey- the record's primary key- Returns:
- a new
SplittableRandom
-
random
Seed aSplittableRandomdeterministically from a UUID identity (such as a task id), using all 128 bits into the seed.- Parameters:
identity- the UUID identity- Returns:
- a new
SplittableRandom
-
splitMixDouble
public static double splitMixDouble(long x) Returns high-quality double hash code between0and1. It usessplitMixLong(long)internally.- Parameters:
x- along- Returns:
- a high quality hash code of
xas adoublein the range[0.0d, 1.0d).
-
randomUuid
@Nonnull public static UUID randomUuid(@Nonnull SplittableRandom random, boolean deterministicRandomness) Returns a freshUUID: a reproducible one derived fromrandomwhen deterministic randomness is requested, or a true randomUUID.randomUUID()otherwise.- Parameters:
random- the random source to derive a reproducible UUID from whendeterministicRandomnessis setdeterministicRandomness- whether to derive the UUID deterministically fromrandom- Returns:
- a new UUID
-
randomUuid
Derives a type-4 (random)UUIDfrom the givenSplittableRandom, so that UUID generation can be made reproducible by seeding the source.- Parameters:
random- the random source to draw the UUID bits from- Returns:
- a type-4 UUID derived from
random
-
randomUuid
Returns aUUIDfor a record: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the primary key's encoded bytes when deterministic randomness is requested, or a randomUUID.randomUUID()otherwise. Deriving the deterministic id straight from the full primary key (rather than from a low-entropy seeded stream) guarantees distinct records get distinct ids.- Parameters:
primaryKey- the record's primary keydeterministicRandomness- whether to derive the UUID deterministically fromprimaryKey- Returns:
- a new UUID
-
randomUuid
Returns aUUID: a reproducible, collision-resistant 128-bit hash (UUID.nameUUIDFromBytes(byte[])) of the given UUID identity's bytes when deterministic randomness is requested, or a randomUUID.randomUUID()otherwise.- Parameters:
identity- the UUID identity to derive fromdeterministicRandomness- whether to derive the UUID deterministically fromidentity- Returns:
- a new UUID
-
forEach
@Nonnull public static <T,U> CompletableFuture<List<U>> forEach(@Nonnull SplittableRandom splittableRandom, @Nonnull Iterable<T> items, @Nonnull BiFunction<T, SplittableRandom, CompletableFuture<U>> body, int parallelism, @Nonnull Executor executor) Applies an asynchronous body to each item, giving every invocation its own independentSplittableRandomsplit fromsplittableRandomso the work stays reproducible under deterministic randomness.- Type Parameters:
T- the type of input itemU- the type of result produced per item- Parameters:
splittableRandom- the random source to split per itemitems- the items to processbody- the asynchronous operation to apply to each item together with its own random streamparallelism- the maximum number of items to process concurrentlyexecutor- the executor to run on- Returns:
- a future completing with the per-item results
-