gdritter repos tansu / 66c9cf9
Added key and value types Getty Ritter 8 years ago
6 changed file(s) with 17 addition(s) and 9 deletion(s). Collapse all Expand all
4343 createEphemeralDb ls = EDB (M.fromList [ (encode k, encode v) | (k, v) <- ls ])
4444
4545 -- | Run a 'Tansu' operation with an empty in-memory table.
46 withNewEphemeralDb :: (TansuDb -> IO a) -> IO a
46 withNewEphemeralDb :: (TansuDb k v -> IO a) -> IO a
4747 withNewEphemeralDb = withEphemeralDb $ EDB M.empty
4848
4949 -- | Run a 'Tansu' operation with an existing in-memory table.
50 withEphemeralDb :: EphemeralDb -> (TansuDb -> IO a) -> IO a
50 withEphemeralDb :: EphemeralDb -> (TansuDb k v -> IO a) -> IO a
5151 withEphemeralDb init comp = do
5252 lock <- newMVar ()
5353 table <- newIORef (fromEDB init)
4646 -- @.lock@ file in the specified directory, but note that
4747 -- file locking is not a guaranteed way of ensuring exclusion,
4848 -- and that the files themselves are not locked in any way.
49 withFilesystemDb :: FilePath -> (TansuDb -> IO a) -> IO a
49 withFilesystemDb :: FilePath -> (TansuDb k v -> IO a) -> IO a
5050 withFilesystemDb path comp = do
5151 createDirectoryIfMissing True path
5252 comp $ TansuDb { dbSet = filePathSet path
2323 -- should treat this as an abstract type, but the full definition
2424 -- is exposed by the "Database.Tansu.Internal" module so that
2525 -- other libraries can implement new storage backends.
26 data TansuDb = TansuDb
26 --
27 -- A given instantiation of the database will have
28 data TansuDb k v = TansuDb
2729 { dbSet :: ByteString -> ByteString -> IO (Either TansuError ())
2830 , dbGet :: ByteString -> IO (Either TansuError ByteString)
2931 , dbDel :: ByteString -> IO (Either TansuError ())
1 {-# LANGUAGE RankNTypes #-}
2
13 module Database.Tansu ( -- * The 'Tansu' monad
24 Tansu
35 , TansuDb
2426 )
2527 import Database.Tansu.Internal
2628
27 type TansuM a = ReaderT TansuDb (ExceptionT TansuError IO) a
29 type TansuM k v a = ReaderT (TansuDb k v) (ExceptionT TansuError IO) a
2830
2931 -- | The 'Tansu' type is a monad which represents some sequence
3032 -- of 'get' and 'set' operations. Ideally, backends should
3133 -- make some guarantee about the atomicity of a given
3234 -- 'Tansu' computation, but you should consult the documentation
3335 -- about a given backend to make sure that holds.
34 newtype Tansu k v a = Tansu { runTansu :: TansuM a }
36 newtype Tansu k v a = Tansu { runTansu :: TansuM k v a }
3537
3638 instance Functor (Tansu k v) where
3739 fmap f (Tansu t) = Tansu (fmap f t)
9496 -- | Given a storage backend and a 'Tansu' computation, execute the
9597 -- sequence of 'get' and 'set' commands and produce either the value
9698 -- or the error encountered while running the computation.
97 run :: TansuDb -> Tansu k v a -> IO (Either TansuError a)
99 run :: TansuDb k v -> Tansu k v a -> IO (Either TansuError a)
98100 run db (Tansu comp) =
99101 dbRunTransaction db (runExceptionT (runReaderT db comp))
6868 del :: (Serialize k) => k -> Tansu k v ()
6969
7070 -- run a Tansu computation
71 run :: TansuDb -> Tansu k v a -> IO (Either TansuError a)
71 run :: TansuDb k v -> Tansu k v a -> IO (Either TansuError a)
7272 ~~~
7373
7474 A value of type `TansuDb` should be supplied by a _backend_, which can
8080 backends can be simply implemented separately from the core `tansu`
8181 library, and backends can be easily swapped out as desired.
8282
83 ## Tansu Backends
84
85 [...]
86
8387 ## About the Name
8488
8589 A _tansu_ is a kind of
1313 putStrLn "Testing ephemeral db"
1414 withNewEphemeralDb sample
1515
16 sample :: TansuDb -> IO ()
16 sample :: TansuDb String String -> IO ()
1717 sample db = do
1818 putStrLn "Populating test database"
1919 run db $ do