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
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2019-02-20 20:59:10 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2015-02-16 19:08:29 +00:00
|
|
|
|
|
|
|
module Database.Types where
|
|
|
|
|
|
|
|
import Database.Persist.TH
|
2019-02-20 20:59:10 +00:00
|
|
|
import Database.Persist.Class hiding (Key)
|
|
|
|
import Database.Persist.Sql hiding (Key)
|
2015-02-16 19:08:29 +00:00
|
|
|
import Data.Maybe
|
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
|
|
|
import Data.Char
|
2019-02-20 20:59:10 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.Text as T
|
2015-02-16 19:08:29 +00:00
|
|
|
|
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
|
|
|
import Utility.PartialPrelude
|
2017-02-24 17:42:30 +00:00
|
|
|
import Key
|
2015-12-09 21:00:37 +00:00
|
|
|
import Utility.InodeCache
|
2017-09-18 17:57:25 +00:00
|
|
|
import Git.Types (Ref(..))
|
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
|
|
|
|
|
|
|
-- A serialized Key
|
|
|
|
newtype SKey = SKey String
|
|
|
|
deriving (Show, Read)
|
|
|
|
|
|
|
|
toSKey :: Key -> SKey
|
2019-01-14 17:03:35 +00:00
|
|
|
toSKey = SKey . serializeKey
|
2015-02-16 19:08:29 +00:00
|
|
|
|
|
|
|
fromSKey :: SKey -> Key
|
2019-01-14 17:03:35 +00:00
|
|
|
fromSKey (SKey s) = fromMaybe (error $ "bad serialized Key " ++ s) (deserializeKey s)
|
2015-02-16 19:08:29 +00:00
|
|
|
|
|
|
|
derivePersistField "SKey"
|
2015-12-09 21:00:37 +00:00
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
-- A Key index. More efficient than SKey, but its Read instance does not
|
|
|
|
-- work when it's used in any kind of complex data structure.
|
|
|
|
newtype IKey = IKey String
|
|
|
|
|
|
|
|
instance Read IKey where
|
|
|
|
readsPrec _ s = [(IKey s, "")]
|
|
|
|
|
|
|
|
instance Show IKey where
|
|
|
|
show (IKey s) = s
|
|
|
|
|
|
|
|
toIKey :: Key -> IKey
|
2019-01-14 17:03:35 +00:00
|
|
|
toIKey = IKey . serializeKey
|
2016-01-12 18:01:50 +00:00
|
|
|
|
|
|
|
fromIKey :: IKey -> Key
|
2019-01-14 17:03:35 +00:00
|
|
|
fromIKey (IKey s) = fromMaybe (error $ "bad serialized Key " ++ s) (deserializeKey s)
|
2016-01-12 18:01:50 +00:00
|
|
|
|
|
|
|
derivePersistField "IKey"
|
|
|
|
|
2015-12-09 21:00:37 +00:00
|
|
|
-- A serialized InodeCache
|
|
|
|
newtype SInodeCache = I String
|
|
|
|
deriving (Show, Read)
|
|
|
|
|
|
|
|
toSInodeCache :: InodeCache -> SInodeCache
|
|
|
|
toSInodeCache = I . showInodeCache
|
|
|
|
|
|
|
|
fromSInodeCache :: SInodeCache -> InodeCache
|
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
|
|
|
fromSInodeCache (I s) = fromMaybe (error $ "bad serialized InodeCache " ++ s) (readInodeCache s)
|
2015-12-09 21:00:37 +00:00
|
|
|
|
|
|
|
derivePersistField "SInodeCache"
|
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
|
|
|
|
|
|
|
-- A serialized FilePath.
|
|
|
|
--
|
|
|
|
-- Not all unicode characters round-trip through sqlite. In particular,
|
|
|
|
-- surrigate code points do not. So, escape the FilePath. But, only when
|
|
|
|
-- it contains such characters.
|
|
|
|
newtype SFilePath = SFilePath String
|
|
|
|
|
|
|
|
-- Note that Read instance does not work when used in any kind of complex
|
|
|
|
-- data structure.
|
|
|
|
instance Read SFilePath where
|
|
|
|
readsPrec _ s = [(SFilePath s, "")]
|
|
|
|
|
|
|
|
instance Show SFilePath where
|
|
|
|
show (SFilePath s) = s
|
|
|
|
|
|
|
|
toSFilePath :: FilePath -> SFilePath
|
|
|
|
toSFilePath s@('"':_) = SFilePath (show s)
|
|
|
|
toSFilePath s
|
|
|
|
| any needsescape s = SFilePath (show s)
|
|
|
|
| otherwise = SFilePath s
|
|
|
|
where
|
|
|
|
needsescape c = case generalCategory c of
|
|
|
|
Surrogate -> True
|
|
|
|
PrivateUse -> True
|
|
|
|
NotAssigned -> True
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
fromSFilePath :: SFilePath -> FilePath
|
|
|
|
fromSFilePath (SFilePath s@('"':_)) =
|
|
|
|
fromMaybe (error "bad serialized SFilePath " ++ s) (readish s)
|
|
|
|
fromSFilePath (SFilePath s) = s
|
|
|
|
|
|
|
|
derivePersistField "SFilePath"
|
|
|
|
|
2017-09-18 17:57:25 +00:00
|
|
|
-- A serialized Ref
|
|
|
|
newtype SRef = SRef Ref
|
|
|
|
|
|
|
|
-- Note that Read instance does not work when used in any kind of complex
|
|
|
|
-- data structure.
|
|
|
|
instance Read SRef where
|
|
|
|
readsPrec _ s = [(SRef (Ref s), "")]
|
|
|
|
|
|
|
|
instance Show SRef where
|
|
|
|
show (SRef (Ref s)) = s
|
|
|
|
|
|
|
|
derivePersistField "SRef"
|
|
|
|
|
|
|
|
toSRef :: Ref -> SRef
|
|
|
|
toSRef = SRef
|
|
|
|
|
|
|
|
fromSRef :: SRef -> Ref
|
|
|
|
fromSRef (SRef r) = r
|
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
|