{-# LANGUAGE OverloadedStrings #-}

module Database.Redis.ManualCommands.Function
    ( functionDelete
    , functionDump
    , functionFlush
    , functionFlushOpts
    , functionKill
    , functionLoad
    , functionLoadReplace
    , FunctionRestorePolicy(..)
    , FunctionRestoreOpts(..)
    , defaultFunctionRestoreOpts
    , functionRestore
    , functionRestoreOpts
    , functionStats
    ) where

import Data.ByteString (ByteString)

import Database.Redis.Core
import Database.Redis.Protocol
import Database.Redis.Types
import Database.Redis.ManualCommands (FlushOpts, FunctionRestorePolicy(..))
import qualified Database.Redis.ManualCommands as Manual

data FunctionRestoreOpts
    = FunctionRestoreDefault
    | FunctionRestoreWithPolicy FunctionRestorePolicy
    deriving (Int -> FunctionRestoreOpts -> ShowS
[FunctionRestoreOpts] -> ShowS
FunctionRestoreOpts -> String
(Int -> FunctionRestoreOpts -> ShowS)
-> (FunctionRestoreOpts -> String)
-> ([FunctionRestoreOpts] -> ShowS)
-> Show FunctionRestoreOpts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FunctionRestoreOpts -> ShowS
showsPrec :: Int -> FunctionRestoreOpts -> ShowS
$cshow :: FunctionRestoreOpts -> String
show :: FunctionRestoreOpts -> String
$cshowList :: [FunctionRestoreOpts] -> ShowS
showList :: [FunctionRestoreOpts] -> ShowS
Show, FunctionRestoreOpts -> FunctionRestoreOpts -> Bool
(FunctionRestoreOpts -> FunctionRestoreOpts -> Bool)
-> (FunctionRestoreOpts -> FunctionRestoreOpts -> Bool)
-> Eq FunctionRestoreOpts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FunctionRestoreOpts -> FunctionRestoreOpts -> Bool
== :: FunctionRestoreOpts -> FunctionRestoreOpts -> Bool
$c/= :: FunctionRestoreOpts -> FunctionRestoreOpts -> Bool
/= :: FunctionRestoreOpts -> FunctionRestoreOpts -> Bool
Eq)

defaultFunctionRestoreOpts :: FunctionRestoreOpts
defaultFunctionRestoreOpts :: FunctionRestoreOpts
defaultFunctionRestoreOpts = FunctionRestoreOpts
FunctionRestoreDefault

-- |Deletes a library and its functions (<https://redis.io/commands/function-delete>).
--
-- Deletes the library named by the argument together with all functions it contains.
--
-- /O(1)/
--
-- Since Redis 7.0.0
functionDelete
    :: (RedisCtx m f)
    => ByteString -- ^ Library name.
    -> m (f Status)
functionDelete :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f Status)
functionDelete = ByteString -> m (f Status)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f Status)
Manual.functionDelete

-- |Dumps all libraries into a serialized binary payload (<https://redis.io/commands/function-dump>).
--
-- Serializes all loaded libraries into a binary payload that can later be used with 'functionRestore'.
--
-- /O(N)/ where /N/ is the number of functions.
--
-- Since Redis 7.0.0
functionDump
    :: (RedisCtx m f)
    => m (f ByteString)
functionDump :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
m (f ByteString)
functionDump = m (f ByteString)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
m (f ByteString)
Manual.functionDump

-- |Deletes all libraries and functions (<https://redis.io/commands/function-flush>).
--
-- Removes every library currently loaded into Redis.
--
-- /O(N)/ where /N/ is the number of functions deleted.
--
-- Since Redis 7.0.0
functionFlush
    :: (RedisCtx m f)
    => m (f Status)
functionFlush :: forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Status)
functionFlush = m (f Status)
forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Status)
Manual.functionFlush

