diff --git a/src/Breve/UrlTable.hs b/src/Breve/UrlTable.hs index ac3b143..aa57009 100644 --- a/src/Breve/UrlTable.hs +++ b/src/Breve/UrlTable.hs @@ -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