Add a hashtable file save method

This commit is contained in:
rnhmjoj 2015-04-10 19:39:28 +02:00
parent 3fb9a21753
commit 1a125265ab

View File

@ -1,24 +1,41 @@
module Breve.UrlTable module Breve.UrlTable
( UrlTable ( UrlTable
, records , load
, insert , insert
, extract , extract
) where ) where
import Breve.Generator import Breve.Generator
import Control.Monad (forever)
import Control.Concurrent (forkIO, threadDelay)
import Text.Read (readMaybe)
import qualified Data.HashTable.IO as H import qualified Data.HashTable.IO as H
type UrlTable = H.CuckooHashTable Word Url type UrlTable = H.CuckooHashTable Word Url
--Empty url hash table -- Periodically write a url table to a file
records :: IO UrlTable sync :: UrlTable -> FilePath -> IO ()
records = H.new sync table file = forever $ do
threadDelay (round 3.0e8)
content <- fmap show (H.toList table)
writeFile file content
-- Insert the url in the table and return the word -- Load a url table from a file
load :: FilePath -> IO UrlTable
load file = do
content <- readFile file
table <- case readMaybe content of
Just list -> H.fromList list
Nothing -> H.new
forkIO (sync table file)
return table
-- Insert the url in a table and return the word
insert :: UrlTable -> Url -> IO Word insert :: UrlTable -> Url -> IO Word
insert table url = H.insert table new url >> return new insert table url = H.insert table new url >> return new
where new = wordID url where new = wordID url
-- Lookup the table for the associated url -- Lookup a table for the associated url
extract :: UrlTable -> Word -> IO (Maybe Url) extract :: UrlTable -> Word -> IO (Maybe Url)
extract = H.lookup extract = H.lookup