alexandrepiveteau / elm-ordt / Ordt.Weft

Wefts are like vector clocks – they offer causality information across different events in a distributed system. A new weft is empty. Each site is then associated with an index, indicating the index of the last operation received for that particular site.

For any two instances of Weft, an upper bound as well as a lower bound can be found. The lower bound is the last known state to both wefts. The upper bound is the minimal state that could know both wefts. This ability to find an upper or/and a lower bound is often referred to as a semi-lattice.

Usually, these clocks are associated with some operations. There is, in general, no guarantee that a specific instance of weft is associated to an operation though.

To manage the operation indices for any site, use the get and insert functions.

The site identifiers can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or lists of comparable types.

Wefts


type Weft comparable

Represents a weft in a distributed system. So Weft (Int) means that each site is identified by an Int, and Weft (String) that each site is identified by a String.

Build

empty : Weft comparable

Create a baseline that will always act as a proper lower bound for all the possible instances of an Ordt.Weft.

-- joinLower clock empty == empty
-- joinUpper clock empty == clock

singleton : comparable -> Basics.Int -> Weft comparable

Create a weft that will contain only a single index for a given site.

-- joinLower (singleton "Alice" 1) empty == empty
-- joinUpper (singleton "Alice" 1) empty == singleton "Alice" 1

insert : comparable -> Basics.Int -> Weft comparable -> Weft comparable

Set the last operation index for a certain yarn.

remove : comparable -> Weft comparable -> Weft comparable

Remove a yarn from a weft. If the key is not found, no changes are made.

Query

get : comparable -> Weft comparable -> Maybe Basics.Int

Get the last operation index for a certain site.

compare : Weft comparable -> Weft comparable -> Basics.Order

Compare any two Weft instances. This comparison takes in consideration that two wefts might not be comparable, in which case Order.EQ will be returned.

alice =
    insert "Alice 3" empty

bob =
    insert "Bob" 2 empty

-- compare empty alice == Order.LT
-- compare empty empty == Order.EQ
-- compare alice bob == Order.EQ
-- compare bob empty == Order.GT

Semi-lattice

joinLower : Weft comparable -> Weft comparable -> Weft comparable

Perform a semi-lattice join on the lower bound of the two wefts.

joinUpper : Weft comparable -> Weft comparable -> Weft comparable

Perform a semi-lattice join on the upper bound of the two wefts.

Dicts

fromDict : Dict comparable Basics.Int -> Weft comparable

Turn a Dict into a weft.

toDict : Weft comparable -> Dict comparable Basics.Int

Turn a weft into a Dict.

Encoders

encode : (comparable -> Json.Encode.Value) -> Weft comparable -> Json.Encode.Value

Turn a Weft into a JSON value.

decoder : Json.Decode.Decoder comparable -> Json.Decode.Decoder (Weft comparable)

Decode a JSON value into a Weft.