Permalink
Browse files

Use single atomically when setting a value

  • Loading branch information...
1 parent 42a9d5e commit 319e66c61565b1520e0f4af84108114bcb419bf0 @honza committed Feb 16, 2013
Showing with 10 additions and 6 deletions.
  1. +10 −6 Main.hs
View
@@ -44,9 +44,11 @@ getCommand handle cmd db = do
setCommand :: Handle -> [String] -> (TVar DB) -> IO ()
setCommand handle cmd db = do
- d <- atomRead db
- atomically $ setValue db d (head cmd) (unwords (tail cmd))
+ appV (conv k v) db
hPutStrLn handle $ "OK"
+ where k = (head cmd)
+ v = (unwords (tail cmd))
+
commandProcessor :: Handle -> (TVar DB) -> IO ()
commandProcessor handle db = do
@@ -65,12 +67,14 @@ commandProcessor handle db = do
atomRead :: TVar a -> IO a
atomRead = atomically . readTVar
+conv :: String -> String -> DB -> DB
+conv k v db = insert k v db
+
+appV :: (DB -> DB) -> TVar DB -> IO ()
+appV fn x = atomically $ readTVar x >>= writeTVar x . fn
+
getValue :: DB -> String -> IO (String)
getValue db k = do
case lookup k db of
Just s -> return s
Nothing -> return "null"
-
-setValue :: (TVar DB) -> DB -> String -> String -> STM ()
-setValue db dbmap k v = do
- writeTVar db $ insert k v dbmap

0 comments on commit 319e66c

Please sign in to comment.