2016-01-11 19:52:11 +00:00
|
|
|
|
{- Sqlite database of information about Keys
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
{-# LANGUAGE QuasiQuotes, TypeFamilies, TemplateHaskell #-}
|
|
|
|
|
{-# LANGUAGE OverloadedStrings, GADTs, FlexibleContexts #-}
|
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
|
|
|
|
|
{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
|
|
|
|
|
|
|
|
|
|
module Database.Keys.SQL where
|
|
|
|
|
|
|
|
|
|
import Database.Types
|
2016-01-12 17:01:44 +00:00
|
|
|
|
import Database.Handle
|
2016-01-11 19:52:11 +00:00
|
|
|
|
import qualified Database.Queue as H
|
|
|
|
|
import Utility.InodeCache
|
|
|
|
|
import Git.FilePath
|
|
|
|
|
|
|
|
|
|
import Database.Persist.TH
|
|
|
|
|
import Database.Esqueleto hiding (Key)
|
|
|
|
|
import Data.Time.Clock
|
|
|
|
|
import Control.Monad
|
|
|
|
|
|
|
|
|
|
share [mkPersist sqlSettings, mkMigrate "migrateKeysDb"] [persistLowerCase|
|
|
|
|
|
Associated
|
2016-01-12 18:01:50 +00:00
|
|
|
|
key IKey
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
file SFilePath
|
2016-01-11 19:52:11 +00:00
|
|
|
|
KeyFileIndex key file
|
2016-01-12 17:07:14 +00:00
|
|
|
|
FileKeyIndex file key
|
2016-01-11 19:52:11 +00:00
|
|
|
|
Content
|
2016-01-12 18:01:50 +00:00
|
|
|
|
key IKey
|
2016-01-11 19:52:11 +00:00
|
|
|
|
cache SInodeCache
|
|
|
|
|
KeyCacheIndex key cache
|
|
|
|
|
|]
|
|
|
|
|
|
2016-01-12 17:01:44 +00:00
|
|
|
|
containedTable :: TableName
|
|
|
|
|
containedTable = "content"
|
|
|
|
|
|
|
|
|
|
createTables :: SqlPersistM ()
|
|
|
|
|
createTables = void $ runMigrationSilent migrateKeysDb
|
|
|
|
|
|
2016-01-11 19:52:11 +00:00
|
|
|
|
newtype ReadHandle = ReadHandle H.DbQueue
|
|
|
|
|
|
|
|
|
|
readDb :: SqlPersistM a -> ReadHandle -> IO a
|
|
|
|
|
readDb a (ReadHandle h) = H.queryDbQueue h a
|
|
|
|
|
|
|
|
|
|
newtype WriteHandle = WriteHandle H.DbQueue
|
|
|
|
|
|
|
|
|
|
queueDb :: SqlPersistM () -> WriteHandle -> IO ()
|
|
|
|
|
queueDb a (WriteHandle h) = H.queueDb h checkcommit a
|
|
|
|
|
where
|
|
|
|
|
-- commit queue after 1000 changes or 5 minutes, whichever comes first
|
|
|
|
|
checkcommit sz lastcommittime
|
|
|
|
|
| sz > 1000 = return True
|
|
|
|
|
| otherwise = do
|
|
|
|
|
now <- getCurrentTime
|
|
|
|
|
return $ diffUTCTime lastcommittime now > 300
|
|
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
|
addAssociatedFile :: IKey -> TopFilePath -> WriteHandle -> IO ()
|
|
|
|
|
addAssociatedFile ik f = queueDb $ do
|
2016-01-11 19:52:11 +00:00
|
|
|
|
-- If the same file was associated with a different key before,
|
|
|
|
|
-- remove that.
|
|
|
|
|
delete $ from $ \r -> do
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
where_ (r ^. AssociatedFile ==. val af &&. not_ (r ^. AssociatedKey ==. val ik))
|
|
|
|
|
void $ insertUnique $ Associated ik af
|
|
|
|
|
where
|
|
|
|
|
af = toSFilePath (getTopFilePath f)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
|
|
|
|
|
{- Note that the files returned were once associated with the key, but
|
|
|
|
|
- some of them may not be any longer. -}
|
2016-01-12 18:01:50 +00:00
|
|
|
|
getAssociatedFiles :: IKey -> ReadHandle -> IO [TopFilePath]
|
|
|
|
|
getAssociatedFiles ik = readDb $ do
|
2016-01-11 19:52:11 +00:00
|
|
|
|
l <- select $ from $ \r -> do
|
2016-01-12 18:01:50 +00:00
|
|
|
|
where_ (r ^. AssociatedKey ==. val ik)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
return (r ^. AssociatedFile)
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
return $ map (asTopFilePath . fromSFilePath . unValue) l
|
2016-01-11 19:52:11 +00:00
|
|
|
|
|
|
|
|
|
{- Gets any keys that are on record as having a particular associated file.
|
|
|
|
|
- (Should be one or none but the database doesn't enforce that.) -}
|
2016-01-12 18:01:50 +00:00
|
|
|
|
getAssociatedKey :: TopFilePath -> ReadHandle -> IO [IKey]
|
2016-01-11 19:52:11 +00:00
|
|
|
|
getAssociatedKey f = readDb $ do
|
|
|
|
|
l <- select $ from $ \r -> do
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
where_ (r ^. AssociatedFile ==. val af)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
return (r ^. AssociatedKey)
|
|
|
|
|
return $ map unValue l
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
where
|
|
|
|
|
af = toSFilePath (getTopFilePath f)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
|
removeAssociatedFile :: IKey -> TopFilePath -> WriteHandle -> IO ()
|
|
|
|
|
removeAssociatedFile ik f = queueDb $
|
2016-01-11 19:52:11 +00:00
|
|
|
|
delete $ from $ \r -> do
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
|
where_ (r ^. AssociatedKey ==. val ik &&. r ^. AssociatedFile ==. val af)
|
|
|
|
|
where
|
|
|
|
|
af = toSFilePath (getTopFilePath f)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
|
addInodeCaches :: IKey -> [InodeCache] -> WriteHandle -> IO ()
|
|
|
|
|
addInodeCaches ik is = queueDb $
|
|
|
|
|
forM_ is $ \i -> insertUnique $ Content ik (toSInodeCache i)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
|
|
|
|
|
{- A key may have multiple InodeCaches; one for the annex object, and one
|
|
|
|
|
- for each pointer file that is a copy of it. -}
|
2016-01-12 18:01:50 +00:00
|
|
|
|
getInodeCaches :: IKey -> ReadHandle -> IO [InodeCache]
|
|
|
|
|
getInodeCaches ik = readDb $ do
|
2016-01-11 19:52:11 +00:00
|
|
|
|
l <- select $ from $ \r -> do
|
2016-01-12 18:01:50 +00:00
|
|
|
|
where_ (r ^. ContentKey ==. val ik)
|
2016-01-11 19:52:11 +00:00
|
|
|
|
return (r ^. ContentCache)
|
|
|
|
|
return $ map (fromSInodeCache . unValue) l
|
|
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
|
removeInodeCaches :: IKey -> WriteHandle -> IO ()
|
|
|
|
|
removeInodeCaches ik = queueDb $
|
2016-01-11 19:52:11 +00:00
|
|
|
|
delete $ from $ \r -> do
|
2016-01-12 18:01:50 +00:00
|
|
|
|
where_ (r ^. ContentKey ==. val ik)
|