项目作者: hankjacobs

项目描述 :
A delta-based CRDT map implementation in Go
高级语言: Go
项目地址: git://github.com/hankjacobs/vvmap.git
创建时间: 2018-10-08T03:21:49Z
项目社区:https://github.com/hankjacobs/vvmap

开源协议:MIT License

下载


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:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/hankjacobs/vvmap"
  6. )
  7. func main() {
  8. lexicographicConflictResolver := func(key string, left, right vvmap.Record) bool {
  9. leftVal := left.Value.(string)
  10. rightVal := right.Value.(string)
  11. return strings.Compare(leftVal, rightVal) > 0 // choose left if lexicographically greater
  12. }
  13. alice := vvmap.New("alice", lexicographicConflictResolver)
  14. bob := vvmap.New("bob", lexicographicConflictResolver)
  15. tim := vvmap.New("tim", lexicographicConflictResolver)
  16. // concurrently update everyone -- causes a conflict, should all resolve to "turkey" since
  17. // lexicographically greatest
  18. alice.Set("lunch", "turkey")
  19. bob.Set("lunch", "ham")
  20. tim.Set("lunch", "chicken")
  21. // get records that Bob has but Alice doesn't
  22. delta := bob.Delta(alice.Version())
  23. alice.Merge(delta)
  24. // get records that Tim has but Alice doesn't
  25. delta = tim.Delta(alice.Version())
  26. alice.Merge(delta)
  27. // sync bob
  28. bob.Merge(alice.Delta(bob.Version())) // alice is most up-to-date so no need to sync with Tim
  29. // sync tim
  30. tim.Merge(alice.Delta(tim.Version()))
  31. fmt.Println("alice:", alice.Get("lunch"))
  32. fmt.Println("bob:", bob.Get("lunch"))
  33. fmt.Println("tim:", tim.Get("lunch"))
  34. }