| 1 | module Search (Search, withDB, searchMe, searchChild, searchFamily) where |
| 2 | |
| 3 | import Data.List |
| 4 | import Data.Map (Map) |
| 5 | import qualified Data.Map as Map hiding (Map) |
| 6 | import Data.Ord |
| 7 | import Database.HDBC.Sqlite3 |
| 8 | import Msg |
| 9 | import Sql |
| 10 | import System.EasyFile |
| 11 | |
| 12 | ---------------------------------------------------------------- |
| 13 | |
| 14 | type Search a = ID -> FilePath -> Connection -> IO a |
| 15 | |
| 16 | ---------------------------------------------------------------- |
| 17 | |
| 18 | searchMe :: Search [Msg] |
| 19 | searchMe mid dir conn = chooseOne dir <$> getByID conn mid |
| 20 | |
| 21 | ---------------------------------------------------------------- |
| 22 | |
| 23 | searchChild :: Search [Msg] |
| 24 | searchChild mid dir conn = chooseOne dir <$> getByPaID conn [mid] |
| 25 | |
| 26 | ---------------------------------------------------------------- |
| 27 | |
| 28 | searchFamily :: Search [Msg] |
| 29 | searchFamily mid dir conn = sortBy (comparing date) <$> findFamily |
| 30 | where |
| 31 | findFamily = findRoot conn mid mid >>= findDescendants conn dir |
| 32 | |
| 33 | data ParentError = NoEntry | NoPid |
| 34 | |
| 35 | getPaid :: Connection -> ID -> IO (Either ParentError ID) |
| 36 | getPaid conn mid = getPid <$> getByID conn mid |
| 37 | where |
| 38 | getPid [] = Left NoEntry |
| 39 | getPid (e:_) |
| 40 | | pid == "" = Left NoPid |
| 41 | | otherwise = Right pid |
| 42 | where |
| 43 | pid = paid e |
| 44 | |
| 45 | findRoot :: Connection -> ID -> ID -> IO ID |
| 46 | findRoot conn previd mid = |
| 47 | getPaid conn mid >>= either terminate (findRoot conn mid) |
| 48 | where |
| 49 | terminate NoEntry = return previd |
| 50 | terminate NoPid = return mid |
| 51 | |
| 52 | type Hash = Map ID Msg |
| 53 | |
| 54 | findDescendants :: Connection -> FilePath -> ID -> IO [Msg] |
| 55 | findDescendants conn dir rtid = do |
| 56 | root <- head . chooseOne dir <$> getByID conn rtid |
| 57 | let mmap = Map.insert rtid root Map.empty |
| 58 | findChildren ([rtid],mmap) |
| 59 | where |
| 60 | findChildren :: ([ID],Hash) -> IO [Msg] |
| 61 | findChildren (ids,hash) = getByPaID conn ids >>= findChildren' |
| 62 | where |
| 63 | findChildren' [] = return (Map.elems hash) |
| 64 | findChildren' msgs = findChildren $ pushChildren hash msgs [] |
| 65 | |
| 66 | pushChildren :: Hash -> [Msg] -> [ID] -> ([ID],Hash) |
| 67 | pushChildren hash [] ids = (ids, hash) |
| 68 | pushChildren hash (m:ms) ids |
| 69 | | Map.notMember mid hash = pushChildren hash' ms (mid:ids) |
| 70 | | mdir == dir = pushChildren hash' ms ids |
| 71 | -- insert overwrites the value |
| 72 | | otherwise = pushChildren hash ms ids |
| 73 | where |
| 74 | hash' = Map.insert mid m hash |
| 75 | mdir = (takeDirectory.path) m |
| 76 | mid = myid m |
| 77 | |
| 78 | ---------------------------------------------------------------- |
| 79 | -- to express failure, the empty list is used |
| 80 | |
| 81 | chooseOne :: FilePath -> [Msg] -> [Msg] |
| 82 | chooseOne _ [] = [] -- failure |
| 83 | chooseOne "" (m:_) = [m] |
| 84 | chooseOne dir msgs@(m:_) |
| 85 | | null sames = [m] |
| 86 | | otherwise = [head sames] |
| 87 | where |
| 88 | sames = filter sameDir msgs |
| 89 | sameDir x = (takeDirectory.path) x == dir |