-- |Deletes all libraries and functions (<https://redis.io/commands/function-flush>).
--
-- Removes every library currently loaded into Redis using the requested flushing mode.
--
-- /O(N)/ where /N/ is the number of functions deleted.
--
-- Since Redis 7.0.0
functionFlushOpts
    :: (RedisCtx m f)
    => FlushOpts -- ^ Flush mode.
    -> m (f Status)
functionFlushOpts :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
FlushOpts -> m (f Status)
functionFlushOpts = FlushOpts -> m (f Status)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
FlushOpts -> m (f Status)
Manual.functionFlushOpts

-- |Terminates a function during execution (<https://redis.io/commands/function-kill>).
--
-- Terminates the currently running function, if it is marked as killable by Redis.
--
-- /O(1)/
--
-- Since Redis 7.0.0
functionKill
    :: (RedisCtx m f)
    => m (f Status)
functionKill :: forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Status)
functionKill = m (f Status)
forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Status)
Manual.functionKill

-- |Creates a library (<https://redis.io/commands/function-load>).
--
-- Loads a new function library and returns its library name.
--
-- /O(N)/ where /N/ is the number of bytes in the function's source code.
--
-- Since Redis 7.0.0
functionLoad
    :: (RedisCtx m f)
    => ByteString -- ^ Library source code.
    -> m (f ByteString)
functionLoad :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f ByteString)
functionLoad = ByteString -> m (f ByteString)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f ByteString)
Manual.functionLoad

-- |Creates a library, replacing an existing one with the same name (<https://redis.io/commands/function-load>).
--
-- Loads a function library and replaces an existing library with the same name.
--
-- /O(N)/ where /N/ is the number of bytes in the function's source code.
--
-- Since Redis 7.0.0
functionLoadReplace
    :: (RedisCtx m f)
    => ByteString -- ^ Library source code.
    -> m (f ByteString)
functionLoadReplace :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f ByteString)
functionLoadReplace = ByteString -> m (f ByteString)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f ByteString)
Manual.functionLoadReplace

-- |Restores all libraries from a payload (<https://redis.io/commands/function-restore>).
--
-- Restores all libraries from a payload previously returned by 'functionDump'.
--
-- /O(N)/ where /N/ is the number of functions restored.
--
-- Since Redis 7.0.0
functionRestore
    :: (RedisCtx m f)
    => ByteString -- ^ Serialized libraries payload.
    -> m (f Status)
functionRestore :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> m (f Status)
functionRestore ByteString
payload = ByteString -> FunctionRestoreOpts -> m (f Status)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> FunctionRestoreOpts -> m (f Status)
functionRestoreOpts ByteString
payload FunctionRestoreOpts
defaultFunctionRestoreOpts

-- |Restores all libraries from a payload (<https://redis.io/commands/function-restore>).
--
-- Restores all libraries from a payload previously returned by 'functionDump', optionally selecting the restore policy.
--
-- /O(N)/ where /N/ is the number of functions restored.
--
-- Since Redis 7.0.0
functionRestoreOpts
    :: (RedisCtx m f)
    => ByteString -- ^ Serialized libraries payload.
    -> FunctionRestoreOpts -- ^ Restore options.
    -> m (f Status)
functionRestoreOpts :: forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> FunctionRestoreOpts -> m (f Status)
functionRestoreOpts ByteString
payload FunctionRestoreOpts
opts =
    ByteString -> Maybe FunctionRestorePolicy -> m (f Status)
forall (m :: * -> *) (f :: * -> *).
RedisCtx m f =>
ByteString -> Maybe FunctionRestorePolicy -> m (f Status)
Manual.functionRestore ByteString
payload Maybe FunctionRestorePolicy
restorePolicy
  where
    restorePolicy :: Maybe FunctionRestorePolicy
restorePolicy = case FunctionRestoreOpts
opts of
        FunctionRestoreOpts
FunctionRestoreDefault -> Maybe FunctionRestorePolicy
forall a. Maybe a
Nothing
        FunctionRestoreWithPolicy FunctionRestorePolicy
policy -> FunctionRestorePolicy -> Maybe FunctionRestorePolicy
forall a. a -> Maybe a
Just FunctionRestorePolicy
policy

-- |Returns information about a function during execution (<https://redis.io/commands/function-stats>).
--
-- Returns execution statistics and runtime information for the function engine.
--
-- /O(1)/
--
-- Since Redis 7.0.0
functionStats
    :: (RedisCtx m f)
    => m (f Reply)
functionStats :: forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Reply)
functionStats = m (f Reply)
forall (m :: * -> *) (f :: * -> *). RedisCtx m f => m (f Reply)
Manual.functionStats