File: UUIDBased.hs

package info (click to toggle)
git-annex 10.20250416-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 73,572 kB
  • sloc: haskell: 90,656; javascript: 9,103; sh: 1,469; makefile: 211; perl: 137; ansic: 44
file content (94 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{- git-annex uuid-based logs
 -
 - This is used to store information about UUIDs in a way that can
 - be union merged.
 -
 - The old format looks like: "UUID[ INFO[ timestamp=foo]]"
 - The timestamp is last for backwards compatibility reasons,
 - and may not be present on very old log lines.
 -
 - New uuid based logs instead use the form: "timestamp UUID INFO"
 - 
 - Copyright 2011-2023 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

{-# LANGUAGE OverloadedStrings, TupleSections #-}

module Logs.UUIDBased (
	Log,
	LogEntry(..),
	VectorClock,
	currentVectorClock,
	parseLogOld,
	parseLogNew,
	parseLogOldWithUUID,
	buildLogOld,
	buildLogNew,
	changeLog,
	addLog,
	simpleMap,
) where

import qualified Data.Map as M

import Common
import Types.UUID
import Annex.VectorClock
import Logs.MapLog

import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.ByteString.Char8 as A8
import Data.ByteString.Builder
import qualified Data.DList as D

type Log v = MapLog UUID v

buildLogOld :: (v -> Builder) -> Log v -> Builder
buildLogOld builder = mconcat . map genline . M.toList . fromMapLog
  where
	genline (u, LogEntry c@(VectorClock {}) v) =
		buildUUID u <> sp <> builder v <> sp
			<> byteString "timestamp="
			<> buildVectorClock c
			<> nl
	genline (u, LogEntry Unknown v) =
		buildUUID u <> sp <> builder v <> nl
	sp = charUtf8 ' '
	nl = charUtf8 '\n'

parseLogOld :: A.Parser a -> L.ByteString -> Log a
parseLogOld = parseLogOldWithUUID . const

parseLogOldWithUUID :: (UUID -> A.Parser a) -> L.ByteString -> Log a
parseLogOldWithUUID parser = parseMapLogWith (logParserOld parser)

logParserOld :: (UUID -> A.Parser a) -> A.Parser (Log a)
logParserOld parser = mapLogParser' $ do
	u <- toUUID <$> A8.takeWhile1 (/= ' ')
	(dl, ts) <- accumval D.empty
	v <- either fail return $ A.parseOnly (parser u <* A.endOfInput)
		(S.intercalate " " $ D.toList dl)
	return (u, LogEntry ts v)
  where
	accumval dl =
		((dl,) <$> parsetimestamp)
		<|> (A8.char ' ' *> (A8.takeWhile (/= ' ')) >>= accumval . D.snoc dl)
	parsetimestamp = 
		(A8.string " timestamp=" *> vectorClockParser <* A.endOfInput)
		<|> (const Unknown <$> A.endOfInput)

buildLogNew :: (v -> Builder) -> Log v -> Builder
buildLogNew = buildMapLog buildUUID

parseLogNew :: A.Parser v -> L.ByteString -> Log v
parseLogNew = parseMapLog (toUUID <$> A.takeByteString)

changeLog :: CandidateVectorClock -> UUID -> v -> Log v -> Log v
changeLog = changeMapLog

addLog :: UUID -> LogEntry v -> Log v -> Log v
addLog = addMapLog