{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
module Database.EventStore.Internal.Operation.ReadEvent
( ReadEvent(..)
, readEvent
) where
import Data.Int
import Data.ProtocolBuffers
import Database.EventStore.Internal.Command
import Database.EventStore.Internal.Control (publishWith)
import Database.EventStore.Internal.Communication (Transmit(..))
import Database.EventStore.Internal.Exec (Exec)
import Database.EventStore.Internal.Operation
import Database.EventStore.Internal.Operation.ReadEvent.Message
import Database.EventStore.Internal.Operation.Read.Common
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Settings
import Database.EventStore.Internal.Stream
import Database.EventStore.Internal.Types
data ReadEvent
= ReadEventNotFound
{ ReadEvent -> Text
readEventStream :: !Text
, ReadEvent -> Int64
readEventNumber :: !Int64
}
| ReadEvent
{ readEventStream :: !Text
, readEventNumber :: !Int64
, ReadEvent -> ResolvedEvent
readEventResolved :: !ResolvedEvent
} deriving Int -> ReadEvent -> ShowS
[ReadEvent] -> ShowS
ReadEvent -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ReadEvent] -> ShowS
$cshowList :: [ReadEvent] -> ShowS
show :: ReadEvent -> String
$cshow :: ReadEvent -> String
showsPrec :: Int -> ReadEvent -> ShowS
$cshowsPrec :: Int -> ReadEvent -> ShowS
Show
readEvent
:: Settings
-> Exec
-> Text
-> Int64
-> Bool
-> Maybe Credentials
-> IO (Async (ReadResult EventNumber ReadEvent))
readEvent :: Settings
-> Exec
-> Text
-> Int64
-> Bool
-> Maybe Credentials
-> IO (Async (ReadResult EventNumber ReadEvent))
readEvent Settings{Bool
Maybe Text
Maybe TLSSettings
Maybe Credentials
LogType
NominalDiffTime
LoggerFilter
MonitoringBackend
Retry
s_defaultUserCredentials :: Settings -> Maybe Credentials
s_defaultConnectionName :: Settings -> Maybe Text
s_monitoring :: Settings -> MonitoringBackend
s_operationRetry :: Settings -> Retry
s_operationTimeout :: Settings -> NominalDiffTime
s_loggerDetailed :: Settings -> Bool
s_loggerFilter :: Settings -> LoggerFilter
s_loggerType :: Settings -> LogType
s_ssl :: Settings -> Maybe TLSSettings
s_reconnect_delay :: Settings -> NominalDiffTime
s_retry :: Settings -> Retry
s_requireMaster :: Settings -> Bool
s_heartbeatTimeout :: Settings -> NominalDiffTime
s_heartbeatInterval :: Settings -> NominalDiffTime
s_defaultUserCredentials :: Maybe Credentials
s_defaultConnectionName :: Maybe Text
s_monitoring :: MonitoringBackend
s_operationRetry :: Retry
s_operationTimeout :: NominalDiffTime
s_loggerDetailed :: Bool
s_loggerFilter :: LoggerFilter
s_loggerType :: LogType
s_ssl :: Maybe TLSSettings
s_reconnect_delay :: NominalDiffTime
s_retry :: Retry
s_requireMaster :: Bool
s_heartbeatTimeout :: NominalDiffTime
s_heartbeatInterval :: NominalDiffTime
..} Exec
exec Text
stream Int64
evtn Bool
tos 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 -> Bool -> Request
newRequest Text
stream Int64
evtn Bool
tos Bool
s_requireMaster
Package
pkg <- forall msg (m :: * -> *).
(Encode msg, MonadIO m) =>
Command -> Maybe Credentials -> msg -> m Package
createPkg Command
readEventCmd Maybe Credentials
creds Request
req
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 Result))
r = forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Required 1 (Enumeration Result)
_result Response
resp
evt :: ResolvedEvent
evt = ResolvedIndexedEvent -> ResolvedEvent
newResolvedEvent forall a b. (a -> b) -> a -> b
$ forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Required 2 (Message ResolvedIndexedEvent)
_indexedEvent Response
resp
err :: FieldType (Optional 3 (Value Text))
err = forall a. HasField a => a -> FieldType a
getField forall a b. (a -> b) -> a -> b
$ Response -> Optional 3 (Value Text)
_error Response
resp
notFound :: ReadResult EventNumber ReadEvent
notFound = forall a t. a -> ReadResult t a
ReadSuccess forall a b. (a -> b) -> a -> b
$ Text -> Int64 -> ReadEvent
ReadEventNotFound Text
stream Int64
evtn
found :: ReadResult EventNumber ReadEvent
found = forall a t. a -> ReadResult t a
ReadSuccess forall a b. (a -> b) -> a -> b
$ Text -> Int64 -> ResolvedEvent -> ReadEvent
ReadEvent Text
stream Int64
evtn ResolvedEvent
evt in
case FieldType (Required 1 (Enumeration Result))
r of
FieldType (Required 1 (Enumeration Result))
Result
NOT_FOUND -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ReadResult EventNumber ReadEvent
notFound
FieldType (Required 1 (Enumeration Result))
Result
NO_STREAM -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. ReadResult EventNumber a
ReadNoStream
FieldType (Required 1 (Enumeration Result))
Result
STREAM_DELETED -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. StreamName -> ReadResult EventNumber a
ReadStreamDeleted forall a b. (a -> b) -> a -> b
$ Text -> StreamName
StreamName Text
stream
FieldType (Required 1 (Enumeration Result))
Result
ERROR -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall t a. Maybe Text -> ReadResult t a
ReadError FieldType (Optional 3 (Value Text))
err)
FieldType (Required 1 (Enumeration Result))
Result
ACCESS_DENIED -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall t a. StreamId t -> ReadResult t a
ReadAccessDenied forall a b. (a -> b) -> a -> b
$ Text -> StreamName
StreamName Text
stream
FieldType (Required 1 (Enumeration Result))
Result
SUCCESS -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ReadResult EventNumber ReadEvent
found