crdt

package
v0.0.0-...-a9a9d8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CrdtEngine

type CrdtEngine[T any] interface {
	Add(string, T) error
	AddWithTime(string, T, time.Time) error
	AddedAt(string) (time.Time, bool)
	Each(func(string, T, time.Time) error) error
	Size() int
	Get(string) (T, bool)
}

type Element

type Element[T any] struct {
	Value     T
	Timestamp time.Time
}

type LWWSet

type LWWSet[T any] struct {
	Additions CrdtEngine[T] `json:"additions"`
	Removals  CrdtEngine[T] `json:"removals"`
}

LWWSet is a Last-Writer-Wins Set implementation

func (*LWWSet[T]) Add

func (s *LWWSet[T]) Add(key string, value T) error

Add marks an element to be added at a given timestamp

func (*LWWSet[T]) Exists

func (s *LWWSet[T]) Exists(key string) bool

Exists checks if an element is marked as present in the set

func (*LWWSet[T]) Get

func (s *LWWSet[T]) Get(key string) (T, bool)

func (*LWWSet[T]) GetAdditions

func (s *LWWSet[T]) GetAdditions() CrdtEngine[T]

func (*LWWSet[T]) GetAll

func (s *LWWSet[T]) GetAll() (map[string]T, error)

Get returns set content

func (*LWWSet[T]) GetRemovals

func (s *LWWSet[T]) GetRemovals() CrdtEngine[T]

func (*LWWSet[T]) Merge

func (s *LWWSet[T]) Merge(other LastWriterWinsSet[T]) error

Merge additions and removals from other LWWSet into current set

func (*LWWSet[T]) Remove

func (s *LWWSet[T]) Remove(key string, value T) error

Remove marks an element to be removed at a given timestamp

type LastWriterWinsSet

type LastWriterWinsSet[T any] interface {
	Add(string, T) error
	Remove(string, T) error
	Exists(string) bool
	Get(string) (T, bool)
	GetAll() (map[string]T, error)
	Merge(LastWriterWinsSet[T]) error
	GetAdditions() CrdtEngine[T]
	GetRemovals() CrdtEngine[T]
}

func NewLWWSet

func NewLWWSet[T comparable](addition, removal CrdtEngine[T]) LastWriterWinsSet[T]

NewLWWSet returns an implementation of a LastWriterWinsSet

type TimeMap

type TimeMap[T any] struct {
	Storage map[string]Element[T] `json:"elements"`
	// contains filtered or unexported fields
}

TimeMap is an implementation of a timeSet that uses a map data structure. We map items to timestamps.

func NewTimeSet

func NewTimeSet[T comparable]() *TimeMap[T]

newTimeSet returns an empty map-backed implementation of the CrdtEngine interface

func (*TimeMap[T]) Add

func (s *TimeMap[T]) Add(key string, value T) error

Add an element in the set if one of the following condition is met: - Given element does not exist yet - Given element already exists but with a lesser timestamp than the given one

func (*TimeMap[T]) AddWithTime

func (s *TimeMap[T]) AddWithTime(key string, value T, t time.Time) error

Add an element in the set if one of the following condition is met: - Given element does not exist yet - Given element already exists but with a lesser timestamp than the given one

func (*TimeMap[T]) AddedAt

func (s *TimeMap[T]) AddedAt(key string) (time.Time, bool)

AddedAt returns the timestamp of a given element if it exists

The second return value (bool) indicates whether the element exists or not If the given element does not exist, the second return (bool) is false

func (*TimeMap[T]) Each

func (s *TimeMap[T]) Each(f func(key string, val T, addedAt time.Time) error) error

Each traverses the items in the Set, calling the provided function for each element key/value/timestamp association

func (*TimeMap[T]) Get

func (s *TimeMap[T]) Get(key string) (T, bool)

func (*TimeMap[T]) Size

func (s *TimeMap[T]) Size() int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL