gdritter repos tansu / 4addf02
Added file locking to filesystem backend Getty Ritter 8 years ago
2 changed file(s) with 10 addition(s) and 4 deletion(s). Collapse all Expand all
55 import qualified Data.ByteString.Char8 as BS
66 import Database.Tansu.Internal (Database(..))
77 import System.Directory (createDirectoryIfMissing, doesFileExist)
8 import System.FileLock (SharedExclusive(Exclusive), withFileLock)
89 import System.FilePath.Posix ((</>))
910
1011 filePathSet :: FilePath -> ByteString -> ByteString -> IO ()
2021 then Just `fmap` BS.readFile keyPath
2122 else return Nothing
2223
24 filePathLock :: FilePath -> IO a -> IO a
25 filePathLock path comp = do
26 withFileLock (path </> ".lock") Exclusive (const comp)
27
2328 withFilesystemDb :: FilePath -> (Database -> IO a) -> IO a
2429 withFilesystemDb path comp = do
2530 createDirectoryIfMissing True path
26 comp $ Database { dbSet = filePathSet path
27 , dbGet = filePathGet path
28 , dbRunTransaction = id
31 comp $ Database { dbSet = filePathSet path
32 , dbGet = filePathGet path
33 , dbRunTransaction = filePathLock path
2934 }
2424 directory,
2525 filepath,
2626 base64-bytestring,
27 containers
27 containers,
28 filelock
2829 default-language: Haskell2010
2930
3031 executable sample