2012-07-23 03:16:56 +00:00
|
|
|
{- git-annex assistant remotes needing scanning
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.ScanRemotes where
|
|
|
|
|
|
|
|
import Common.Annex
|
2012-08-23 17:41:38 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2012-07-23 03:16:56 +00:00
|
|
|
|
2012-08-23 17:41:38 +00:00
|
|
|
import Data.Function
|
2012-07-23 03:16:56 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2012-08-23 19:22:23 +00:00
|
|
|
data ScanInfo = ScanInfo
|
|
|
|
{ scanPriority :: Int
|
|
|
|
, fullScan :: Bool
|
|
|
|
}
|
2012-08-23 17:41:38 +00:00
|
|
|
|
2012-08-23 19:22:23 +00:00
|
|
|
type ScanRemoteMap = TMVar (M.Map Remote ScanInfo)
|
2012-07-23 03:16:56 +00:00
|
|
|
|
|
|
|
{- The TMVar starts empty, and is left empty when there are no remotes
|
|
|
|
- to scan. -}
|
|
|
|
newScanRemoteMap :: IO ScanRemoteMap
|
|
|
|
newScanRemoteMap = atomically newEmptyTMVar
|
|
|
|
|
|
|
|
{- Blocks until there is a remote that needs to be scanned.
|
2012-08-23 17:41:38 +00:00
|
|
|
- Processes higher priority remotes first. -}
|
2012-08-23 19:22:23 +00:00
|
|
|
getScanRemote :: ScanRemoteMap -> IO (Remote, ScanInfo)
|
2012-07-23 03:16:56 +00:00
|
|
|
getScanRemote v = atomically $ do
|
|
|
|
m <- takeTMVar v
|
2012-08-23 19:22:23 +00:00
|
|
|
let l = reverse $ sortBy (compare `on` scanPriority . snd) $ M.toList m
|
2012-08-05 19:14:21 +00:00
|
|
|
case l of
|
|
|
|
[] -> retry -- should never happen
|
2012-08-23 19:22:23 +00:00
|
|
|
(ret@(r, _):_) -> do
|
|
|
|
let m' = M.delete r m
|
2012-08-05 19:14:21 +00:00
|
|
|
unless (M.null m') $
|
|
|
|
putTMVar v m'
|
2012-08-23 19:22:23 +00:00
|
|
|
return ret
|
2012-07-23 03:16:56 +00:00
|
|
|
|
|
|
|
{- Adds new remotes that need scanning to the map. -}
|
2012-08-23 19:22:23 +00:00
|
|
|
addScanRemotes :: ScanRemoteMap -> [Remote] -> Bool -> IO ()
|
|
|
|
addScanRemotes _ [] _ = noop
|
|
|
|
addScanRemotes v rs full = atomically $ do
|
2012-08-23 17:41:38 +00:00
|
|
|
m <- fromMaybe M.empty <$> tryTakeTMVar v
|
2012-08-24 17:28:20 +00:00
|
|
|
putTMVar v $ M.unionWith merge (M.fromList $ zip rs (map info rs)) m
|
2012-08-23 19:22:23 +00:00
|
|
|
where
|
|
|
|
info r = ScanInfo (Remote.cost r) full
|
2012-08-24 17:28:20 +00:00
|
|
|
merge x y = ScanInfo
|
|
|
|
{ scanPriority = max (scanPriority x) (scanPriority y)
|
|
|
|
, fullScan = fullScan x || fullScan y
|
|
|
|
}
|