summaryrefslogtreecommitdiff
path: root/Types
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-10-08 16:55:11 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-10-08 16:55:11 -0400
commitd5494842274030d21356c7492e6de5969173c34d (patch)
treea1c25eacba4e6b98b0b1bf275a89ecc7ea0e20d2 /Types
parent55fb90edfc8732b08bea9239a6f4a471ac7867c3 (diff)
add VerifiedCopy data type
There should be no behavior changes in this commit, it just adds a more expressive data type and adjusts code that had been passing around a [UUID] or sometimes a Maybe Remote to instead use [VerifiedCopy]. Although, since some functions were taking two different [UUID] lists, there's some potential for me to have gotten it horribly wrong.
Diffstat (limited to 'Types')
-rw-r--r--Types/NumCopies.hs42
-rw-r--r--Types/UUID.hs11
2 files changed, 49 insertions, 4 deletions
diff --git a/Types/NumCopies.hs b/Types/NumCopies.hs
index d8ea31e69..732c928d2 100644
--- a/Types/NumCopies.hs
+++ b/Types/NumCopies.hs
@@ -1,4 +1,4 @@
-{- git-annex numcopies type
+{- git-annex numcopies types
-
- Copyright 2014 Joey Hess <id@joeyh.name>
-
@@ -7,8 +7,48 @@
module Types.NumCopies where
+import Types.UUID
+
+import qualified Data.Map as M
+
newtype NumCopies = NumCopies Int
deriving (Ord, Eq)
fromNumCopies :: NumCopies -> Int
fromNumCopies (NumCopies n) = n
+
+data VerifiedCopy
+ {- Use when a repository cannot be accessed, but it's
+ - a trusted repository, which is presumably not going to
+ - lose a copy. This is the weakest level of verification. -}
+ = TrustedCopy UUID
+ {- Represents a recent verification that a copy of an
+ - object exists in a repository with the given UUID. -}
+ | VerifiedCopy UUID
+ {- The strongest proof of the existence of a copy.
+ - Until its associated action is called to unlock it,
+ - the copy is locked in the repository and is guaranteed
+ - not to be dropped by any git-annex process. -}
+ | VerifiedCopyLock UUID (IO ())
+
+instance ToUUID VerifiedCopy where
+ toUUID (VerifiedCopy u) = u
+ toUUID (VerifiedCopyLock u _) = u
+ toUUID (TrustedCopy u) = u
+
+instance Show VerifiedCopy where
+ show (TrustedCopy u) = "TrustedCopy " ++ show u
+ show (VerifiedCopy u) = "VerifiedCopy " ++ show u
+ show (VerifiedCopyLock u _) = "VerifiedCopyLock " ++ show u
+
+strongestVerifiedCopy :: VerifiedCopy -> VerifiedCopy -> VerifiedCopy
+strongestVerifiedCopy a@(VerifiedCopyLock _ _) _ = a
+strongestVerifiedCopy _ b@(VerifiedCopyLock _ _) = b
+strongestVerifiedCopy a@(VerifiedCopy _) _ = a
+strongestVerifiedCopy _ b@(VerifiedCopy _) = b
+strongestVerifiedCopy a@(TrustedCopy _) _ = a
+
+-- Retains stronger verifications over weaker for the same uuid.
+deDupVerifiedCopies :: [VerifiedCopy] -> [VerifiedCopy]
+deDupVerifiedCopies l = M.elems $
+ M.fromListWith strongestVerifiedCopy (zip (map toUUID l) l)
diff --git a/Types/UUID.hs b/Types/UUID.hs
index de7ddd65d..27d82b86c 100644
--- a/Types/UUID.hs
+++ b/Types/UUID.hs
@@ -5,6 +5,8 @@
- Licensed under the GNU GPL version 3 or higher.
-}
+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
+
module Types.UUID where
import qualified Data.Map as M
@@ -19,9 +21,12 @@ fromUUID :: UUID -> String
fromUUID (UUID u) = u
fromUUID NoUUID = ""
-toUUID :: String -> UUID
-toUUID [] = NoUUID
-toUUID s = UUID s
+class ToUUID a where
+ toUUID :: a -> UUID
+
+instance ToUUID String where
+ toUUID [] = NoUUID
+ toUUID s = UUID s
isUUID :: String -> Bool
isUUID = isJust . U.fromString