joeybright / hybrid-logical-clock / Hlc


type Hlc

A hybrid logical clock.

The constructor for this type is hidden. You can create a new clock with the create function in this module.


type alias LocalHlc =
Hlc

An alias for an Hlc representing the local clock.

When used in this module, it's used to denote the difference between the local clock and a remote clock when those differences affect the outcome of a function.


type alias RemoteHlc =
Hlc

An alias for an Hlc representing a remote clock.

When used in this module, it's used to denote the difference between the local clock and a remote clock when those differences affect the outcome of a function.

create : String -> Time.Posix -> Hlc

Create a new hybrid logical-clock. To do so, you need a few pieces:

local : String -> Time.Posix -> LocalHlc -> Hlc

When an action takes place locally and you want to increment the clock, use this function.

This will return a new clock that is guaranteed to result in a logical order of events made on this machine, regardless of how accurate the clock on the machine is.

remote : String -> LocalHlc -> RemoteHlc -> Time.Posix -> Hlc

When an event is recieved from a remote source, this function can be used to increment the clock.

Note: The order of arguments matters here; the first should be your local clock and the second is the remote clock. It's important to order these correctly. If you do not, this function will return an unreliable clock.

compare : Hlc -> Hlc -> Basics.Order

Compare two hybrid logical clocks to determine their order.

Note that the order of arguments is important! The first clock is being compared to the second. The returned value will indicate if the first is greater than, less than, or equal to, the second.

toString : Hlc -> String

Turn the clock into a string.

This string can use comparison operators (==, >, <, etc.) to determine what clock is greater when comparing two clocks.

Running the following example:

    Hlc.create "unique-id" (millisToPosix 1685853812)
        |> Hlc.toString

Will result in the following string representation of the clock:

    "000001685853812:00000000:unique-id"

fromString : String -> Maybe Hlc

Attempts to take a string and turn it into a hybrid logical-clock.

values : Hlc -> { clock : Time.Posix, count : Basics.Int, id : String }

Return the values in a clock.

This is primarily used in testing. It is not possible to reconstruct a clock from the returned values.