Skip to content

Commit 63ba2e2

Browse files
author
James Cor
committed
pass cursor as pointer
1 parent ab5d58b commit 63ba2e2

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

go/store/prolly/tree/map.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ func (t StaticMap[K, V, O]) WalkNodes(ctx context.Context, cb NodeCb) error {
240240
}
241241

242242
func (t StaticMap[K, V, O]) Get(ctx context.Context, query K, cb KeyValueFn[K, V]) (err error) {
243-
cur, err := newLeafCursorAtKey(ctx, t.NodeStore, t.Root, query, t.Order)
243+
cur := &cursor{nd: t.Root, nrw: t.NodeStore}
244+
err = moveCursorToKey(ctx, cur, query, t.Order)
244245
if err != nil {
245246
return err
246247
}
@@ -260,7 +261,8 @@ func (t StaticMap[K, V, O]) Get(ctx context.Context, query K, cb KeyValueFn[K, V
260261
}
261262

262263
func (t StaticMap[K, V, O]) GetPrefix(ctx context.Context, query K, prefixOrder O, cb KeyValueFn[K, V]) (err error) {
263-
cur, err := newLeafCursorAtKey(ctx, t.NodeStore, t.Root, query, prefixOrder)
264+
cur := &cursor{nd: t.Root, nrw: t.NodeStore}
265+
err = moveCursorToKey(ctx, cur, query, prefixOrder)
264266
if err != nil {
265267
return err
266268
}
@@ -280,7 +282,8 @@ func (t StaticMap[K, V, O]) GetPrefix(ctx context.Context, query K, prefixOrder
280282
}
281283

282284
func (t StaticMap[K, V, O]) Has(ctx context.Context, query K) (ok bool, err error) {
283-
cur, err := newLeafCursorAtKey(ctx, t.NodeStore, t.Root, query, t.Order)
285+
cur := &cursor{nd: t.Root, nrw: t.NodeStore}
286+
err = moveCursorToKey(ctx, cur, query, t.Order)
284287
if err != nil {
285288
return false, err
286289
} else if cur.Valid() {
@@ -290,7 +293,8 @@ func (t StaticMap[K, V, O]) Has(ctx context.Context, query K) (ok bool, err erro
290293
}
291294

292295
func (t StaticMap[K, V, O]) HasPrefix(ctx context.Context, query K, prefixOrder O) (ok bool, err error) {
293-
cur, err := newLeafCursorAtKey(ctx, t.NodeStore, t.Root, query, prefixOrder)
296+
cur := &cursor{nd: t.Root, nrw: t.NodeStore}
297+
err = moveCursorToKey(ctx, cur, query, prefixOrder)
294298
if err != nil {
295299
return false, err
296300
} else if cur.Valid() {

go/store/prolly/tree/node_cursor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ func newCursorFromSearchFn(ctx context.Context, ns NodeStore, nd *Node, search S
183183
return
184184
}
185185

186-
func newLeafCursorAtKey[K ~[]byte, O Ordering[K]](ctx context.Context, ns NodeStore, nd *Node, key K, order O) (cursor, error) {
186+
func moveCursorToKey[K ~[]byte, O Ordering[K]](ctx context.Context, cur *cursor, key K, order O) error {
187187
var err error
188-
cur := cursor{nd: nd, nrw: ns}
189188
for {
190189
// binary search |cur.nd| for |key|
191190
i, j := 0, cur.nd.Count()
@@ -208,12 +207,12 @@ func newLeafCursorAtKey[K ~[]byte, O Ordering[K]](ctx context.Context, ns NodeSt
208207
cur.keepInBounds()
209208

210209
// reuse |cur| object to keep stack alloc'd
211-
cur.nd, err = fetchChild(ctx, ns, cur.currentRef())
210+
cur.nd, err = fetchChild(ctx, cur.nrw, cur.currentRef())
212211
if err != nil {
213-
return cur, err
212+
return err
214213
}
215214
}
216-
return cur, nil
215+
return nil
217216
}
218217

219218
// searchForKey returns a SearchFn for |key|.

0 commit comments

Comments
 (0)