Class IndexUtils
java.lang.Object
io.fluxcapacitor.javaclient.tracking.IndexUtils
Use 48 bits of the current time in milliseconds since epoch as the base of the index. The remaining 16 bits (65k) are
used to increment the index if messages are written in the same ms as the last batch.
The index is only able to store 2^47 - 1 ms of time since epoch, i.e. about 4,500 years.
Uses FluxCapacitor.currentClock()
to get the index corresponding to the current timestamp of Flux Capacitor's
internal clock.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long
Calculates an index based on the current timestamp from Flux Capacitor's internal clock.static long
indexFromMillis
(long millisSinceEpoch) Derives an index value from the given timestamp in milliseconds since the epoch by shifting it 16 bits to the left.static long
indexFromTimestamp
(Instant timestamp) Computes an index based on the provided timestamp.static long
maxIndexFromMillis
(long millisSinceEpoch) Computes the maximum possible index for the given timestamp in milliseconds since the epoch.static long
millisFromIndex
(long index) Extracts the millisecond component from the given index value.static long
Generates the next unique index based on the current time or the last provided index.static int
offsetFromIndex
(long index) Calculates the offset from the given index by extracting the least significant 16 bits.static Instant
timestampFromIndex
(long index) Converts the given index to a timestamp.
-
Constructor Details
-
IndexUtils
public IndexUtils()
-
-
Method Details
-
millisFromIndex
public static long millisFromIndex(long index) Extracts the millisecond component from the given index value.- Parameters:
index
- The input index value, where the higher 48 bits represent the milliseconds since the epoch, and the lower 16 bits are used for incrementing within the same millisecond.- Returns:
- The number of milliseconds since the epoch, derived from the given index.
-
timestampFromIndex
Converts the given index to a timestamp.The method interprets the index as a value where the upper 48 bits represent milliseconds since the epoch.
- Parameters:
index
- the index to convert, where the upper 48 bits represent milliseconds since epoch- Returns:
- an
Instant
representing the timestamp corresponding to the provided index
-
indexFromTimestamp
Computes an index based on the provided timestamp. The index is generated by converting the timestamp to milliseconds since the Unix epoch and shifting it to allocate 48 bits for storing the timestamp with additional 16 bits reserved for increments within the same millisecond.- Parameters:
timestamp
- the Instant object representing a specific timestamp- Returns:
- the computed index as a long value, based on the provided timestamp
-
indexFromMillis
public static long indexFromMillis(long millisSinceEpoch) Derives an index value from the given timestamp in milliseconds since the epoch by shifting it 16 bits to the left. This method is used for generating a unique index value with additional room for handling increments within the same millisecond.- Parameters:
millisSinceEpoch
- the timestamp in milliseconds since the epoch- Returns:
- the computed index value derived from the given timestamp
-
maxIndexFromMillis
public static long maxIndexFromMillis(long millisSinceEpoch) Computes the maximum possible index for the given timestamp in milliseconds since the epoch.The index is calculated by shifting the given milliseconds since epoch by 16 bits to the left and subsequently adding 65,535 to determine the upper limit for that millisecond.
- Parameters:
millisSinceEpoch
- the time in milliseconds since the epoch (January 1, 1970, 00:00:00 GMT)- Returns:
- the maximum index corresponding to the provided time in the Flux Capacitor index space
-
offsetFromIndex
public static int offsetFromIndex(long index) Calculates the offset from the given index by extracting the least significant 16 bits.- Parameters:
index
- the index from which the offset will be derived, typically a value combining time-based and incremental components- Returns:
- the offset, which is an integer in the range 0 to 65,535
-
indexForCurrentTime
public static long indexForCurrentTime()Calculates an index based on the current timestamp from Flux Capacitor's internal clock. The method uses the current time in milliseconds since the epoch, shifted left by 16 bits.- Returns:
- a 64-bit long value representing the calculated index. The upper 48 bits represent the current time in milliseconds since the epoch, and the lower 16 bits are reserved for potential incrementation within the same millisecond.
-
nextIndex
Generates the next unique index based on the current time or the last provided index.- Parameters:
lastIndex
- The last index that was generated. Can be null. If null, the method generates an index based on the current time.- Returns:
- The next index, which is either based on the current time or incremented from the provided last index.
-