gdritter repos tansu / 06b8518
Renamed Database->TansuDB Getty Ritter 8 years ago
5 changed file(s) with 15 addition(s) and 15 deletion(s). Collapse all Expand all
55 import Data.ByteString (ByteString)
66 import Data.IORef
77 import qualified Data.Map.Strict as M
8 import Database.Tansu.Internal (Database(..))
8 import Database.Tansu.Internal (TansuDb(..))
99
1010 type Table = M.Map ByteString ByteString
1111
1818 ephemeralGet :: IORef Table -> ByteString -> IO (Maybe ByteString)
1919 ephemeralGet table key = M.lookup key `fmap` readIORef table
2020
21 withEphemeralDb :: (Database -> IO a) -> IO a
21 withEphemeralDb :: (TansuDb -> IO a) -> IO a
2222 withEphemeralDb comp = do
2323 lock <- newMVar ()
2424 table <- newIORef M.empty
25 comp $ Database
25 comp $ TansuDb
2626 { dbRunTransaction = ephemeralRunTransaction lock
2727 , dbSet = ephemeralSet table
2828 , dbGet = ephemeralGet table
33 import Data.ByteString (ByteString)
44 import Data.ByteString.Base64
55 import qualified Data.ByteString.Char8 as BS
6 import Database.Tansu.Internal (Database(..))
6 import Database.Tansu.Internal (TansuDb(..))
77 import System.Directory (createDirectoryIfMissing, doesFileExist)
88 import System.FileLock (SharedExclusive(Exclusive), withFileLock)
99 import System.FilePath.Posix ((</>))
2525 filePathLock path comp = do
2626 withFileLock (path </> ".lock") Exclusive (const comp)
2727
28 withFilesystemDb :: FilePath -> (Database -> IO a) -> IO a
28 withFilesystemDb :: FilePath -> (TansuDb -> IO a) -> IO a
2929 withFilesystemDb path comp = do
3030 createDirectoryIfMissing True path
31 comp $ Database { dbSet = filePathSet path
32 , dbGet = filePathGet path
33 , dbRunTransaction = filePathLock path
34 }
31 comp $ TansuDb { dbSet = filePathSet path
32 , dbGet = filePathGet path
33 , dbRunTransaction = filePathLock path
34 }
11 {-# LANGUAGE Rank2Types #-}
22
3 module Database.Tansu.Internal ( Database(..)
3 module Database.Tansu.Internal ( TansuDb(..)
44 , TansuError(..)
55 ) where
66
1111 | DecodeError String
1212 deriving (Eq, Show)
1313
14 data Database = Database
14 data TansuDb = TansuDb
1515 { dbSet :: ByteString -> ByteString -> IO ()
1616 , dbGet :: ByteString -> IO (Maybe ByteString)
1717 , dbRunTransaction :: forall a. IO a -> IO a
11 module Database.Tansu ( Tansu
2 , Database
2 , TansuDb
33 , TansuError(..)
44 , get
55 , getMb
2020 )
2121 import Database.Tansu.Internal
2222
23 type TansuM a = ReaderT Database (ExceptionT TansuError IO) a
23 type TansuM a = ReaderT TansuDb (ExceptionT TansuError IO) a
2424
2525 newtype Tansu a = Tansu { runTansu :: TansuM a }
2626
5959 Right val' -> return (Just val')
6060 Left err -> Tansu (raise (DecodeError err))
6161
62 run :: Database -> Tansu a -> IO (Either TansuError a)
62 run :: TansuDb -> Tansu a -> IO (Either TansuError a)
6363 run db (Tansu comp) =
6464 dbRunTransaction db (runExceptionT (runReaderT db comp))
1313 putStrLn "Testing ephemeral db"
1414 withEphemeralDb sample
1515
16 sample :: Database -> IO ()
16 sample :: TansuDb -> IO ()
1717 sample db = do
1818 putStrLn "Populating test database"
1919 run db $ do