Add a hashtable file save method

master
rnhmjoj 2015-04-10 19:39:28 +02:00
parent 3fb9a21753
commit 1a125265ab
1 changed files with 23 additions and 6 deletions

View File

@ -1,24 +1,41 @@
module Breve.UrlTable
( UrlTable
, records
, load
, insert
, extract
) where
import Breve.Generator
import Control.Monad (forever)
import Control.Concurrent (forkIO, threadDelay)
import Text.Read (readMaybe)
import qualified Data.HashTable.IO as H
type UrlTable = H.CuckooHashTable Word Url
--Empty url hash table
records :: IO UrlTable
records = H.new
-- Periodically write a url table to a file
sync :: UrlTable -> FilePath -> IO ()
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 table url = H.insert table new url >> return new
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 = H.lookup