Added del operation
Getty Ritter
8 years ago
25 | 25 |
ephemeralGet :: IORef Table -> ByteString -> IO (Maybe ByteString)
|
26 | 26 |
ephemeralGet table key = M.lookup key `fmap` readIORef table
|
27 | 27 |
|
| 28 |
ephemeralDel :: IORef Table -> ByteString -> IO ()
|
| 29 |
ephemeralDel table key = modifyIORef table (M.delete key)
|
| 30 |
|
28 | 31 |
-- | An 'EphemeralDb' is just an in-memory map, with no way of saving it.
|
29 | 32 |
-- It is intended to be used for testing Tansu code.
|
30 | 33 |
newtype EphemeralDb = EDB { fromEDB :: Table }
|
|
46 | 49 |
{ dbRunTransaction = ephemeralRunTransaction lock
|
47 | 50 |
, dbSet = ephemeralSet table
|
48 | 51 |
, dbGet = ephemeralGet table
|
| 52 |
, dbDel = ephemeralDel table
|
49 | 53 |
}
|
4 | 4 |
import Data.ByteString.Base64
|
5 | 5 |
import qualified Data.ByteString.Char8 as BS
|
6 | 6 |
import Database.Tansu.Internal (TansuDb(..))
|
7 | |
import System.Directory (createDirectoryIfMissing, doesFileExist)
|
| 7 |
import System.Directory ( createDirectoryIfMissing
|
| 8 |
, doesFileExist
|
| 9 |
, removeFile
|
| 10 |
)
|
8 | 11 |
import System.FileLock (SharedExclusive(Exclusive), withFileLock)
|
9 | 12 |
import System.FilePath.Posix ((</>))
|
10 | 13 |
|
|
20 | 23 |
if exists
|
21 | 24 |
then Just `fmap` BS.readFile keyPath
|
22 | 25 |
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
|
23 | 31 |
|
24 | 32 |
filePathLock :: FilePath -> IO a -> IO a
|
25 | 33 |
filePathLock path comp = do
|
|
37 | 45 |
createDirectoryIfMissing True path
|
38 | 46 |
comp $ TansuDb { dbSet = filePathSet path
|
39 | 47 |
, dbGet = filePathGet path
|
| 48 |
, dbDel = filePathDel path
|
40 | 49 |
, dbRunTransaction = filePathLock path
|
41 | 50 |
}
|
26 | 26 |
data TansuDb = TansuDb
|
27 | 27 |
{ dbSet :: ByteString -> ByteString -> IO ()
|
28 | 28 |
, dbGet :: ByteString -> IO (Maybe ByteString)
|
| 29 |
, dbDel :: ByteString -> IO ()
|
29 | 30 |
, dbRunTransaction :: forall a. IO a -> IO a
|
30 | 31 |
}
|
7 | 7 |
, getMb
|
8 | 8 |
, set
|
9 | 9 |
, (=:)
|
| 10 |
, del
|
| 11 |
-- * Running a 'Tansu' operation
|
10 | 12 |
, run
|
11 | 13 |
) where
|
12 | 14 |
|
|
76 | 78 |
Right val' -> return (Just val')
|
77 | 79 |
Left err -> Tansu (raise (DecodeError err))
|
78 | 80 |
|
| 81 |
del :: (Serialize k) => k -> Tansu ()
|
| 82 |
del key = do
|
| 83 |
db <- Tansu ask
|
| 84 |
Tansu $ inBase $ dbDel db (encode key)
|
| 85 |
|
79 | 86 |
-- | Given a storage backend and a 'Tansu' computation, execute the
|
80 | 87 |
-- sequence of 'get' and 'set' commands and produce either the value
|
81 | 88 |
-- or the error encountered while running the computation.
|
48 | 48 |
way to interact with a `TansuDb` is by running a `Tansu` command, which
|
49 | 49 |
represents a (possibly empty) sequence of stores and loads applied
|
50 | 50 |
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.
|
53 | 53 |
|
54 | 54 |
~~~.haskell
|
55 | 55 |
-- set a key to a value
|
|
63 | 63 |
|
64 | 64 |
-- get a value, returning Nothing if it does not exist
|
65 | 65 |
getMb :: (Serialize k, Serialize v) => k -> Tansu (Maybe v)
|
| 66 |
|
| 67 |
-- remove a key and its associated value
|
| 68 |
del :: (Serialize k) => k -> Tansu ()
|
66 | 69 |
|
67 | 70 |
-- run a Tansu computation
|
68 | 71 |
run :: TansuDb -> Tansu a -> IO (Either TansuError a)
|