-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokyoGraph.hs
33 lines (30 loc) · 1.17 KB
/
TokyoGraph.hs
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
32
33
module TokyoGraph (fetchGraph) where
import Database.TokyoCabinet
import IntBS
import Graph
import JSON2Graph
import Control.Monad.Trans -- liftIO
import System.IO
fetchGraph :: FilePath -> IntBS -> Maybe Int -> Maybe Int -> TCM (IntBS,Graph)
fetchGraph fileName dic maxElems progress = do
tc <- new :: TCM HDB -- alternatively you can use BDB or FDB
open tc fileName [OREADER]
iterinit tc
collect maxElems tc 0 []
where
collect maxElems tc count acc =
let (haveMax,theMax) = case maxElems of {Just n -> (True,n); _ -> (False,0)} in
do
k <- iternext tc
case k of
Just key | not haveMax || count < theMax -> do
liftIO (case progress of
Just n | count `mod` n == 0 -> do
hPutChar stderr '.'
hFlush stderr
_ -> return ())
v <- get tc key
case v of
Just val -> collect maxElems tc (succ count) ((key,val):acc)
_ -> error "iternext has a key without a val"
_ -> return (json2graph dic (reverse acc))