mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-01 20:00:22 -05:00
Settle big/small vs to/from ONCE AND FOR ALL
This commit is contained in:
@@ -1 +0,0 @@
|
||||
differ
|
||||
@@ -1,57 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
"github.com/attic-labs/noms/walk"
|
||||
)
|
||||
|
||||
var (
|
||||
customUsage = func() {
|
||||
fmtString := `%s emits hashes reachable from root <big> that cannot be reached from root <small>.`
|
||||
fmt.Fprintf(os.Stderr, fmtString, os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "\n\nUsage: %s [options] <small-root> <big-root>\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
flags := chunks.NewFlags()
|
||||
flag.Usage = customUsage
|
||||
flag.Parse()
|
||||
|
||||
cs := flags.CreateStore()
|
||||
small := flag.Arg(0)
|
||||
big := flag.Arg(1)
|
||||
if cs == nil || small == "" || big == "" {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
smallRef, err := ref.Parse(small)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not parse small root hash:", err)
|
||||
}
|
||||
smallValue, err := types.ReadValue(smallRef, cs)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not read value for small root hash:", err)
|
||||
}
|
||||
|
||||
bigRef, err := ref.Parse(big)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not parse big root hash:", err)
|
||||
}
|
||||
bigValue, err := types.ReadValue(bigRef, cs)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not read value for big root hash:", err)
|
||||
}
|
||||
|
||||
refs := walk.Diff(smallValue, bigValue)
|
||||
fmt.Println(refs)
|
||||
}
|
||||
+7
-7
@@ -5,14 +5,14 @@ import (
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
// Diff returns the hashes of the chunks reachable from 'to' that cannot be reached from 'from'
|
||||
func Diff(from, to types.Value) (hashes []ref.Ref) {
|
||||
fromRefs := map[ref.Ref]bool{}
|
||||
WalkAll(from, func(v types.Value) {
|
||||
fromRefs[v.Ref()] = true
|
||||
// GetReachabilitySetDiff returns the hashes of the chunks reachable from 'big' that cannot be reached from 'small'
|
||||
func GetReachabilitySetDiff(small, big types.Value) (hashes []ref.Ref) {
|
||||
smallRefs := map[ref.Ref]bool{}
|
||||
WalkAll(small, func(v types.Value) {
|
||||
smallRefs[v.Ref()] = true
|
||||
})
|
||||
WalkAll(to, func(v types.Value) {
|
||||
if !fromRefs[v.Ref()] {
|
||||
WalkAll(big, func(v types.Value) {
|
||||
if !smallRefs[v.Ref()] {
|
||||
hashes = append(hashes, v.Ref())
|
||||
}
|
||||
})
|
||||
|
||||
+5
-7
@@ -7,16 +7,14 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDiff(t *testing.T) {
|
||||
func TestGetReachabilitySetDiff(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// {"string": "string",
|
||||
// "list": [false true],
|
||||
// "map": {"nested": "string"}
|
||||
// "mtlist": []
|
||||
// "set": [5 7 8]
|
||||
// }
|
||||
from := types.NewMap(
|
||||
small := types.NewMap(
|
||||
types.NewString("string"), types.NewString("string"),
|
||||
types.NewString("map"), types.NewMap(types.NewString("nested"), types.NewString("string")),
|
||||
types.NewString("mtlist"), types.NewList())
|
||||
@@ -24,10 +22,10 @@ func TestDiff(t *testing.T) {
|
||||
setKey := types.NewString("set")
|
||||
setElem := types.Int32(7)
|
||||
setVal := types.NewSet(setElem)
|
||||
to := from.Set(setKey, setVal)
|
||||
big := small.Set(setKey, setVal)
|
||||
|
||||
var hashes []string
|
||||
for _, r := range Diff(from, to) {
|
||||
for _, r := range GetReachabilitySetDiff(small, big) {
|
||||
hashes = append(hashes, r.String())
|
||||
}
|
||||
|
||||
@@ -35,5 +33,5 @@ func TestDiff(t *testing.T) {
|
||||
assert.Contains(hashes, setElem.Ref().String())
|
||||
assert.Contains(hashes, setVal.Ref().String())
|
||||
|
||||
assert.Empty(Diff(from, from))
|
||||
assert.Empty(GetReachabilitySetDiff(small, small))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user