Skip to content

Commit

Permalink
diffstore2: AnyLeaf initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcluseau committed Jan 17, 2022
1 parent 52d8664 commit c0d094c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 34 deletions.
46 changes: 46 additions & 0 deletions client/diffstore2/any-leaf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package diffstore2

import (
"constraints"
"fmt"
)

func NewAnyStore[K constraints.Ordered, T any](equal func(a,b T) bool) *Store[K, *AnyLeaf[T]] {
return New[K](func() *AnyLeaf[T] { return NewAnyLeaf(equal) })
}

type AnyLeaf[T any] struct {
equal func(a,b T) bool
value T

hash uint64
}

func NewAnyLeaf[T any](equal func(a,b T) bool) *AnyLeaf[T] {
return &AnyLeaf[T]{equal: equal, hash: 1}
}

var _ Leaf = NewAnyLeaf(func(a,b string) bool { return a == b })

func (l *AnyLeaf[T]) Reset() {
}

func (l *AnyLeaf[T]) Hash() uint64 {
return l.hash
}

func (l *AnyLeaf[T]) Get() T {
return l.value
}

func (l *AnyLeaf[T]) Set(v T) {
if !l.equal(l.value, v) {
l.hash++
}

l.value = v
}

func (l *AnyLeaf[T]) String() string {
return fmt.Sprintf("{%v}", l.value)
}
33 changes: 33 additions & 0 deletions client/diffstore2/any-leaf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package diffstore2

func ExampleAnyLeafStore() {
store := NewAnyStore[string](func(a, b string) bool { return a == b })

store.Get("a").Set("a1")
store.Done()
store.printDiff()

store.Reset()
store.Get("a").Set("a1")
store.Done()
store.printDiff()

store.Reset()
store.Get("a").Set("a2")
store.Done()
store.printDiff()

store.Reset()
store.Done()
store.printDiff()

// Output:
// -----
// C a => "{a1}"
// -----
// <same>
// -----
// U a => "{a2}"
// -----
// D a
}
5 changes: 3 additions & 2 deletions client/diffstore2/buffer-leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package diffstore2

import (
"bytes"
"constraints"

"github.com/cespare/xxhash"
)

func NewBufferStore() *Store[string, *BufferLeaf] {
return New[string](NewBufferLeaf)
func NewBufferStore[K constraints.Ordered]() *Store[K, *BufferLeaf] {
return New[K](NewBufferLeaf)
}

type BufferLeaf struct {
Expand Down
39 changes: 7 additions & 32 deletions client/diffstore2/diffstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,13 @@ import (
)

func ExampleStore() {
store := NewBufferStore()

printDiff := func() {
fmt.Println("-----")
hasChanges := false

for _, i := range store.Changed() {
hasChanges = true

s := "U"
if i.Created() {
s = "C"
}
fmt.Printf("%s %s => %q\n", s, i.Key(), i.Value())
}

for _, i := range store.Deleted() {
hasChanges = true

fmt.Printf("D %s\n", i.Key())
}

if !hasChanges {
fmt.Println("<same>")
}
}
store := NewBufferStore[string]()

{
fmt.Fprint(store.Get("a"), "hello a")

store.Done()
printDiff()
store.printDiff()
}

{
Expand All @@ -46,7 +21,7 @@ func ExampleStore() {
fmt.Fprint(store.Get("a"), "hello a")

store.Done()
printDiff()
store.printDiff()
}

{
Expand All @@ -56,7 +31,7 @@ func ExampleStore() {
fmt.Fprint(store.Get("b"), "hello b")

store.Done()
printDiff()
store.printDiff()
}

{
Expand All @@ -65,7 +40,7 @@ func ExampleStore() {
fmt.Fprint(store.Get("a"), "hi a")

store.Done()
printDiff()
store.printDiff()
}

{
Expand All @@ -74,14 +49,14 @@ func ExampleStore() {
fmt.Fprint(store.Get("b"), "hi b")

store.Done()
printDiff()
store.printDiff()
}

{
store.Reset()

store.Done()
printDiff()
store.printDiff()
}

// Output:
Expand Down
30 changes: 30 additions & 0 deletions client/diffstore2/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package diffstore2

import (
"fmt"
)

func (store *Store[K,V]) printDiff() {
fmt.Println("-----")
hasChanges := false

for _, i := range store.Changed() {
hasChanges = true

s := "U"
if i.Created() {
s = "C"
}
fmt.Printf("%s %v => %q\n", s, i.Key(), fmt.Sprint(i.Value()))
}

for _, i := range store.Deleted() {
hasChanges = true

fmt.Printf("D %v\n", i.Key())
}

if !hasChanges {
fmt.Println("<same>")
}
}

0 comments on commit c0d094c

Please sign in to comment.