TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: A threaded, TCP, key value store in Haskell

63 pointsby mrhonzaover 12 years ago

6 comments

soldover 12 years ago
You can change<p><pre><code> appV fn x = atomically $ readTVar x &#62;&#62;= writeTVar x . fn </code></pre> to<p><pre><code> appV fn x = atomically $ modifyTVar x fn </code></pre> The function getValue need not be in IO monad. You can simplify it using fromMaybe in Data.Maybe.<p>Sometimes you check "head l" and "tail l"; it's more idiomatic to use pattern matching. For example, you can change:<p><pre><code> setCommand handle cmd db = do appV (conv k v) db hPutStrLn handle $ "OK" where k = (head cmd) v = (unwords (tail cmd)) .... case (head cmd) of ("get") -&#62; getCommand handle (unwords (tail cmd)) db ("set") -&#62; setCommand handle (tail cmd) db </code></pre> to something like this:<p><pre><code> setCommand handle key val db = do appV (conv key val) db hPutStrLn handle "OK" ... case cmd of "get":key -&#62; getCommand handle (unwords key) db "set":key:val -&#62; setCommand handle key (unwords val) db </code></pre> Also consider using hlint, it often gives good advice.
评论 #5232927 未加载
joeyhover 12 years ago
Nice example of using Software Transactional Memory. Which is starting to be available in things like gcc, but is much much nicer in Haskell for reasons involving purity and monads.<p>Particularly this function, which changes a variable in the database, transactionally.<p><pre><code> appV :: (DB -&#62; DB) -&#62; TVar DB -&#62; IO () appV fn x = atomically $ readTVar x &#62;&#62;= writeTVar x . fn </code></pre> It would be easy to use this to add commands that eg, increment counters in the database, and the STM would ensure that it works correctly with multiple concurrent writers. No locking needed. A quick example, which could be improved by changing the DB type to support Int as well as String values:<p><pre><code> incrCommand :: Handle -&#62; [String] -&#62; (TVar DB) -&#62; IO () incrCommand handle k db = do appV incvar db hPutStrLn handle $ "OK" where incvar k v = insert k $ show $ (fromMaybe 0 $ readMaybe v) + 1 </code></pre> I don't know how well Haskell's STM performs compared with eg, database transactions or locking. It's been more than fast enough for my own needs. Anybody know?
评论 #5232650 未加载
tincoover 12 years ago
<p><pre><code> conv k v db = insert k v db </code></pre> You could rewrite that as:<p><pre><code> conv = insert </code></pre> Probably some remnant of you figuring out how you should write appV, which is a proper helpful function, but probably should be called updateTVar or something :)
评论 #5232932 未加载
stuffihavemadeover 12 years ago
If you like this, check out <a href="http://www.happstack.com/docs/crashcourse/AcidState.html" rel="nofollow">http://www.happstack.com/docs/crashcourse/AcidState.html</a> . It allows for arbitrary Haskell data structures to safely and automatically be marshaled in and out of the data store.
评论 #5232582 未加载
lclarkmichalekover 12 years ago
How do I store something with a \n in it ;)<p>Edit: While I posted this as a joke, I do love the way that redis avoids any kind of injection attack via prepending the message length. Of course, I understand that this project is not at the stage where it is meant to be deployed in production/security become an issue; it's just interesting to see how the different communication methods have different consequences.
dschiptsovover 12 years ago
No monads?!)
评论 #5232968 未加载