gdritter repos tansu / 7874530
Added del operation Getty Ritter 8 years ago
5 changed file(s) with 27 addition(s) and 3 deletion(s). Collapse all Expand all
2525 ephemeralGet :: IORef Table -> ByteString -> IO (Maybe ByteString)
2626 ephemeralGet table key = M.lookup key `fmap` readIORef table
2727
28 ephemeralDel :: IORef Table -> ByteString -> IO ()
29 ephemeralDel table key = modifyIORef table (M.delete key)
30
2831 -- | An 'EphemeralDb' is just an in-memory map, with no way of saving it.
2932 -- It is intended to be used for testing Tansu code.
3033 newtype EphemeralDb = EDB { fromEDB :: Table }
4649 { dbRunTransaction = ephemeralRunTransaction lock
4750 , dbSet = ephemeralSet table
4851 , dbGet = ephemeralGet table
52 , dbDel = ephemeralDel table
4953 }
44 import Data.ByteString.Base64
55 import qualified Data.ByteString.Char8 as BS
66 import Database.Tansu.Internal (TansuDb(..))
7 import System.Directory (createDirectoryIfMissing, doesFileExist)
7 import System.Directory ( createDirectoryIfMissing
8 , doesFileExist
9 , removeFile
10 )
811 import System.FileLock (SharedExclusive(Exclusive), withFileLock)
912 import System.FilePath.Posix ((</>))
1013
2023 if exists
2124 then Just `fmap` BS.readFile keyPath
2225 else return Nothing
26
27 filePathDel :: FilePath -> ByteString -> IO ()
28 filePathDel path key = do
29 let keyPath = path </> BS.unpack (encode key)
30 removeFile keyPath
2331
2432 filePathLock :: FilePath -> IO a -> IO a
2533 filePathLock path comp = do
3745 createDirectoryIfMissing True path
3846 comp $ TansuDb { dbSet = filePathSet path
3947 , dbGet = filePathGet path
48 , dbDel = filePathDel path
4049 , dbRunTransaction = filePathLock path
4150 }
2626 data TansuDb = TansuDb
2727 { dbSet :: ByteString -> ByteString -> IO ()
2828 , dbGet :: ByteString -> IO (Maybe ByteString)
29 , dbDel :: ByteString -> IO ()
2930 , dbRunTransaction :: forall a. IO a -> IO a
3031 }
77 , getMb
88 , set
99 , (=:)
10 , del
11 -- * Running a 'Tansu' operation
1012 , run
1113 ) where
1214
7678 Right val' -> return (Just val')
7779 Left err -> Tansu (raise (DecodeError err))
7880
81 del :: (Serialize k) => k -> Tansu ()
82 del key = do
83 db <- Tansu ask
84 Tansu $ inBase $ dbDel db (encode key)
85
7986 -- | Given a storage backend and a 'Tansu' computation, execute the
8087 -- sequence of 'get' and 'set' commands and produce either the value
8188 -- or the error encountered while running the computation.
4848 way to interact with a `TansuDb` is by running a `Tansu` command, which
4949 represents a (possibly empty) sequence of stores and loads applied
5050 successively to the key-value mapping. Values can be set using the `set`
51 command (or its operator synonym `=:`) and retrieved using the `get`
52 command.
51 command (or its operator synonym `=:`), retrieved using the `get`
52 command, and deleted using the `del` command.
5353
5454 ~~~.haskell
5555 -- set a key to a value
6363
6464 -- get a value, returning Nothing if it does not exist
6565 getMb :: (Serialize k, Serialize v) => k -> Tansu (Maybe v)
66
67 -- remove a key and its associated value
68 del :: (Serialize k) => k -> Tansu ()
6669
6770 -- run a Tansu computation
6871 run :: TansuDb -> Tansu a -> IO (Either TansuError a)