{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

-- | Versionstamps. For a great overview, see
-- https://forums.foundationdb.org/t/implementing-versionstamps-in-bindings/250
module FoundationDB.Versionstamp
  ( Versionstamp (..),
    VersionstampCompleteness (..),
    TransactionVersionstamp (..),
    encodeVersionstamp,
    encodeTransactionVersionstamp,
    decodeVersionstamp,
    decodeTransactionVersionstamp,
    transactionVersion,
    transactionBatchOrder,
    userVersion,
  )
where

import Data.Word (Word16, Word64)
import FoundationDB.Versionstamp.Internal

-- | Extracts the transaction version from the versionstamp. This is the
-- database version at which the versionstamp was committed.
transactionVersion :: TransactionVersionstamp -> Word64
transactionVersion :: TransactionVersionstamp -> Word64
transactionVersion (TransactionVersionstamp Word64
x Word16
_) = Word64
x

-- | Extracts the batch order from the versionstamp.
transactionBatchOrder :: TransactionVersionstamp -> Word16
transactionBatchOrder :: TransactionVersionstamp -> Word16
transactionBatchOrder (TransactionVersionstamp Word64
_ Word16
x) = Word16
x

userVersion :: Versionstamp a -> Word16
userVersion :: Versionstamp a -> Word16
userVersion (CompleteVersionstamp TransactionVersionstamp
_ Word16
x) = Word16
x
userVersion (IncompleteVersionstamp Word16
x) = Word16
x