2015-02-16 19:08:29 +00:00
|
|
|
{- types for SQL databases
|
|
|
|
-
|
2019-02-20 20:59:10 +00:00
|
|
|
- Copyright 2015-2019 Joey Hess <id@joeyh.name>
|
2015-02-16 19:08:29 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-02-16 19:08:29 +00:00
|
|
|
-}
|
|
|
|
|
2019-02-20 20:59:10 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2019-10-30 19:16:03 +00:00
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
2015-02-16 19:08:29 +00:00
|
|
|
|
2019-10-29 16:28:01 +00:00
|
|
|
module Database.Types (
|
|
|
|
module Database.Types,
|
|
|
|
Key,
|
2019-10-30 19:16:03 +00:00
|
|
|
EpochTime,
|
|
|
|
FileSize,
|
2019-10-29 16:28:01 +00:00
|
|
|
) where
|
2015-02-16 19:08:29 +00:00
|
|
|
|
2019-02-20 20:59:10 +00:00
|
|
|
import Database.Persist.Class hiding (Key)
|
|
|
|
import Database.Persist.Sql hiding (Key)
|
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.Text as T
|
2019-10-29 16:28:01 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString as A
|
2019-10-30 19:16:03 +00:00
|
|
|
import System.PosixCompat.Types
|
|
|
|
import Data.Int
|
|
|
|
import Data.Text.Read
|
|
|
|
import Foreign.C.Types
|
2015-02-16 19:08:29 +00:00
|
|
|
|
2017-02-24 17:42:30 +00:00
|
|
|
import Key
|
2015-12-09 21:00:37 +00:00
|
|
|
import Utility.InodeCache
|
2019-10-30 19:16:03 +00:00
|
|
|
import Utility.FileSize
|
2020-04-06 21:14:49 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2019-10-29 16:28:01 +00:00
|
|
|
import Git.Types
|
2019-02-20 20:59:10 +00:00
|
|
|
import Types.UUID
|
2019-02-21 17:38:27 +00:00
|
|
|
import Types.Import
|
2015-02-16 19:08:29 +00:00
|
|
|
|
2019-10-29 16:28:01 +00:00
|
|
|
instance PersistField Key where
|
2019-12-06 19:17:54 +00:00
|
|
|
toPersistValue = toPersistValue . serializeKey'
|
2019-10-29 16:28:01 +00:00
|
|
|
fromPersistValue b = fromPersistValue b >>= parse
|
|
|
|
where
|
|
|
|
parse = either (Left . T.pack) Right . A.parseOnly keyParser
|
2017-09-18 17:57:25 +00:00
|
|
|
|
2019-10-29 16:28:01 +00:00
|
|
|
-- A key can contain arbitrarily encoded characters, so store in sqlite as a
|
|
|
|
-- blob to avoid encoding problems.
|
|
|
|
instance PersistFieldSql Key where
|
|
|
|
sqlType _ = SqlBlob
|
2017-09-18 17:57:25 +00:00
|
|
|
|
2019-10-29 16:28:01 +00:00
|
|
|
instance PersistField InodeCache where
|
|
|
|
toPersistValue = toPersistValue . showInodeCache
|
|
|
|
fromPersistValue b = fromPersistValue b >>= parse
|
|
|
|
where
|
|
|
|
parse s = maybe
|
|
|
|
(Left $ T.pack $ "bad serialized InodeCache "++ s)
|
|
|
|
Right
|
|
|
|
(readInodeCache s)
|
2017-09-18 17:57:25 +00:00
|
|
|
|
2019-10-29 16:28:01 +00:00
|
|
|
instance PersistFieldSql InodeCache where
|
|
|
|
sqlType _ = SqlString
|
2019-02-20 20:59:10 +00:00
|
|
|
|
|
|
|
instance PersistField UUID where
|
|
|
|
toPersistValue u = toPersistValue b
|
|
|
|
where
|
|
|
|
b :: S.ByteString
|
|
|
|
b = fromUUID u
|
|
|
|
fromPersistValue v = toUUID <$> go
|
|
|
|
where
|
|
|
|
go :: Either T.Text S.ByteString
|
|
|
|
go = fromPersistValue v
|
|
|
|
|
|
|
|
instance PersistFieldSql UUID where
|
|
|
|
sqlType _ = SqlBlob
|
|
|
|
|
|
|
|
instance PersistField ContentIdentifier where
|
|
|
|
toPersistValue (ContentIdentifier b) = toPersistValue b
|
|
|
|
fromPersistValue v = ContentIdentifier <$> go
|
|
|
|
where
|
|
|
|
go :: Either T.Text S.ByteString
|
|
|
|
go = fromPersistValue v
|
|
|
|
|
|
|
|
instance PersistFieldSql ContentIdentifier where
|
|
|
|
sqlType _ = SqlBlob
|
2019-10-29 16:28:01 +00:00
|
|
|
|
sqlite datbase for importfeed
importfeed: Use caching database to avoid needing to list urls on every
run, and avoid using too much memory.
Benchmarking in my podcasts repo, importfeed got 1.42 seconds faster,
and memory use dropped from 203000k to 59408k.
Database.ImportFeed is Database.ContentIdentifier with the serial number
filed off. There is a bit of code duplication I would like to avoid,
particularly recordAnnexBranchTree, and getAnnexBranchTree. But these use
the persistent sqlite tables, so despite the code being the same, they
cannot be factored out.
Since this database includes the contentidentifier metadata, it will be
slightly redundant if a sqlite database is ever added for metadata. I
did consider making such a generic database and using it for this. But,
that would then need importfeed to update both the url database and the
metadata database, which is twice as much work diffing the git-annex
branch trees. Or would entagle updating two databases in a complex way.
So instead it seems better to optimise the database that
importfeed needs, and if the metadata database is used by another command,
use a little more disk space and do a little bit of redundant work to
update it.
Sponsored-by: unqueued on Patreon
2023-10-23 20:12:26 +00:00
|
|
|
-- A serialized bytestring.
|
|
|
|
newtype SByteString = SByteString S.ByteString
|
2019-10-29 16:28:01 +00:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
sqlite datbase for importfeed
importfeed: Use caching database to avoid needing to list urls on every
run, and avoid using too much memory.
Benchmarking in my podcasts repo, importfeed got 1.42 seconds faster,
and memory use dropped from 203000k to 59408k.
Database.ImportFeed is Database.ContentIdentifier with the serial number
filed off. There is a bit of code duplication I would like to avoid,
particularly recordAnnexBranchTree, and getAnnexBranchTree. But these use
the persistent sqlite tables, so despite the code being the same, they
cannot be factored out.
Since this database includes the contentidentifier metadata, it will be
slightly redundant if a sqlite database is ever added for metadata. I
did consider making such a generic database and using it for this. But,
that would then need importfeed to update both the url database and the
metadata database, which is twice as much work diffing the git-annex
branch trees. Or would entagle updating two databases in a complex way.
So instead it seems better to optimise the database that
importfeed needs, and if the metadata database is used by another command,
use a little more disk space and do a little bit of redundant work to
update it.
Sponsored-by: unqueued on Patreon
2023-10-23 20:12:26 +00:00
|
|
|
instance PersistField SByteString where
|
|
|
|
toPersistValue (SByteString b) = toPersistValue b
|
|
|
|
fromPersistValue v = SByteString <$> fromPersistValue v
|
2019-10-29 16:28:01 +00:00
|
|
|
|
sqlite datbase for importfeed
importfeed: Use caching database to avoid needing to list urls on every
run, and avoid using too much memory.
Benchmarking in my podcasts repo, importfeed got 1.42 seconds faster,
and memory use dropped from 203000k to 59408k.
Database.ImportFeed is Database.ContentIdentifier with the serial number
filed off. There is a bit of code duplication I would like to avoid,
particularly recordAnnexBranchTree, and getAnnexBranchTree. But these use
the persistent sqlite tables, so despite the code being the same, they
cannot be factored out.
Since this database includes the contentidentifier metadata, it will be
slightly redundant if a sqlite database is ever added for metadata. I
did consider making such a generic database and using it for this. But,
that would then need importfeed to update both the url database and the
metadata database, which is twice as much work diffing the git-annex
branch trees. Or would entagle updating two databases in a complex way.
So instead it seems better to optimise the database that
importfeed needs, and if the metadata database is used by another command,
use a little more disk space and do a little bit of redundant work to
update it.
Sponsored-by: unqueued on Patreon
2023-10-23 20:12:26 +00:00
|
|
|
instance PersistFieldSql SByteString where
|
2019-10-29 16:28:01 +00:00
|
|
|
sqlType _ = SqlBlob
|
|
|
|
|
|
|
|
-- A serialized git Sha
|
|
|
|
newtype SSha = SSha String
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
toSSha :: Sha -> SSha
|
2021-08-11 00:45:02 +00:00
|
|
|
toSSha (Ref s) = SSha (decodeBS s)
|
2019-10-29 16:28:01 +00:00
|
|
|
|
|
|
|
fromSSha :: SSha -> Ref
|
2021-08-11 00:45:02 +00:00
|
|
|
fromSSha (SSha s) = Ref (encodeBS s)
|
2019-10-29 16:28:01 +00:00
|
|
|
|
2019-10-29 21:08:36 +00:00
|
|
|
instance PersistField SSha where
|
2019-10-29 16:28:01 +00:00
|
|
|
toPersistValue (SSha b) = toPersistValue b
|
|
|
|
fromPersistValue v = SSha <$> fromPersistValue v
|
|
|
|
|
|
|
|
instance PersistFieldSql SSha where
|
|
|
|
sqlType _ = SqlString
|
2019-10-30 19:16:03 +00:00
|
|
|
|
|
|
|
-- 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
|