From 92678501caeaf89a709bd7545d702cccfa992039 Mon Sep 17 00:00:00 2001 From: Andy Arthur Date: Tue, 13 Oct 2020 17:00:31 -0700 Subject: [PATCH] refactored cell-wise diff to use row.TaggedValues --- go/libraries/doltcore/diff/diff_summary.go | 4 +- go/libraries/doltcore/row/tagged_values.go | 27 ++++++++++++ go/store/types/tuple.go | 51 ---------------------- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/go/libraries/doltcore/diff/diff_summary.go b/go/libraries/doltcore/diff/diff_summary.go index b66609153a..d8efa16c9f 100644 --- a/go/libraries/doltcore/diff/diff_summary.go +++ b/go/libraries/doltcore/diff/diff_summary.go @@ -19,6 +19,8 @@ import ( "errors" "time" + "github.com/dolthub/dolt/go/libraries/doltcore/row" + "github.com/dolthub/dolt/go/store/diff" "github.com/dolthub/dolt/go/store/types" ) @@ -64,7 +66,7 @@ func reportChanges(change *diff.Difference, ch chan<- DiffSummaryProgress) error case types.DiffChangeModified: oldTuple := change.OldValue.(types.Tuple) newTuple := change.NewValue.(types.Tuple) - cellChanges, err := oldTuple.CountDifferencesBetweenTupleFields(newTuple) + cellChanges, err := row.CountCellDiffs(oldTuple, newTuple) if err != nil { return err } diff --git a/go/libraries/doltcore/row/tagged_values.go b/go/libraries/doltcore/row/tagged_values.go index e84c7251bf..ad58f8074a 100644 --- a/go/libraries/doltcore/row/tagged_values.go +++ b/go/libraries/doltcore/row/tagged_values.go @@ -199,3 +199,30 @@ func (tt TaggedValues) String() string { str += "\n}" return str } + +// CountCellDiffs returns the number of fields that are different between two +// tuples and does not panic if tuples are different lengths. +func CountCellDiffs(from, to types.Tuple) (uint64, error) { + changed := 0 + f, err := ParseTaggedValues(from) + t, err := ParseTaggedValues(to) + + if err != nil { + return 0, err + } + + for i, v := range f { + ov, ok := t[i] + if !ok || !v.Equals(ov) { + changed++ + } + } + + for i := range t { + if f[i] == nil { + changed++ + } + } + + return uint64(changed), nil +} diff --git a/go/store/types/tuple.go b/go/store/types/tuple.go index 4fac25ed27..570035d18f 100644 --- a/go/store/types/tuple.go +++ b/go/store/types/tuple.go @@ -542,57 +542,6 @@ func (t Tuple) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { return TupleKind < other.Kind(), nil } -// CountDifferencesBetweenTupleFields returns the number of fields that are different between two -// tuples and does not panic if tuples are different lengths. -func (t Tuple) CountDifferencesBetweenTupleFields(other Tuple) (uint64, error) { - changed := 0 - tMap, err := t.fieldsToMap() - otherMap, err := other.fieldsToMap() - - if err != nil { - return 0, err - } - - for i, v := range tMap { - ov, ok := otherMap[i] - if !ok || !v.Equals(ov) { - changed++ - } - } - - for i := range otherMap { - if tMap[i] == nil { - changed++ - } - } - - return uint64(changed), nil -} - -func (t Tuple) fieldsToMap() (map[Value]Value, error) { - valMap := make(map[Value]Value) - - err := t.IterFields(func(index uint64, val Value) (stop bool, err error) { - if index%2 == 0 { - value, err := t.Get(index + 1) - if err != nil { - return true, err - } - if value.Kind() != NullKind { - valMap[val] = value - } - } - - return false, nil - }) - - if err != nil { - return nil, err - } - - return valMap, nil -} - func (t Tuple) StartsWith(otherTuple Tuple) bool { tplDec, _ := t.decoderSkipToFields() otherDec, _ := otherTuple.decoderSkipToFields()