Debian Codegraph @

System/IO/Storage.hs

raw
1 {- |
2 Conceptually, this library provides a way to arbitrarily extend the
3 global state represented by the IO monad. Viewed another way, this
4 library provides a basic facility for setting and retrieving values
5 from global variables.
6
7 The interface takes the form of a very basic key-value store, with
8 multiple different stores made available through the 'withStore'
9 function. Stores are referenced by arbitrary strings, and keys
10 within those stores are treated likewise. The 'putValue', 'getValue',
11 and 'delValue' functions allow you to store, retrieve, and delete
12 data from the store.
13
14 Internally, data is stored within an IORef which is created using the
15 'unsafePerformIO hack', but this is hidden within the library so that
16 it can easily be modified if and when a more 'proper' solution is
17 implemented.
18 -}
19 module System.IO.Storage
20 ( withStore
21 , putValue
22 , getValue
23 , getDefaultValue
24 , delValue
25 ) where
26
27 import Data.IORef ( IORef, newIORef, modifyIORef, readIORef )
28 import Data.List as L ( lookup, deleteFirstsBy )
29 import Data.Map as M ( Map, empty, lookup, insert, delete )
30 import Data.Dynamic ( Dynamic, toDyn, fromDyn, fromDynamic )
31 import Data.Typeable ( Typeable )
32 import Data.Function ( on )
33 import Control.Exception ( bracket )
34 import System.IO.Unsafe ( unsafePerformIO )
35
36 type ValueStore = M.Map String Dynamic
37
38 -- | This is the magic bit that makes the data-stores global to the
39 -- entire program. Sure, it cheats a little, but who doesn't?
40 globalPeg :: IORef [(String, IORef ValueStore)]
41 {-# NOINLINE globalPeg #-}
42 globalPeg = unsafePerformIO (newIORef [])
43
44 -- | Create a named key-value store, and then execute the given
45 -- IO action within its extent. Calls to 'withStore' can be
46 -- nested, and calling it again with the name of a data-store
47 -- that has already been initialized will cause the original
48 -- to be shadowed for the duration of the call to 'withStore'.
49 withStore :: String -> IO a -> IO a
50 withStore storeName action = do
51 store <- newIORef M.empty
52 let emptyStore = (storeName, store)
53 let create = modifyIORef globalPeg (emptyStore:)
54 let delete = modifyIORef globalPeg deleteStore
55 bracket create (const delete) (const action)
56 where deleteStore xs = deleteFirstsBy ((==) `on` fst) xs dummyStore
57 dummyStore = [(storeName, undefined)]
58
59 getPrimitive :: String -> String -> IO (Maybe Dynamic)
60 getPrimitive storeName key = do
61 storeList <- readIORef globalPeg
62 case storeName `L.lookup` storeList of
63 Nothing -> return Nothing
64 Just st -> do map <- readIORef st
65 return $ key `M.lookup` map
66
67 -- | Get a value from the given data-store, if it exists. If it
68 -- doesn't exist, obviously, 'Nothing' will be returned.
69 getValue :: Typeable a => String -> String -> IO (Maybe a)
70 getValue storeName key = do
71 value <- getPrimitive storeName key
72 case value of
73 Nothing -> return $ Nothing
74 Just dy -> return $ fromDynamic dy
75
76 -- | Get a value from the given store, with a default if it
77 -- doesn't exist.
78 getDefaultValue :: Typeable a => String -> String -> a -> IO a
79 getDefaultValue storeName key val = do
80 value <- getPrimitive storeName key
81 case value of
82 Nothing -> return $ val
83 Just dy -> return $ fromDyn dy val
84
85 -- | Put a value into the given data-store.
86 putValue :: Typeable a => String -> String -> a -> IO ()
87 putValue storeName key value = do
88 storeList <- readIORef globalPeg
89 case storeName `L.lookup` storeList of
90 Nothing -> return ()
91 Just st -> modifyIORef st . M.insert key . toDyn $ value
92
93 -- | Delete a value from the given data-store.
94 delValue :: String -> String -> IO ()
95 delValue storeName key = do
96 storeList <- readIORef globalPeg
97 case storeName `L.lookup` storeList of
98 Nothing -> return ()
99 Just st -> modifyIORef st . M.delete $ key