summaryrefslogtreecommitdiff
path: root/Key.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2019-01-16 16:09:53 -0400
committerJoey Hess <joeyh@joeyh.name>2019-01-16 16:21:59 -0400
commit96aba8eff7597898f53bfbab5865ff30a927d355 (patch)
treeb4c7f6f3f9d5ad01c65eab0a16d8bfeaeb245dd1 /Key.hs
parent863ed51ae0eef57e1f70f339b2e232a726a8c131 (diff)
Revert "cache the serialization of a Key"
This reverts commit 4536c93bb2ecf114ab711beac33fa358facd6985. That broke Read/Show of a Key, and unfortunately Key is read in at least one place; the GitAnnexDistribution data type. It would be worth bringing this optimisation back, but it would need either a custom Read/Show instance that preserves back-compat, or wrapping Key in a data type that contains the serialization, or changing how GitAnnexDistribution is serialized. Also, the Eq instance would need to compare keys with and without a cached seralization the same.
Diffstat (limited to 'Key.hs')
-rw-r--r--Key.hs28
1 files changed, 8 insertions, 20 deletions
diff --git a/Key.hs b/Key.hs
index 00aea87352..593d674ccd 100644
--- a/Key.hs
+++ b/Key.hs
@@ -51,18 +51,14 @@ stubKey = Key
, keyMtime = Nothing
, keyChunkSize = Nothing
, keyChunkNum = Nothing
- , keySerialization = Nothing
}
-- Gets the parent of a chunk key.
nonChunkKey :: Key -> Key
-nonChunkKey k
- | keyChunkSize k == Nothing && keyChunkNum k == Nothing = k
- | otherwise = k
- { keyChunkSize = Nothing
- , keyChunkNum = Nothing
- , keySerialization = Nothing
- }
+nonChunkKey k = k
+ { keyChunkSize = Nothing
+ , keyChunkNum = Nothing
+ }
-- Where a chunk key is offset within its parent.
chunkKeyOffset :: Key -> Maybe Integer
@@ -98,13 +94,10 @@ buildKey k = byteString (formatKeyVariety (keyVariety k))
_ ?: Nothing = mempty
serializeKey :: Key -> String
-serializeKey = decodeBS' . serializeKey'
+serializeKey = decodeBL' . serializeKey'
-serializeKey' :: Key -> S.ByteString
-serializeKey' k = case keySerialization k of
- Nothing -> L.toStrict $
- toLazyByteStringWith (safeStrategy 128 smallChunkSize) L.empty (buildKey k)
- Just b -> b
+serializeKey' :: Key -> L.ByteString
+serializeKey' = toLazyByteStringWith (safeStrategy 128 smallChunkSize) L.empty . buildKey
{- This is a strict parser for security reasons; a key
- can contain only 4 fields, which all consist only of numbers.
@@ -134,7 +127,6 @@ keyParser = do
, keyMtime = m
, keyChunkSize = cs
, keyChunkNum = cn
- , keySerialization = Nothing
}
else fail "invalid keyName"
where
@@ -148,10 +140,7 @@ deserializeKey :: String -> Maybe Key
deserializeKey = deserializeKey' . encodeBS'
deserializeKey' :: S.ByteString -> Maybe Key
-deserializeKey' b = either
- (const Nothing)
- (\k -> Just $ k { keySerialization = Just b })
- (A.parseOnly keyParser b)
+deserializeKey' b = eitherToMaybe $ A.parseOnly keyParser b
{- This splits any extension out of the keyName, returning the
- keyName minus extension, and the extension (including leading dot).
@@ -189,7 +178,6 @@ instance Arbitrary Key where
<*> ((abs . fromInteger <$>) <$> arbitrary) -- mtime cannot be negative
<*> ((abs <$>) <$> arbitrary) -- chunksize cannot be negative
<*> ((succ . abs <$>) <$> arbitrary) -- chunknum cannot be 0 or negative
- <*> pure Nothing
instance Hashable Key where
hashIO32 = hashIO32 . serializeKey'