{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecordWildCards #-}
module Database.EventStore.Internal.Operation.DeleteStream
( DeleteResult(..)
, deleteStream
) where
import Data.Maybe
import Data.ProtocolBuffers
import Database.EventStore.Internal.Command
import Database.EventStore.Internal.Communication (Transmit(..))
import Database.EventStore.Internal.Control (publishWith)
import Database.EventStore.Internal.Exec
import Database.EventStore.Internal.Operation
import Database.EventStore.Internal.Operation.DeleteStream.Message
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Settings
import Database.EventStore.Internal.Stream
import Database.EventStore.Internal.Types
newtype DeleteResult = DeleteResult Position deriving (DeleteResult -> DeleteResult -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeleteResult -> DeleteResult -> Bool
$c/= :: DeleteResult -> DeleteResult -> Bool
== :: DeleteResult -> DeleteResult -> Bool
$c== :: DeleteResult -> DeleteResult -> Bool
Eq, Int -> DeleteResult -> ShowS
[DeleteResult] -> ShowS
DeleteResult -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DeleteResult] -> ShowS
$cshowList :: [DeleteResult] -> ShowS
show :: DeleteResult -> String
$cshow :: DeleteResult -> String
showsPrec :: Int -> DeleteResult -> ShowS
$cshowsPrec :: Int -> DeleteResult -> ShowS
Show)
deleteStream
:: Settings
-> Exec
-> Text
-> ExpectedVersion
-> Maybe Bool
-> Maybe Credentials
-> IO (Async DeleteResult)
deleteStream :: Settings
-> Exec
-> Text
-> ExpectedVersion
-> Maybe Bool
-> Maybe Credentials
-> IO (Async DeleteResult)
deleteStream Settings
setts Exec
exec Text
stream ExpectedVersion
version Maybe Bool
hard Maybe Credentials
creds
= do Mailbox
m <- forall (m :: * -> *). MonadBase IO m => m Mailbox
mailboxNew
forall (m :: * -> *) a.
MonadBaseControl IO m =>
m a -> m (Async (StM m a))
async forall a b. (a -> b) -> a -> b
$
do let req :: Request
req = Text -> Int64 -> Bool -> Maybe Bool -> Request
newRequest Text
stream (ExpectedVersion -> Int64
expVersionInt64 ExpectedVersion
version) (Settings -> Bool
s_requireMaster Settings
setts) Maybe Bool
hard
Package
pkg <- forall msg (m :: * -> *).
(Encode msg, MonadIO m) =>
Command -> Maybe Credentials -> msg -> m Package
createPkg Command
deleteStreamCmd Maybe Credentials
creds Request
req
forall (m :: * -> *) a. Monad m => m (Loop a) -> m a
keepLooping forall a b. (a -> b) -> a -> b
$ do
forall p a (m :: * -> *).
(Pub p, Typeable a, MonadIO m) =>
p -> a -> m ()
publishWith Exec
exec (Mailbox -> Lifetime -> Package -> Transmit
Transmit Mailbox
m Lifetime
OneTime Package
pkg)
Either OperationError Response
outcome <- forall (m :: * -> *) resp.
(MonadBase IO m, Decode resp) =>
Mailbox -> m (Either OperationError resp)
mailboxReadDecoded Mailbox
m
case Either OperationError Response
outcome of
Left OperationError
e
-> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw OperationError
e
Right Response
resp
-> let r :: FieldType (Required 1 (Enumeration OpResult))
r = forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Required 1 (Enumeration OpResult)
_result Response
resp
com_pos :: FieldType (Optional 4 (Value Int64))
com_pos = forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Optional 4 (Value Int64)
_commitPosition Response
resp
prep_pos :: FieldType (Optional 3 (Value Int64))
prep_pos = forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Optional 3 (Value Int64)
_preparePosition Response
resp
com_pos_int :: Int64
com_pos_int = forall a. a -> Maybe a -> a
fromMaybe (-Int64
1) FieldType (Optional 4 (Value Int64))
com_pos
prep_pos_int :: Int64
prep_pos_int = forall a. a -> Maybe a -> a
fromMaybe (-Int64
1) FieldType (Optional 3 (Value Int64))
prep_pos
pos :: Position
pos = Int64 -> Int64 -> Position
Position Int64
com_pos_int Int64
prep_pos_int
res :: DeleteResult
res = Position -> DeleteResult
DeleteResult Position
pos in
case FieldType (Required 1 (Enumeration OpResult))
r of
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_SUCCESS -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Loop a
Break DeleteResult
res
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_PREPARE_TIMEOUT -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Loop a
Loop
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_FORWARD_TIMEOUT -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Loop a
Loop
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_COMMIT_TIMEOUT -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Loop a
Loop
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_WRONG_EXPECTED_VERSION -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw forall a b. (a -> b) -> a -> b
$ Text -> ExpectedVersion -> OperationError
WrongExpectedVersion Text
stream ExpectedVersion
version
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_STREAM_DELETED -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw forall a b. (a -> b) -> a -> b
$ StreamName -> OperationError
StreamDeleted forall a b. (a -> b) -> a -> b
$ Text -> StreamName
StreamName Text
stream
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_INVALID_TRANSACTION -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw OperationError
InvalidTransaction
FieldType (Required 1 (Enumeration OpResult))
OpResult
OP_ACCESS_DENIED -> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw forall a b. (a -> b) -> a -> b
$ forall t. StreamId t -> OperationError
AccessDenied (Text -> StreamName
StreamName Text
stream)