aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-07-19 14:02:49 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-07-19 14:02:49 -0400
commitb642cb47275f5e98b82203e825d7dd57d571d65e (patch)
treeb462d17de42e9b73292d389436407d5a80e546fd
parent11b52352183e4206648225214d53fe2db7765a60 (diff)
slightly more efficient checking of versionUsesKeysDatabase
It's a mvar lookup either way, but I think this way will be slightly more efficient. And it reduces the number of places where it's checked to 1.
-rw-r--r--Database/Keys.hs19
-rw-r--r--Database/Keys/Handle.hs4
2 files changed, 13 insertions, 10 deletions
diff --git a/Database/Keys.hs b/Database/Keys.hs
index 73c9a5bff..3d9330e90 100644
--- a/Database/Keys.hs
+++ b/Database/Keys.hs
@@ -54,12 +54,9 @@ import Database.Esqueleto hiding (Key)
- Any queued writes will be flushed before the read.
-}
runReader :: Monoid v => (SQL.ReadHandle -> Annex v) -> Annex v
-runReader a = ifM versionUsesKeysDatabase
- ( do
- h <- getDbHandle
- withDbState h go
- , return mempty
- )
+runReader a = do
+ h <- getDbHandle
+ withDbState h go
where
go DbUnavailable = return (mempty, DbUnavailable)
go st@(DbOpen qh) = do
@@ -81,7 +78,7 @@ runReaderIO a = runReader (liftIO . a)
-
- The database is created if it doesn't exist yet. -}
runWriter :: (SQL.WriteHandle -> Annex ()) -> Annex ()
-runWriter a = whenM versionUsesKeysDatabase $ do
+runWriter a = do
h <- getDbHandle
withDbState h go
where
@@ -105,7 +102,10 @@ getDbHandle = go =<< Annex.getState Annex.keysdbhandle
where
go (Just h) = pure h
go Nothing = do
- h <- liftIO newDbHandle
+ h <- ifM versionUsesKeysDatabase
+ ( liftIO newDbHandle
+ , liftIO unavailableDbHandle
+ )
Annex.changeState $ \s -> s { Annex.keysdbhandle = Just h }
return h
@@ -150,8 +150,7 @@ openDb createdb _ = catchPermissionDenied permerr $ withExclusiveLock gitAnnexKe
- data to it.
-}
closeDb :: Annex ()
-closeDb = whenM versionUsesKeysDatabase $
- liftIO . closeDbHandle =<< getDbHandle
+closeDb = liftIO . closeDbHandle =<< getDbHandle
addAssociatedFile :: Key -> TopFilePath -> Annex ()
addAssociatedFile k f = runWriterIO $ SQL.addAssociatedFile (toIKey k) f
diff --git a/Database/Keys/Handle.hs b/Database/Keys/Handle.hs
index 1ef16d031..eec9e0fda 100644
--- a/Database/Keys/Handle.hs
+++ b/Database/Keys/Handle.hs
@@ -8,6 +8,7 @@
module Database.Keys.Handle (
DbHandle,
newDbHandle,
+ unavailableDbHandle,
DbState(..),
withDbState,
flushDbQueue,
@@ -33,6 +34,9 @@ data DbState = DbClosed | DbOpen H.DbQueue | DbUnavailable
newDbHandle :: IO DbHandle
newDbHandle = DbHandle <$> newMVar DbClosed
+unavailableDbHandle :: IO DbHandle
+unavailableDbHandle = DbHandle <$> newMVar DbUnavailable
+
-- Runs an action on the state of the handle, which can change its state.
-- The MVar is empty while the action runs, which blocks other users
-- of the handle from running.