optimise getUUID

This avoids a Map lookup each time it's called, instead the GitConfig field
lazily looks it up once and then caches.
This commit is contained in:
Joey Hess 2016-01-20 16:55:06 -04:00
parent 737e45156e
commit 23ff58cd4f
Failed to extract signature
2 changed files with 14 additions and 5 deletions

View file

@ -6,7 +6,7 @@
- UUIDs of remotes are cached in git config, using keys named - UUIDs of remotes are cached in git config, using keys named
- remote.<name>.annex-uuid - remote.<name>.annex-uuid
- -
- Copyright 2010-2013 Joey Hess <id@joeyh.name> - Copyright 2010-2016 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -29,6 +29,7 @@ module Annex.UUID (
) where ) where
import Annex.Common import Annex.Common
import qualified Annex
import qualified Git import qualified Git
import qualified Git.Config import qualified Git.Config
import Config import Config
@ -58,9 +59,10 @@ gCryptNameSpace = U5.generateNamed U5.namespaceURL $
{- Get current repository's UUID. -} {- Get current repository's UUID. -}
getUUID :: Annex UUID getUUID :: Annex UUID
getUUID = getRepoUUID =<< gitRepo getUUID = annexUUID <$> Annex.getGitConfig
{- Looks up a repo's UUID, caching it in .git/config if it's not already. -} {- Looks up a remote repo's UUID, caching it in .git/config if
- it's not already. -}
getRepoUUID :: Git.Repo -> Annex UUID getRepoUUID :: Git.Repo -> Annex UUID
getRepoUUID r = do getRepoUUID r = do
c <- toUUID <$> getConfig cachekey "" c <- toUUID <$> getConfig cachekey ""
@ -78,7 +80,9 @@ getRepoUUID r = do
cachekey = remoteConfig r "uuid" cachekey = remoteConfig r "uuid"
removeRepoUUID :: Annex () removeRepoUUID :: Annex ()
removeRepoUUID = unsetConfig configkey removeRepoUUID = do
unsetConfig configkey
storeUUID NoUUID
getUncachedUUID :: Git.Repo -> UUID getUncachedUUID :: Git.Repo -> UUID
getUncachedUUID = toUUID . Git.Config.get key "" getUncachedUUID = toUUID . Git.Config.get key ""
@ -98,7 +102,9 @@ prepUUID = whenM ((==) NoUUID <$> getUUID) $
storeUUID =<< liftIO genUUID storeUUID =<< liftIO genUUID
storeUUID :: UUID -> Annex () storeUUID :: UUID -> Annex ()
storeUUID = storeUUIDIn configkey storeUUID u = do
Annex.changeGitConfig $ \c -> c { annexUUID = u }
storeUUIDIn configkey u
storeUUIDIn :: ConfigKey -> UUID -> Annex () storeUUIDIn :: ConfigKey -> UUID -> Annex ()
storeUUIDIn configfield = setConfig configfield . fromUUID storeUUIDIn configfield = setConfig configfield . fromUUID

View file

@ -19,6 +19,7 @@ import qualified Git.Construct
import Git.SharedRepository import Git.SharedRepository
import Utility.DataUnits import Utility.DataUnits
import Config.Cost import Config.Cost
import Types.UUID
import Types.Distribution import Types.Distribution
import Types.Availability import Types.Availability
import Types.NumCopies import Types.NumCopies
@ -32,6 +33,7 @@ import Utility.ThreadScheduler (Seconds(..))
- such as annex.foo -} - such as annex.foo -}
data GitConfig = GitConfig data GitConfig = GitConfig
{ annexVersion :: Maybe String { annexVersion :: Maybe String
, annexUUID :: UUID
, annexNumCopies :: Maybe NumCopies , annexNumCopies :: Maybe NumCopies
, annexDiskReserve :: Integer , annexDiskReserve :: Integer
, annexDirect :: Bool , annexDirect :: Bool
@ -75,6 +77,7 @@ data GitConfig = GitConfig
extractGitConfig :: Git.Repo -> GitConfig extractGitConfig :: Git.Repo -> GitConfig
extractGitConfig r = GitConfig extractGitConfig r = GitConfig
{ annexVersion = notempty $ getmaybe (annex "version") { annexVersion = notempty $ getmaybe (annex "version")
, annexUUID = maybe NoUUID toUUID $ getmaybe (annex "uuid")
, annexNumCopies = NumCopies <$> getmayberead (annex "numcopies") , annexNumCopies = NumCopies <$> getmayberead (annex "numcopies")
, annexDiskReserve = fromMaybe onemegabyte $ , annexDiskReserve = fromMaybe onemegabyte $
readSize dataUnits =<< getmaybe (annex "diskreserve") readSize dataUnits =<< getmaybe (annex "diskreserve")