| 31 | 31 |
-- make some guarantee about the atomicity of a given
|
| 32 | 32 |
-- 'Tansu' computation, but you should consult the documentation
|
| 33 | 33 |
-- about a given backend to make sure that holds.
|
| 34 | |
newtype Tansu a = Tansu { runTansu :: TansuM a }
|
| 34 |
newtype Tansu k v a = Tansu { runTansu :: TansuM a }
|
| 35 | 35 |
|
| 36 | |
instance Functor Tansu where
|
| 36 |
instance Functor (Tansu k v) where
|
| 37 | 37 |
fmap f (Tansu t) = Tansu (fmap f t)
|
| 38 | 38 |
|
| 39 | |
instance Applicative Tansu where
|
| 39 |
instance Applicative (Tansu k v) where
|
| 40 | 40 |
pure = Tansu . pure
|
| 41 | 41 |
Tansu f <*> Tansu x = Tansu (f <*> x)
|
| 42 | 42 |
|
| 43 | |
instance Monad Tansu where
|
| 43 |
instance Monad (Tansu k v) where
|
| 44 | 44 |
Tansu x >>= f = Tansu (x >>= runTansu . f)
|
| 45 | 45 |
|
| 46 | 46 |
-- | Sets the value for a key to a value.
|
| 47 | |
set :: (Serialize k, Serialize v) => k -> v -> Tansu ()
|
| 47 |
set :: (Serialize k, Serialize v) => k -> v -> Tansu k v ()
|
| 48 | 48 |
set key val = do
|
| 49 | 49 |
db <- Tansu ask
|
| 50 | 50 |
Tansu $ inBase $ dbSet db (encode key) (encode val)
|
| 51 | 51 |
|
| 52 | 52 |
-- | Sets the value for a key to a value. A convenience operator
|
| 53 | 53 |
-- that is identical to 'set'.
|
| 54 | |
(=:) :: (Serialize k, Serialize v) => k -> v -> Tansu ()
|
| 54 |
(=:) :: (Serialize k, Serialize v) => k -> v -> Tansu k v ()
|
| 55 | 55 |
(=:) = set
|
| 56 | 56 |
|
| 57 | 57 |
-- | Gets a value for a given key. The resulting 'Tansu' computation
|
| 58 | 58 |
-- will fail if the key is not present in the storage backend.
|
| 59 | |
get :: (Serialize k, Serialize v) => k -> Tansu v
|
| 59 |
get :: (Serialize k, Serialize v) => k -> Tansu k v v
|
| 60 | 60 |
get key = do
|
| 61 | 61 |
result <- getMb key
|
| 62 | 62 |
case result of
|
|
| 68 | 68 |
-- as problems decoding the serialized value or difficulties
|
| 69 | 69 |
-- communicating with the storage backend, will still cause the
|
| 70 | 70 |
-- 'Tansu' computation to fail.
|
| 71 | |
getMb :: (Serialize k, Serialize v) => k -> Tansu (Maybe v)
|
| 71 |
getMb :: (Serialize k, Serialize v) => k -> Tansu k v (Maybe v)
|
| 72 | 72 |
getMb key = do
|
| 73 | 73 |
db <- Tansu ask
|
| 74 | 74 |
result <- Tansu $ inBase $ dbGet db (encode key)
|
|
| 78 | 78 |
Right val' -> return (Just val')
|
| 79 | 79 |
Left err -> Tansu (raise (DecodeError err))
|
| 80 | 80 |
|
| 81 | |
del :: (Serialize k) => k -> Tansu ()
|
| 81 |
del :: (Serialize k) => k -> Tansu k v ()
|
| 82 | 82 |
del key = do
|
| 83 | 83 |
db <- Tansu ask
|
| 84 | 84 |
Tansu $ inBase $ dbDel db (encode key)
|
|
| 86 | 86 |
-- | Given a storage backend and a 'Tansu' computation, execute the
|
| 87 | 87 |
-- sequence of 'get' and 'set' commands and produce either the value
|
| 88 | 88 |
-- or the error encountered while running the computation.
|
| 89 | |
run :: TansuDb -> Tansu a -> IO (Either TansuError a)
|
| 89 |
run :: TansuDb -> Tansu k v a -> IO (Either TansuError a)
|
| 90 | 90 |
run db (Tansu comp) =
|
| 91 | 91 |
dbRunTransaction db (runExceptionT (runReaderT db comp))
|