2019-02-20 20:59:10 +00:00
|
|
|
{- Sqlite database of ContentIdentifiers imported from special remotes.
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-:
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE QuasiQuotes, TypeFamilies, TemplateHaskell #-}
|
2019-04-09 23:58:24 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, GADTs, FlexibleContexts, EmptyDataDecls #-}
|
2019-02-20 20:59:10 +00:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2019-07-30 16:49:37 +00:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2019-02-20 20:59:10 +00:00
|
|
|
|
|
|
|
module Database.ContentIdentifier (
|
|
|
|
ContentIdentifierHandle,
|
|
|
|
openDb,
|
|
|
|
closeDb,
|
|
|
|
flushDbQueue,
|
|
|
|
recordContentIdentifier,
|
2019-03-04 21:50:41 +00:00
|
|
|
getContentIdentifiers,
|
2019-02-20 20:59:10 +00:00
|
|
|
getContentIdentifierKeys,
|
2019-03-06 22:04:30 +00:00
|
|
|
recordAnnexBranchTree,
|
|
|
|
getAnnexBranchTree,
|
2019-03-07 16:56:40 +00:00
|
|
|
needsUpdateFromLog,
|
|
|
|
updateFromLog,
|
2019-02-20 20:59:10 +00:00
|
|
|
ContentIdentifiersId,
|
2019-03-06 22:04:30 +00:00
|
|
|
AnnexBranchId,
|
2019-02-20 20:59:10 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Database.Types
|
|
|
|
import qualified Database.Queue as H
|
|
|
|
import Database.Init
|
|
|
|
import Annex.Locations
|
|
|
|
import Annex.Common hiding (delete)
|
2019-03-07 16:56:40 +00:00
|
|
|
import qualified Annex.Branch
|
2019-02-21 17:38:27 +00:00
|
|
|
import Types.Import
|
2019-03-06 22:04:30 +00:00
|
|
|
import Git.Types
|
|
|
|
import Git.Sha
|
2019-03-07 16:56:40 +00:00
|
|
|
import Git.FilePath
|
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.DiffTree as DiffTree
|
|
|
|
import Logs
|
|
|
|
import qualified Logs.ContentIdentifier as Log
|
2019-02-20 20:59:10 +00:00
|
|
|
|
|
|
|
import Database.Persist.Sql hiding (Key)
|
|
|
|
import Database.Persist.TH
|
|
|
|
|
|
|
|
data ContentIdentifierHandle = ContentIdentifierHandle H.DbQueue
|
|
|
|
|
|
|
|
share [mkPersist sqlSettings, mkMigrate "migrateContentIdentifier"] [persistLowerCase|
|
|
|
|
ContentIdentifiers
|
|
|
|
remote UUID
|
|
|
|
cid ContentIdentifier
|
2019-03-06 15:14:33 +00:00
|
|
|
key IKey
|
2019-03-06 22:04:30 +00:00
|
|
|
-- The last git-annex branch tree sha that was used to update
|
|
|
|
-- ContentIdentifiers
|
|
|
|
AnnexBranch
|
|
|
|
tree SRef
|
|
|
|
UniqueTree tree
|
2019-02-20 20:59:10 +00:00
|
|
|
|]
|
|
|
|
|
|
|
|
{- Opens the database, creating it if it doesn't exist yet.
|
|
|
|
-
|
|
|
|
- Only a single process should write to the database at a time, so guard
|
|
|
|
- any writes with the gitAnnexContentIdentifierLock.
|
|
|
|
-}
|
|
|
|
openDb :: Annex ContentIdentifierHandle
|
|
|
|
openDb = do
|
|
|
|
dbdir <- fromRepo gitAnnexContentIdentifierDbDir
|
|
|
|
let db = dbdir </> "db"
|
|
|
|
unlessM (liftIO $ doesFileExist db) $ do
|
|
|
|
initDb db $ void $
|
|
|
|
runMigrationSilent migrateContentIdentifier
|
2019-02-27 17:52:56 +00:00
|
|
|
h <- liftIO $ H.openDbQueue H.SingleWriter db "content_identifiers"
|
2019-02-20 20:59:10 +00:00
|
|
|
return $ ContentIdentifierHandle h
|
|
|
|
|
|
|
|
closeDb :: ContentIdentifierHandle -> Annex ()
|
|
|
|
closeDb (ContentIdentifierHandle h) = liftIO $ H.closeDbQueue h
|
|
|
|
|
|
|
|
queueDb :: ContentIdentifierHandle -> SqlPersistM () -> IO ()
|
|
|
|
queueDb (ContentIdentifierHandle h) = H.queueDb h checkcommit
|
|
|
|
where
|
|
|
|
-- commit queue after 1000 changes
|
|
|
|
checkcommit sz _lastcommittime
|
|
|
|
| sz > 1000 = return True
|
|
|
|
| otherwise = return False
|
|
|
|
|
|
|
|
flushDbQueue :: ContentIdentifierHandle -> IO ()
|
|
|
|
flushDbQueue (ContentIdentifierHandle h) = H.flushDbQueue h
|
|
|
|
|
|
|
|
-- Be sure to also update the git-annex branch when using this.
|
|
|
|
recordContentIdentifier :: ContentIdentifierHandle -> UUID -> ContentIdentifier -> Key -> IO ()
|
|
|
|
recordContentIdentifier h u cid k = queueDb h $ do
|
2019-04-09 23:58:24 +00:00
|
|
|
void $ insert_ $ ContentIdentifiers u cid (toIKey k)
|
2019-02-20 20:59:10 +00:00
|
|
|
|
2019-03-04 20:48:07 +00:00
|
|
|
getContentIdentifiers :: ContentIdentifierHandle -> UUID -> Key -> IO [ContentIdentifier]
|
|
|
|
getContentIdentifiers (ContentIdentifierHandle h) u k = H.queryDbQueue h $ do
|
2019-03-05 19:56:28 +00:00
|
|
|
l <- selectList
|
2019-03-06 15:14:33 +00:00
|
|
|
[ ContentIdentifiersKey ==. toIKey k
|
2019-03-05 19:56:28 +00:00
|
|
|
, ContentIdentifiersRemote ==. u
|
|
|
|
] []
|
2019-03-04 21:50:41 +00:00
|
|
|
return $ map (contentIdentifiersCid . entityVal) l
|
2019-03-04 20:48:07 +00:00
|
|
|
|
2019-02-20 20:59:10 +00:00
|
|
|
getContentIdentifierKeys :: ContentIdentifierHandle -> UUID -> ContentIdentifier -> IO [Key]
|
|
|
|
getContentIdentifierKeys (ContentIdentifierHandle h) u cid =
|
|
|
|
H.queryDbQueue h $ do
|
|
|
|
l <- selectList
|
|
|
|
[ ContentIdentifiersCid ==. cid
|
|
|
|
, ContentIdentifiersRemote ==. u
|
|
|
|
] []
|
2019-03-06 15:14:33 +00:00
|
|
|
return $ map (fromIKey . contentIdentifiersKey . entityVal) l
|
2019-03-06 22:04:30 +00:00
|
|
|
|
|
|
|
recordAnnexBranchTree :: ContentIdentifierHandle -> Sha -> IO ()
|
|
|
|
recordAnnexBranchTree h s = queueDb h $ do
|
|
|
|
deleteWhere ([] :: [Filter AnnexBranch])
|
|
|
|
void $ insertUnique $ AnnexBranch $ toSRef s
|
|
|
|
|
|
|
|
getAnnexBranchTree :: ContentIdentifierHandle -> IO Sha
|
|
|
|
getAnnexBranchTree (ContentIdentifierHandle h) = H.queryDbQueue h $ do
|
|
|
|
l <- selectList ([] :: [Filter AnnexBranch]) []
|
|
|
|
case l of
|
|
|
|
(s:[]) -> return $ fromSRef $ annexBranchTree $ entityVal s
|
|
|
|
_ -> return emptyTree
|
2019-03-07 16:56:40 +00:00
|
|
|
|
|
|
|
{- Check if the git-annex branch has been updated and the database needs
|
|
|
|
- to be updated with any new content identifiers in it. -}
|
|
|
|
needsUpdateFromLog :: ContentIdentifierHandle -> Annex (Maybe (Sha, Sha))
|
|
|
|
needsUpdateFromLog db = do
|
|
|
|
oldtree <- liftIO $ getAnnexBranchTree db
|
|
|
|
inRepo (Git.Ref.tree Annex.Branch.fullname) >>= \case
|
|
|
|
Just currtree | currtree /= oldtree ->
|
|
|
|
return $ Just (oldtree, currtree)
|
|
|
|
_ -> return Nothing
|
|
|
|
|
|
|
|
{- The database should be locked for write when calling this. -}
|
|
|
|
updateFromLog :: ContentIdentifierHandle -> (Sha, Sha) -> Annex ()
|
|
|
|
updateFromLog db (oldtree, currtree) = do
|
|
|
|
(l, cleanup) <- inRepo $
|
|
|
|
DiffTree.diffTreeRecursive oldtree currtree
|
|
|
|
mapM_ go l
|
|
|
|
void $ liftIO $ cleanup
|
|
|
|
liftIO $ do
|
|
|
|
recordAnnexBranchTree db currtree
|
|
|
|
flushDbQueue db
|
|
|
|
where
|
|
|
|
go ti = case extLogFileKey remoteContentIdentifierExt (getTopFilePath (DiffTree.file ti)) of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just k -> do
|
|
|
|
l <- Log.getContentIdentifiers k
|
|
|
|
liftIO $ forM_ l $ \(u, cids) ->
|
|
|
|
forM_ cids $ \cid ->
|
|
|
|
recordContentIdentifier db u cid k
|