removed ItemSearchFn param from ApplyMutations

This commit is contained in:
Andy Arthur
2022-04-27 13:29:06 -07:00
parent 5c8ac559ff
commit 7b3b5b2c9c
4 changed files with 34 additions and 39 deletions

View File

@@ -28,8 +28,7 @@ type orderedMap[K, V ~[]byte, O ordering[K]] struct {
}
func (m orderedMap[K, V, O]) makeTree(ctx context.Context) (orderedTree[K, V, O], error) {
sfn, cfn := m.tree.searchNode, m.tree.compareItems
root, err := tree.ApplyMutations(ctx, m.tree.ns, m.tree.root, m.mutations(), sfn, cfn)
root, err := tree.ApplyMutations(ctx, m.tree.ns, m.tree.root, m.mutations(), m.tree.compareItems)
if err != nil {
return orderedTree[K, V, O]{}, err
}

View File

@@ -70,7 +70,7 @@ func ThreeWayMerge(
// consume |patches| and apply them to |left|
eg.Go(func() error {
final, err = ApplyMutations(ctx, ns, left, patches, search, compare)
final, err = ApplyMutations(ctx, ns, left, patches, compare)
return err
})

View File

@@ -24,20 +24,13 @@ type MutationIter interface {
Close() error
}
func ApplyMutations(
ctx context.Context,
ns NodeStore,
root Node,
edits MutationIter,
search ItemSearchFn,
compare CompareFn,
) (Node, error) {
func ApplyMutations(ctx context.Context, ns NodeStore, root Node, edits MutationIter, compare CompareFn) (Node, error) {
newKey, newValue := edits.NextMutation(ctx)
if newKey == nil {
return root, nil // no mutations
}
cur, err := NewCursorAtItem(ctx, ns, root, newKey, search)
cur, err := NewCursorFromCompareFn(ctx, ns, root, newKey, compare)
if err != nil {
return Node{}, err
}

View File

@@ -27,7 +27,6 @@ import (
"sort"
"github.com/dolthub/dolt/go/store/hash"
"github.com/dolthub/dolt/go/store/val"
)
// Cursor explores a tree of Nodes.
@@ -45,39 +44,39 @@ type SearchFn func(nd Node) (idx int)
type ItemSearchFn func(item Item, nd Node) (idx int)
func NewCursorAtStart(ctx context.Context, nrw NodeStore, nd Node) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: nrw}
func NewCursorAtStart(ctx context.Context, ns NodeStore, nd Node) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: ns}
for !cur.isLeaf() {
nd, err = fetchChild(ctx, nrw, cur.CurrentRef())
nd, err = fetchChild(ctx, ns, cur.CurrentRef())
if err != nil {
return nil, err
}
parent := cur
cur = &Cursor{nd: nd, parent: parent, nrw: nrw}
cur = &Cursor{nd: nd, parent: parent, nrw: ns}
}
return
}
func NewCursorAtEnd(ctx context.Context, nrw NodeStore, nd Node) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: nrw}
func NewCursorAtEnd(ctx context.Context, ns NodeStore, nd Node) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: ns}
cur.skipToNodeEnd()
for !cur.isLeaf() {
nd, err = fetchChild(ctx, nrw, cur.CurrentRef())
nd, err = fetchChild(ctx, ns, cur.CurrentRef())
if err != nil {
return nil, err
}
parent := cur
cur = &Cursor{nd: nd, parent: parent, nrw: nrw}
cur = &Cursor{nd: nd, parent: parent, nrw: ns}
cur.skipToNodeEnd()
}
return
}
func NewCursorPastEnd(ctx context.Context, nrw NodeStore, nd Node) (cur *Cursor, err error) {
cur, err = NewCursorAtEnd(ctx, nrw, nd)
func NewCursorPastEnd(ctx context.Context, ns NodeStore, nd Node) (cur *Cursor, err error) {
cur, err = NewCursorAtEnd(ctx, ns, nd)
if err != nil {
return nil, err
}
@@ -94,13 +93,13 @@ func NewCursorPastEnd(ctx context.Context, nrw NodeStore, nd Node) (cur *Cursor,
return
}
func NewCursorAtOrdinal(ctx context.Context, nrw NodeStore, nd Node, ord uint64) (cur *Cursor, err error) {
func NewCursorAtOrdinal(ctx context.Context, ns NodeStore, nd Node, ord uint64) (cur *Cursor, err error) {
if ord >= uint64(nd.TreeCount()) {
return NewCursorPastEnd(ctx, nrw, nd)
return NewCursorPastEnd(ctx, ns, nd)
}
distance := int64(ord)
return NewCursorFromSearchFn(ctx, nrw, nd, func(nd Node) (idx int) {
return NewCursorFromSearchFn(ctx, ns, nd, func(nd Node) (idx int) {
if nd.IsLeaf() {
return int(distance)
}
@@ -119,8 +118,8 @@ func NewCursorAtOrdinal(ctx context.Context, nrw NodeStore, nd Node, ord uint64)
})
}
func NewCursorFromSearchFn(ctx context.Context, nrw NodeStore, nd Node, search SearchFn) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: nrw}
func NewCursorFromSearchFn(ctx context.Context, ns NodeStore, nd Node, search SearchFn) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: ns}
cur.idx = search(cur.nd)
for !cur.isLeaf() {
@@ -128,13 +127,13 @@ func NewCursorFromSearchFn(ctx context.Context, nrw NodeStore, nd Node, search S
// stay in bounds for internal nodes
cur.keepInBounds()
nd, err = fetchChild(ctx, nrw, cur.CurrentRef())
nd, err = fetchChild(ctx, ns, cur.CurrentRef())
if err != nil {
return cur, err
}
parent := cur
cur = &Cursor{nd: nd, parent: parent, nrw: nrw}
cur = &Cursor{nd: nd, parent: parent, nrw: ns}
cur.idx = search(cur.nd)
}
@@ -142,12 +141,16 @@ func NewCursorFromSearchFn(ctx context.Context, nrw NodeStore, nd Node, search S
return
}
func newCursorAtTuple(ctx context.Context, nrw NodeStore, nd Node, tup val.Tuple, search ItemSearchFn) (cur *Cursor, err error) {
return NewCursorAtItem(ctx, nrw, nd, Item(tup), search)
func NewCursorFromCompareFn(ctx context.Context, ns NodeStore, n Node, i Item, compare CompareFn) (*Cursor, error) {
return NewCursorAtItem(ctx, ns, n, i, func(item Item, node Node) (idx int) {
return sort.Search(node.Count(), func(i int) bool {
return compare(item, node.GetKey(i)) <= 0
})
})
}
func NewCursorAtItem(ctx context.Context, nrw NodeStore, nd Node, item Item, search ItemSearchFn) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: nrw}
func NewCursorAtItem(ctx context.Context, ns NodeStore, nd Node, item Item, search ItemSearchFn) (cur *Cursor, err error) {
cur = &Cursor{nd: nd, nrw: ns}
cur.idx = search(item, cur.nd)
for !cur.isLeaf() {
@@ -155,13 +158,13 @@ func NewCursorAtItem(ctx context.Context, nrw NodeStore, nd Node, item Item, sea
// stay in bounds for internal nodes
cur.keepInBounds()
nd, err = fetchChild(ctx, nrw, cur.CurrentRef())
nd, err = fetchChild(ctx, ns, cur.CurrentRef())
if err != nil {
return cur, err
}
parent := cur
cur = &Cursor{nd: nd, parent: parent, nrw: nrw}
cur = &Cursor{nd: nd, parent: parent, nrw: ns}
cur.idx = search(item, cur.nd)
}
@@ -169,8 +172,8 @@ func NewCursorAtItem(ctx context.Context, nrw NodeStore, nd Node, item Item, sea
return
}
func NewLeafCursorAtItem(ctx context.Context, nrw NodeStore, nd Node, item Item, search ItemSearchFn) (cur Cursor, err error) {
cur = Cursor{nd: nd, parent: nil, nrw: nrw}
func NewLeafCursorAtItem(ctx context.Context, ns NodeStore, nd Node, item Item, search ItemSearchFn) (cur Cursor, err error) {
cur = Cursor{nd: nd, parent: nil, nrw: ns}
cur.idx = search(item, cur.nd)
for !cur.isLeaf() {
@@ -179,7 +182,7 @@ func NewLeafCursorAtItem(ctx context.Context, nrw NodeStore, nd Node, item Item,
cur.keepInBounds()
// reuse |cur| object to keep stack alloc'd
cur.nd, err = fetchChild(ctx, nrw, cur.CurrentRef())
cur.nd, err = fetchChild(ctx, ns, cur.CurrentRef())
if err != nil {
return cur, err
}