import Types.LockCache
import Types.DesktopNotify
import Types.CleanupActions
+import qualified Database.AssociatedFiles.Types
#ifdef WITH_QUVI
import Utility.Quvi (QuviVersion)
#endif
, desktopnotify :: DesktopNotify
, workers :: [Either AnnexState (Async AnnexState)]
, concurrentjobs :: Maybe Int
+ , associatedfilesdbhandle :: Maybe Database.AssociatedFiles.Types.DbHandle
}
newState :: GitConfig -> Git.Repo -> AnnexState
, desktopnotify = mempty
, workers = []
, concurrentjobs = Nothing
+ , associatedfilesdbhandle = Nothing
}
{- Makes an Annex state object for the specified git repo.
updateAssociatedFiles :: Key -> FilePath -> Annex ()
updateAssociatedFiles k f = do
- h <- AssociatedFiles.openDb
- liftIO $ do
- AssociatedFiles.addDb h k f
- AssociatedFiles.closeDb h
+ AssociatedFiles.addDb k f
+ AssociatedFiles.flushDb
module Database.AssociatedFiles (
DbHandle,
openDb,
+ flushDb,
closeDb,
addDb,
getDb,
) where
import Database.Types
+import Database.AssociatedFiles.Types
import qualified Database.Handle as H
import Locations
import Common hiding (delete)
import Database.Persist.TH
import Database.Esqueleto hiding (Key)
-newtype DbHandle = DbHandle H.DbHandle
-
share [mkPersist sqlSettings, mkMigrate "migrateAssociated"] [persistLowerCase|
Associated
key SKey
closeDb :: DbHandle -> IO ()
closeDb (DbHandle h) = H.closeDb h
-addDb :: DbHandle -> Key -> FilePath -> IO ()
-addDb (DbHandle h) k f = H.queueDb h (\_ _ -> pure True) $ do
+withDbHandle :: (H.DbHandle -> IO a) -> Annex a
+withDbHandle a = do
+ (DbHandle h) <- dbHandle
+ liftIO $ a h
+
+dbHandle :: Annex DbHandle
+dbHandle = maybe startup return =<< Annex.getState Annex.associatedfilesdbhandle
+ where
+ startup = do
+ h <- openDb
+ Annex.changeState $ \s -> s { Annex.associatedfilesdbhandle = Just h }
+ return h
+
+{- Flushes any changes made to the database. -}
+flushDb :: Annex ()
+flushDb = withDbHandle H.flushQueueDb
+
+addDb :: Key -> FilePath -> Annex ()
+addDb k f = withDbHandle $ \h -> H.queueDb h (\_ _ -> pure True) $ do
-- If the same file was associated with a different key before,
-- remove that.
delete $ from $ \r -> do
{- Note that the files returned used to be associated with the key, but
- some of them may not be any longer. -}
-getDb :: DbHandle -> Key -> IO [FilePath]
-getDb (DbHandle h) = H.queryDb h . getDb' . toSKey
+getDb :: Key -> Annex [FilePath]
+getDb k = withDbHandle $ \h -> H.queryDb h $ getDb' $ toSKey k
getDb' :: SKey -> SqlPersistM [FilePath]
getDb' sk = do
return (r ^. AssociatedFile)
return $ map unValue l
-removeDb :: DbHandle -> Key -> FilePath -> IO ()
-removeDb (DbHandle h) k f = H.queueDb h (\_ _ -> pure True) $
+removeDb :: Key -> FilePath -> Annex ()
+removeDb k f = withDbHandle $ \h -> H.queueDb h (\_ _ -> pure True) $
delete $ from $ \r -> do
where_ (r ^. AssociatedKey ==. val sk &&. r ^. AssociatedFile ==. val f)
where
--- /dev/null
+{- Sqlite database used for tracking a key's associated files, data types.
+ -
+ - Copyright 2015 Joey Hess <id@joeyh.name>
+ -:
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Database.AssociatedFiles.Types (
+ DbHandle(..)
+) where
+
+import qualified Database.Handle as H
+
+newtype DbHandle = DbHandle H.DbHandle
import Utility.Exception
import Utility.Monad
-import Messages
import Database.Persist.Sqlite
import qualified Database.Sqlite as Sqlite
import Control.Monad.Logger (runNoLoggingT)
import Data.List
import Data.Time.Clock
+import System.IO
{- A DbHandle is a reference to a worker thread that communicates with
- the database. It has a MVar which Jobs are submitted to. -}
workerThread :: T.Text -> TableName -> MVar Job -> IO ()
workerThread db tablename jobs = catchNonAsync (run loop) showerr
where
- showerr e = liftIO $ warningIO $
+ showerr e = liftIO $ hPutStrLn stderr $
"sqlite worker thread crashed: " ++ show e
loop = do