summaryrefslogtreecommitdiff
path: root/Logs/Chunk/Pure.hs
blob: af209d7e19e92c532c1c7bcce12b2ba0a58d38b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{- Chunk logs, pure operations.
 -
 - Copyright 2014 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Logs.Chunk.Pure 
	( ChunkMethod(..)
	, ChunkSize
	, ChunkCount
	, ChunkLog
	, parseLog
	, showLog
	) where

import Common.Annex
import Logs.MapLog
import Data.Int

-- Currently chunks are all fixed size, but other chunking methods
-- may be added.
data ChunkMethod = FixedSizeChunks ChunkSize | UnknownChunks String
	deriving (Ord, Eq, Show)

type ChunkSize = Int64

-- 0 when chunks are no longer present
type ChunkCount = Integer

type ChunkLog = MapLog (UUID, ChunkMethod) ChunkCount

parseChunkMethod :: String -> ChunkMethod
parseChunkMethod s = maybe (UnknownChunks s) FixedSizeChunks (readish s)

showChunkMethod :: ChunkMethod -> String
showChunkMethod (FixedSizeChunks sz) = show sz
showChunkMethod (UnknownChunks s) = s

parseLog :: String -> ChunkLog
parseLog = parseMapLog fieldparser valueparser
  where
	fieldparser s =
		let (u,m) = separate (== sep) s
		in Just (toUUID u, parseChunkMethod m)
	valueparser = readish

showLog :: ChunkLog -> String
showLog = showMapLog fieldshower valueshower
  where
	fieldshower (u, m) = fromUUID u ++ sep : showChunkMethod m
	valueshower = show

sep :: Char
sep = ':'