{-# LANGUAGE DeriveGeneric #-}

module Ouroboros.Consensus.Storage.LedgerDB.V1.Args
  ( FlushFrequency (..)
  , LedgerDbBackendArgs (..)
  , shouldFlush
  ) where

import Data.Word
import GHC.Generics
import Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore

-- | The number of blocks in the immutable part of the chain that we have to see
-- before we flush the ledger tables to disk. See 'onDiskShouldFlush'.
data FlushFrequency
  = -- | A default value, which is determined by a specific 'SnapshotPolicy'. See
    -- 'defaultSnapshotPolicy' as an example.
    DefaultFlushFrequency
  | -- | A requested value: the number of diffs in the immutable part of the
    -- chain required before flushing.
    RequestedFlushFrequency Word64
  | -- | To disable flushing, to be used in tests
    DisableFlushing
  deriving (Int -> FlushFrequency -> ShowS
[FlushFrequency] -> ShowS
FlushFrequency -> String
(Int -> FlushFrequency -> ShowS)
-> (FlushFrequency -> String)
-> ([FlushFrequency] -> ShowS)
-> Show FlushFrequency
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FlushFrequency -> ShowS
showsPrec :: Int -> FlushFrequency -> ShowS
$cshow :: FlushFrequency -> String
show :: FlushFrequency -> String
$cshowList :: [FlushFrequency] -> ShowS
showList :: [FlushFrequency] -> ShowS
Show, FlushFrequency -> FlushFrequency -> Bool
(FlushFrequency -> FlushFrequency -> Bool)
-> (FlushFrequency -> FlushFrequency -> Bool) -> Eq FlushFrequency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FlushFrequency -> FlushFrequency -> Bool
== :: FlushFrequency -> FlushFrequency -> Bool
$c/= :: FlushFrequency -> FlushFrequency -> Bool
/= :: FlushFrequency -> FlushFrequency -> Bool
Eq, (forall x. FlushFrequency -> Rep FlushFrequency x)
-> (forall x. Rep FlushFrequency x -> FlushFrequency)
-> Generic FlushFrequency
forall x. Rep FlushFrequency x -> FlushFrequency
forall x. FlushFrequency -> Rep FlushFrequency x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FlushFrequency -> Rep FlushFrequency x
from :: forall x. FlushFrequency -> Rep FlushFrequency x
$cto :: forall x. Rep FlushFrequency x -> FlushFrequency
to :: forall x. Rep FlushFrequency x -> FlushFrequency
Generic)

shouldFlush :: FlushFrequency -> (Word64 -> Bool)
shouldFlush :: FlushFrequency -> Word64 -> Bool
shouldFlush FlushFrequency
requestedFlushFrequency = case FlushFrequency
requestedFlushFrequency of
  RequestedFlushFrequency Word64
value -> (Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
value)
  FlushFrequency
DefaultFlushFrequency -> (Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
100)
  FlushFrequency
DisableFlushing -> Bool -> Word64 -> Bool
forall a b. a -> b -> a
const Bool
False

data LedgerDbBackendArgs m l = V1Args
  { forall (m :: * -> *) (l :: LedgerStateKind).
LedgerDbBackendArgs m l -> FlushFrequency
v1FlushFrequency :: FlushFrequency
  , forall (m :: * -> *) (l :: LedgerStateKind).
LedgerDbBackendArgs m l -> SomeBackendArgs m l
v1BackendArgs :: SomeBackendArgs m l
  }