eliminate raw sql LIKE query
This commit is contained in:
parent
09c7cbbaa8
commit
4940a135af
4 changed files with 67 additions and 43 deletions
|
@ -24,8 +24,6 @@ import Database.Persist.TH
|
|||
import Data.Time.Clock
|
||||
import Control.Monad
|
||||
import Data.Maybe
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Conduit.List as CL
|
||||
|
||||
-- Note on indexes: KeyFileIndex etc are really uniqueness constraints,
|
||||
-- which cause sqlite to automatically add indexes. So when adding indexes,
|
||||
|
@ -51,6 +49,8 @@ Associated
|
|||
Content
|
||||
key Key
|
||||
inodecache InodeCache
|
||||
filesize FileSize
|
||||
mtime EpochTime
|
||||
KeyInodeCacheIndex key inodecache
|
||||
InodeCacheKeyIndex inodecache key
|
||||
|]
|
||||
|
@ -123,7 +123,9 @@ removeAssociatedFile k f = queueDb $
|
|||
|
||||
addInodeCaches :: Key -> [InodeCache] -> WriteHandle -> IO ()
|
||||
addInodeCaches k is = queueDb $
|
||||
forM_ is $ \i -> insertUnique $ Content k i
|
||||
forM_ is $ \i -> insertUnique $ Content k i
|
||||
(inodeCacheToFileSize i)
|
||||
(inodeCacheToEpochTime i)
|
||||
|
||||
{- A key may have multiple InodeCaches; one for the annex object, and one
|
||||
- for each pointer file that is a copy of it. -}
|
||||
|
@ -136,28 +138,19 @@ removeInodeCaches :: Key -> WriteHandle -> IO ()
|
|||
removeInodeCaches k = queueDb $
|
||||
deleteWhere [ContentKey ==. k]
|
||||
|
||||
{- Check if the inode is known to be used for an annexed file.
|
||||
-
|
||||
- This is currently slow due to the lack of indexes.
|
||||
-}
|
||||
{- Check if the inode is known to be used for an annexed file. -}
|
||||
isInodeKnown :: InodeCache -> SentinalStatus -> ReadHandle -> IO Bool
|
||||
isInodeKnown i s = readDb query
|
||||
isInodeKnown i s = readDb (isJust <$> selectFirst q [])
|
||||
where
|
||||
query
|
||||
q
|
||||
| sentinalInodesChanged s =
|
||||
withRawQuery likesql [] $ isJust <$> CL.head
|
||||
| otherwise =
|
||||
isJust <$> selectFirst [ContentInodecache ==. i] []
|
||||
|
||||
likesql = T.concat
|
||||
[ "SELECT key FROM content WHERE "
|
||||
, T.intercalate " OR " $ map mklike (likeInodeCacheWeak i)
|
||||
, " LIMIT 1"
|
||||
]
|
||||
|
||||
mklike p = T.concat
|
||||
[ "cache LIKE "
|
||||
, "'"
|
||||
, T.pack p
|
||||
, "'"
|
||||
]
|
||||
-- Note that this select is intentionally not
|
||||
-- indexed. Normally, the inodes have not changed,
|
||||
-- and it would be unncessary work to maintain
|
||||
-- indexes for the unusual case.
|
||||
[ ContentFilesize ==. inodeCacheToFileSize i
|
||||
, ContentMtime >=. tmin
|
||||
, ContentMtime <=. tmax
|
||||
]
|
||||
| otherwise = [ContentInodecache ==. i]
|
||||
(tmin, tmax) = inodeCacheEpochTimeRange i
|
||||
|
|
|
@ -6,10 +6,13 @@
|
|||
-}
|
||||
|
||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||
{-# LANGUAGE TypeSynonymInstances #-}
|
||||
|
||||
module Database.Types (
|
||||
module Database.Types,
|
||||
Key,
|
||||
EpochTime,
|
||||
FileSize,
|
||||
) where
|
||||
|
||||
import Database.Persist.Class hiding (Key)
|
||||
|
@ -18,9 +21,14 @@ import qualified Data.ByteString as S
|
|||
import qualified Data.ByteString.Lazy as L
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Attoparsec.ByteString as A
|
||||
import System.PosixCompat.Types
|
||||
import Data.Int
|
||||
import Data.Text.Read
|
||||
import Foreign.C.Types
|
||||
|
||||
import Key
|
||||
import Utility.InodeCache
|
||||
import Utility.FileSize
|
||||
import Utility.FileSystemEncoding
|
||||
import Git.Types
|
||||
import Types.UUID
|
||||
|
@ -105,3 +113,26 @@ instance PersistField SSha where
|
|||
|
||||
instance PersistFieldSql SSha where
|
||||
sqlType _ = SqlString
|
||||
|
||||
-- A FileSize could be stored as an Int64, but some systems could
|
||||
-- conceivably have a larger filesize, and no math is ever done with them
|
||||
-- in sqlite, so store a string instead.
|
||||
instance PersistField FileSize where
|
||||
toPersistValue = toPersistValue . show
|
||||
fromPersistValue v = fromPersistValue v >>= parse
|
||||
where
|
||||
parse = either (Left . T.pack) (Right . fst) . decimal
|
||||
|
||||
instance PersistFieldSql FileSize where
|
||||
sqlType _ = SqlString
|
||||
|
||||
-- Store EpochTime as an Int64, to allow selecting values in a range.
|
||||
instance PersistField EpochTime where
|
||||
toPersistValue (CTime t) = toPersistValue (fromIntegral t :: Int64)
|
||||
fromPersistValue v = CTime . fromIntegral <$> go
|
||||
where
|
||||
go :: Either T.Text Int64
|
||||
go = fromPersistValue v
|
||||
|
||||
instance PersistFieldSql EpochTime where
|
||||
sqlType _ = SqlInt64
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue