2014-07-24 20:23:36 +00:00
|
|
|
{- Chunk logs, pure operations.
|
|
|
|
-
|
2019-01-09 17:06:37 +00:00
|
|
|
- Copyright 2014, 2019 Joey Hess <id@joeyh.name>
|
2014-07-24 20:23:36 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-07-24 21:18:14 +00:00
|
|
|
module Logs.Chunk.Pure
|
2014-07-28 17:19:08 +00:00
|
|
|
( ChunkMethod(..)
|
|
|
|
, ChunkSize
|
2014-07-24 21:18:14 +00:00
|
|
|
, ChunkCount
|
|
|
|
, ChunkLog
|
|
|
|
, parseLog
|
2019-01-09 17:06:37 +00:00
|
|
|
, buildLog
|
2014-07-24 21:18:14 +00:00
|
|
|
) where
|
2014-07-24 20:23:36 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-07-24 20:23:36 +00:00
|
|
|
import Logs.MapLog
|
|
|
|
import Data.Int
|
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
import qualified Data.ByteString as S
|
2019-01-10 17:23:42 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2019-01-09 17:06:37 +00:00
|
|
|
import Data.ByteString.Builder
|
|
|
|
|
2014-07-28 17:19:08 +00:00
|
|
|
-- Currently chunks are all fixed size, but other chunking methods
|
|
|
|
-- may be added.
|
2019-01-09 17:06:37 +00:00
|
|
|
data ChunkMethod = FixedSizeChunks ChunkSize | UnknownChunks S.ByteString
|
2014-08-01 20:30:23 +00:00
|
|
|
deriving (Ord, Eq, Show)
|
2014-07-28 17:19:08 +00:00
|
|
|
|
2014-07-24 20:23:36 +00:00
|
|
|
type ChunkSize = Int64
|
|
|
|
|
2014-07-28 17:19:08 +00:00
|
|
|
-- 0 when chunks are no longer present
|
2014-07-24 20:23:36 +00:00
|
|
|
type ChunkCount = Integer
|
|
|
|
|
2014-07-28 17:19:08 +00:00
|
|
|
type ChunkLog = MapLog (UUID, ChunkMethod) ChunkCount
|
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildChunkMethod :: ChunkMethod -> Builder
|
|
|
|
buildChunkMethod (FixedSizeChunks sz) = int64Dec sz
|
|
|
|
buildChunkMethod (UnknownChunks s) = byteString s
|
2014-07-24 20:23:36 +00:00
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
chunkMethodParser :: A.Parser ChunkMethod
|
|
|
|
chunkMethodParser =
|
|
|
|
(FixedSizeChunks <$> A8.decimal) <|> (UnknownChunks <$> A.takeByteString)
|
2014-07-24 20:23:36 +00:00
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildLog :: ChunkLog -> Builder
|
|
|
|
buildLog = buildMapLog fieldbuilder valuebuilder
|
2014-07-24 20:23:36 +00:00
|
|
|
where
|
2019-01-09 18:00:35 +00:00
|
|
|
fieldbuilder (u, m) = buildUUID u <> sep <> buildChunkMethod m
|
2019-01-09 17:06:37 +00:00
|
|
|
valuebuilder = integerDec
|
|
|
|
sep = charUtf8 ':'
|
2019-01-10 17:23:42 +00:00
|
|
|
|
|
|
|
parseLog :: L.ByteString -> ChunkLog
|
|
|
|
parseLog = parseMapLog fieldparser valueparser
|
|
|
|
where
|
|
|
|
fieldparser = (,)
|
|
|
|
<$> (toUUID <$> A8.takeTill (== ':'))
|
|
|
|
<* A8.char ':'
|
|
|
|
<*> chunkMethodParser
|
|
|
|
<* A.endOfInput
|
|
|
|
valueparser = A8.decimal
|