Skip to content

hankjacobs/vvmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vvmap GoDoc

vvmap is a Go implementation of a delta-based CRDT map as written about in "∆-CRDTs: Making δ-CRDTs Delta-Based", "Dotted Version Vectors: Logical Clocks for Optimistic Replication", and Tyler McMullen's excellent talk "The Anatomy of a Distributed System".

Usage:

package main

import (
	"fmt"
	"strings"
 	"github.com/hankjacobs/vvmap"
)

func main() {
	lexicographicConflictResolver := func(key string, left, right vvmap.Record) bool {
		leftVal := left.Value.(string)
		rightVal := right.Value.(string)
		return strings.Compare(leftVal, rightVal) > 0 // choose left if lexicographically greater
	}

	alice := vvmap.New("alice", lexicographicConflictResolver)
	bob := vvmap.New("bob", lexicographicConflictResolver)
	tim := vvmap.New("tim", lexicographicConflictResolver)

	// concurrently update everyone -- causes a conflict, should all resolve to "turkey" since
	// lexicographically greatest
	alice.Set("lunch", "turkey")
	bob.Set("lunch", "ham")
	tim.Set("lunch", "chicken")

	// get records that Bob has but Alice doesn't
	delta := bob.Delta(alice.Version())
	alice.Merge(delta)

	// get records that Tim has but Alice doesn't
	delta = tim.Delta(alice.Version())
	alice.Merge(delta)

	// sync bob
	bob.Merge(alice.Delta(bob.Version())) // alice is most up-to-date so no need to sync with Tim

	// sync tim
	tim.Merge(alice.Delta(tim.Version()))

	fmt.Println("alice:", alice.Get("lunch"))
	fmt.Println("bob:", bob.Get("lunch"))
	fmt.Println("tim:", tim.Get("lunch"))
}