crdt

package
v0.1.44 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MergeModeLWW = iota
	MergeModeCustom
	MergeModeCustomLWW
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	KeysLike                       interface{}
	ValuesLike                     interface{}
	StoreImmutablePartsWith        mast.Persist
	NodeCache                      mast.NodeCache
	Marshal                        func(interface{}) ([]byte, error)
	Unmarshal                      func([]byte, interface{}) error
	UnmarshalerUsesRegisteredTypes bool
	CustomMerge                    func(key interface{}, v1, v2 crdt.Value) crdt.Value
	OnConflictMerged
	MastNodeFormat string
}

type MergeError

type MergeError error

type MergeFunc

type MergeFunc func(context.Context, *mast.Mast, bool, bool, interface{}, interface{}, interface{},
	OnConflictMerged) (bool, error)
var LWW MergeFunc = MergeFunc(
	func(ctx context.Context, newTree *mast.Mast,
		added, removed bool, key, addedValue, removedValue interface{},
		onConflictMerged OnConflictMerged) (bool, error) {
		var newValue crdt.Value
		if !added && !removed {
			av := addedValue.(crdt.Value)
			rv := removedValue.(crdt.Value)
			newValue = *crdt.LastWriteWins(&av, &rv)
			if onConflictMerged != nil && !av.Tombstoned() && !rv.Tombstoned() &&
				!reflect.DeepEqual(av.Value, rv.Value) {
				err := onConflictMerged(key, av.Value, rv.Value)
				if err != nil {
					return false, fmt.Errorf("OnConflictMerged: %w", err)
				}
			}
		} else if added {

			return true, nil
		} else if removed {
			newValue = removedValue.(crdt.Value)
		} else {
			return false, fmt.Errorf("no added/removed value")
		}
		err := newTree.Insert(ctx, key, newValue)
		if err != nil {
			return false, fmt.Errorf("insert: %w", err)
		}
		return true, nil
	})

func (MergeFunc) ToDiffFunc

func (mf MergeFunc) ToDiffFunc(ctx context.Context, m *mast.Mast, conflictCB OnConflictMerged) func(added, removed bool,
	key, addedValue, removedValue interface{},
) (bool, error)

type OnConflictMerged

type OnConflictMerged func(key, v1, v2 interface{}) error

type Root

type Root struct {
	mast.Root
	Created      *time.Time `json:"cr,omitempty"`
	MergeSources []string   `json:"p,omitempty"`
	MergeMode    int        `json:"mm,omitempty"`
	KVVersion    int        `json:"kv_version,omitempty"`
}

Root identifies a tree, a persisted form, that links the mast and ancestors.

func NewRoot

func NewRoot(when time.Time, branchFactor uint) Root

type Tree

type Tree struct {
	Config       Config
	Mast         *mast.Mast
	Created      *time.Time
	Source       *string
	MergeSources []string
	MergeMode    int
}

func Load

func Load(ctx context.Context, cfg Config, rootName *string, root Root) (*Tree, error)

func (Tree) Clone

func (c Tree) Clone(ctx context.Context) (*Tree, error)

func (*Tree) Get

func (c *Tree) Get(ctx context.Context, key interface{}, value interface{}) (bool, error)

func (Tree) IsDirty

func (c Tree) IsDirty() bool

func (*Tree) IsTombstoned

func (c *Tree) IsTombstoned(ctx context.Context, key interface{}) (bool, error)

func (*Tree) MakeRoot

func (c *Tree) MakeRoot(ctx context.Context) (*Root, error)

func (*Tree) Merge

func (c *Tree) Merge(ctx context.Context, other *Tree) error

func (*Tree) Set

func (c *Tree) Set(ctx context.Context, when time.Time, key, value interface{}) error

func (*Tree) Size

func (c *Tree) Size() uint64

func (*Tree) Tombstone

func (c *Tree) Tombstone(ctx context.Context, when time.Time, key interface{}) error

Jump to

Keyboard shortcuts

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