TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

63 点作者 mrhonza超过 12 年前

6 条评论

sold超过 12 年前
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 未加载
joeyh超过 12 年前
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 未加载
tinco超过 12 年前
<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 未加载
stuffihavemade超过 12 年前
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 未加载
lclarkmichalek超过 12 年前
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.
dschiptsov超过 12 年前
No monads?!)
评论 #5232968 未加载