Skip to content

Commit

Permalink
Merge pull request #23 from rickowens/pr
Browse files Browse the repository at this point in the history
feat: Add `diffSize`.
  • Loading branch information
rickowens authored Feb 18, 2024
2 parents d7b65a5 + 5cae050 commit 703cd6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/cabal.project.local
/.tags.lock
/tags
*.full-imports
2 changes: 1 addition & 1 deletion crdt-event-fold.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: crdt-event-fold
version: 1.8.0.2
version: 1.8.1.0
synopsis: Garbage collected event folding CRDT.
description: Garbage collected event folding CRDT. Consistently
apply arbitrary operations to replicated data.
Expand Down
25 changes: 18 additions & 7 deletions src/Data/CRDT/EventFold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ module Data.CRDT.EventFold (
fullMerge_,
UpdateResult(..),
events,
diffSize,
diffMerge,
diffMerge_,
MergeError(..),
Expand Down Expand Up @@ -184,8 +185,8 @@ import GHC.Generics (Generic)
import Prelude (Applicative(pure), Bool(False, True), Either(Left, Right),
Enum(succ), Eq((/=), (==)), Foldable(foldr, maximum), Functor(fmap),
Maybe(Just, Nothing), Monoid(mempty), Ord((<), (<=), compare, max),
Semigroup((<>)), ($), (.), (<$>), (||), Num, Show, const, fst, id,
not, otherwise, snd)
Semigroup((<>)), ($), (.), (<$>), (||), Int, Num, Show, const, fst,
id, not, otherwise, snd)
import Type.Reflection (Typeable)
import qualified Data.DoubleWord as DW
import qualified Data.Map as Map
Expand Down Expand Up @@ -590,11 +591,11 @@ events peer ef =


{- | A package containing events that can be merged into an event fold. -}
data Diff o p e = Diff {
diffEvents :: Map (EventId p) (Maybe (Delta p e), Set p),
diffOrigin :: o,
diffInfimum :: EventId p,
diffUnjoins :: Set (EventId p)
data Diff o p e = Diff
{ diffEvents :: Map (EventId p) (Maybe (Delta p e), Set p)
, diffOrigin :: o
, diffInfimum :: EventId p
, diffUnjoins :: Set (EventId p)
}
deriving stock (Generic)
deriving stock instance (Eq o, Eq p, Eq e, Eq (Output e)) => Eq (Diff o p e)
Expand All @@ -610,6 +611,16 @@ instance (
Binary (Diff o p e)


{-|
Return the number of events contained in the diff. This information
might be useful for optimizing performance by, for instance, choosing to
use `diffMerge` instead of `fullMerge` when the diff is small or zero.
-}
diffSize :: Diff o p e -> Int
diffSize Diff { diffEvents } =
Map.size diffEvents


{- |
Like 'fullMerge', but merge a remote 'Diff' instead of a full remote
'EventFold'.
Expand Down

0 comments on commit 703cd6c

Please sign in to comment.