{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Database.Redis.ManualCommands.Wait
  ( WaitAofResult(..)
  , wait
  , waitaof
  ) where

import Database.Redis.Core
import Database.Redis.Types

data WaitAofResult = WaitAofResult
    { WaitAofResult -> Integer
waitAofLocal :: Integer
      -- ^ Number of local Redis instances (0 or 1) that fsynced all preceding writes to AOF.
    , WaitAofResult -> Integer
waitAofReplicas :: Integer
      -- ^ Number of replicas that fsynced all preceding writes to AOF.
    } deriving (Int -> WaitAofResult -> ShowS
[WaitAofResult] -> ShowS
WaitAofResult -> String
(Int -> WaitAofResult -> ShowS)
-> (WaitAofResult -> String)
-> ([WaitAofResult] -> ShowS)
-> Show WaitAofResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WaitAofResult -> ShowS
showsPrec :: Int -> WaitAofResult -> ShowS
$cshow :: WaitAofResult -> String
show :: WaitAofResult -> String
$cshowList :: [WaitAofResult] -> ShowS
showList :: [WaitAofResult] -> ShowS
Show, WaitAofResult -> WaitAofResult -> Bool
(WaitAofResult -> WaitAofResult -> Bool)
-> (WaitAofResult -> WaitAofResult -> Bool) -> Eq WaitAofResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WaitAofResult -> WaitAofResult -> Bool
== :: WaitAofResult -> WaitAofResult -> Bool
$c/= :: WaitAofResult -> WaitAofResult -> Bool
/= :: WaitAofResult -> WaitAofResult -> Bool
Eq)

instance RedisResult WaitAofResult where
    decode :: Reply -> Either Reply WaitAofResult
decode Reply
response = do
        (waitAofLocal, waitAofReplicas) <- Reply -> Either Reply (Integer, Integer)
forall a. RedisResult a => Reply -> Either Reply a
decode Reply
response
        pure WaitAofResult{..}

-- |
-- /O(1)/ Wait for preceding writes to be acknowledged by a given number of replicas (<https://redis.io/commands/wait>).
--
-- Blocks until the asynchronous replication of all preceding write commands sent by the connection is completed.
--
-- Since Redis 3.0.0
wait
    :: (RedisCtx m f)
    => Integer -- ^ Number of replicas to wait for.
    -> Integer -- ^ Maximum time to wait in milliseconds. @0@ means wait forever.
    -> m (f Integer)
wait :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
Integer -> Integer -> m (f Integer)
wait Integer
numReplicas Integer
timeout =
    [ByteString] -> m (f Integer)
forall (m :: * -> *) (f :: * -> *) a.
(RedisCtx m f, RedisResult a) =>
[ByteString] -> m (f a)
sendRequest [ByteString
"WAIT", Integer -> ByteString
forall a. RedisArg a => a -> ByteString
encode Integer
numReplicas, Integer -> ByteString
forall a. RedisArg a => a -> ByteString
encode Integer
timeout]

-- |
-- /O(1)/ Wait for preceding writes to be fsynced to the append-only file locally and\/or on replicas (<https://redis.io/commands/waitaof>).
--
-- Blocks until all of the preceding write commands sent by the connection are written to the append-only file of the master and\/or replicas.
--
-- Since Redis 7.2.0
waitaof
    :: (RedisCtx m f)
    => Integer -- ^ Number of local Redis instances to wait for AOF fsync on (@0@ or @1@).
    -> Integer -- ^ Number of replicas to wait for AOF fsync on.
    -> Integer -- ^ Maximum time to wait in milliseconds. @0@ means wait forever.
    -> m (f WaitAofResult)
waitaof :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
Integer -> Integer -> Integer -> m (f WaitAofResult)
waitaof Integer
numLocal Integer
numReplicas Integer
timeout =
    [ByteString] -> m (f WaitAofResult)
forall (m :: * -> *) (f :: * -> *) a.
(RedisCtx m f, RedisResult a) =>
[ByteString] -> m (f a)
sendRequest [ByteString
"WAITAOF", Integer -> ByteString
forall a. RedisArg a => a -> ByteString
encode Integer
numLocal, Integer -> ByteString
forall a. RedisArg a => a -> ByteString
encode Integer
numReplicas, Integer -> ByteString
forall a. RedisArg a => a -> ByteString
encode Integer
timeout]