Compare commits
2 Commits
87f6eb00f6
...
0c75d66122
Author | SHA1 | Date | |
---|---|---|---|
0c75d66122 | |||
e189f87dd7 |
@ -4,15 +4,12 @@ module Control.Addressbook.Query where
|
||||
import Data.Text
|
||||
(Text)
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as TE
|
||||
|
||||
import Conduit
|
||||
import qualified Data.Conduit.Binary as CB
|
||||
import qualified Data.Conduit.Combinators as C
|
||||
import qualified Data.Conduit.List as CL
|
||||
import qualified Data.Conduit.Text as CT
|
||||
|
||||
import qualified Data.Map.Strict as Map
|
||||
|
||||
import System.IO
|
||||
(stdout)
|
||||
@ -26,27 +23,20 @@ import System.FilePath
|
||||
|
||||
import Control.Exception
|
||||
(catch)
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
|
||||
-- XXX: The current Data.Trie implementation is much slower than Data.Set
|
||||
query :: Text -> IO ()
|
||||
query prefix = do
|
||||
datDir <- fromMaybe "./" <$> lookupEnv "HOME"
|
||||
state <- catch @IOError (runResourceT $ runConduit $ readState datDir) (\_ -> pure Map.empty)
|
||||
runConduit $ outputResults state
|
||||
catch @IOError (runResourceT $ runConduit $ filterAddresses datDir) $ \e -> print e
|
||||
where
|
||||
readState :: FilePath -> ConduitM () Void (ResourceT IO) (Map.Map ByteString [Text])
|
||||
readState dir =
|
||||
prefixLC = T.toLower prefix
|
||||
filterAddresses :: FilePath -> ConduitT () Void (ResourceT IO) ()
|
||||
filterAddresses dir =
|
||||
CB.sourceFile (dir </> ".addressbook.dat")
|
||||
.| CT.decode CT.utf8
|
||||
.| CT.lines
|
||||
.| C.foldMap (\s -> Map.singleton (TE.encodeUtf8 $ T.toLower s) [s])
|
||||
prefixB = TE.encodeUtf8 prefix
|
||||
outputResults :: Map.Map ByteString [Text] -> ConduitM () Void IO ()
|
||||
outputResults state =
|
||||
CL.sourceList (Map.elems $ Map.filterWithKey (\k _ -> prefixB `B.isPrefixOf` k) state)
|
||||
.| C.concat
|
||||
.| C.map (<> "\n")
|
||||
.| C.filter (\v -> prefixLC `T.isPrefixOf` (T.toLower v))
|
||||
.| CT.encode CT.utf8
|
||||
.| C.map (<> "\n")
|
||||
.| CB.sinkHandle stdout
|
||||
|
@ -17,7 +17,7 @@ import System.IO
|
||||
|
||||
import qualified Data.Foldable as F
|
||||
|
||||
import qualified Data.Trie as Trie
|
||||
import qualified Data.Map.Strict as Map
|
||||
|
||||
import Data.Maybe
|
||||
(fromMaybe)
|
||||
@ -36,7 +36,7 @@ run = do
|
||||
datDir <- fromMaybe "./" <$> lookupEnv "HOME"
|
||||
runResourceT $ do
|
||||
x <- runConduit stream
|
||||
runConduit (CL.sourceList (Trie.keys x) .| C.map (<> "\n") .| CB.sinkFileCautious (datDir </> ".addressbook.dat"))
|
||||
runConduit (CL.sourceList (Map.keys x) .| C.map (<> "\n") .| CB.sinkFileCautious (datDir </> ".addressbook.dat"))
|
||||
where
|
||||
separate = \case
|
||||
From x -> [x]
|
||||
@ -49,4 +49,4 @@ run = do
|
||||
.| combine
|
||||
.| C.concatMap separate
|
||||
.| CT.encode CT.utf8
|
||||
.| C.foldMap (`Trie.singleton` ())
|
||||
.| C.foldMap (`Map.singleton` ())
|
||||
|
Loading…
Reference in New Issue
Block a user