File: tcadb.hs

package info (click to toggle)
tokyocabinet-haskell 0.0.5-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 308 kB
  • sloc: haskell: 2,276; makefile: 3
file content (31 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Control.Monad
import Database.TokyoCabinet.ADB

main = do adb <- new
          -- open the abstract database object
          -- "+" means that the database will be an on-memory tree database
          open adb "+" >>= err adb "open failed"
          -- store records
          puts adb [("foo", "hop"), ("bar", "step"), ("baz", "jump")]
                   >>= err adb "put failed" . (all id)
          -- retrieve records
          get_print adb "foo"
          -- traverse records
          iterinit adb
          iter adb >>= mapM_ (\k -> putStr (k++":") >> get_print adb k)
          -- close the database
          close adb >>= err adb "close failed"
    where
      puts :: ADB -> [(String, String)] -> IO [Bool]
      puts adb = mapM (uncurry $ put adb)

      get_print :: ADB -> String -> IO ()
      get_print adb key = get adb key >>=
                          maybe (error "something goes wrong") putStrLn

      err :: ADB -> String -> Bool -> IO ()
      err adb msg = flip unless $ error msg

      iter :: ADB -> IO [String]
      iter adb = iternext adb >>=
                 maybe (return []) (\x -> return . (x:) =<< iter adb)