Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktobey committed Dec 17, 2024
1 parent 08f51c5 commit 43fd1e4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
70 changes: 70 additions & 0 deletions go/store/prolly/tree/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,76 @@ func (it *OrderedTreeIter[K, V]) Iterate(ctx context.Context) (err error) {
return
}

type OrderedTreeIterLargeNodes[K, V ~[]byte] struct {
// current tuple location
curr *cursor

// the function called to moved |curr| forward in the direction of iteration.
step func(context.Context) error
// should return |true| if the passed in cursor is past the iteration's stopping point.
stop func(*cursor) bool
}

func (it *OrderedTreeIterLargeNodes[K, V]) currentItems() (key, value Item, err error) {
// The BLOB node type contains no keys, and a single value in each leaf node.
// Some trees (such as the trees for storing JSON) use BLOB leaf nodes but indexed non-leaf nodes.
// When advancing a cursor through such a tree, the leaf nodes won't contain keys, but their parents will.
largeNode, err := LargeNodeFromNode(it.curr.nd)
if err != nil {
return nil, nil, err
}
if largeNode.keys.IsEmpty() {
key = it.curr.parent.CurrentKey()
} else {
key = it.curr.nd.keys.GetItem(it.curr.idx, it.curr.nd.msg)
}
value = it.curr.nd.values.GetItem(it.curr.idx, it.curr.nd.msg)
return
}

func (it *OrderedTreeIterLargeNodes[K, V]) Next(ctx context.Context) (key K, value V, err error) {
if it.curr == nil {
return nil, nil, io.EOF
}

k, v := currentCursorItems(it.curr)
key, value = K(k), V(v)

err = it.step(ctx)
if err != nil {
return nil, nil, err
}
if it.stop(it.curr) {
// past the end of the range
it.curr = nil
}

return
}

func (it *OrderedTreeIterLargeNodes[K, V]) Current() (key K, value V) {
// |it.curr| is set to nil when its range is exhausted
if it.curr != nil && it.curr.Valid() {
k, v := currentCursorItems(it.curr)
key, value = K(k), V(v)
}
return
}

func (it *OrderedTreeIterLargeNodes[K, V]) Iterate(ctx context.Context) (err error) {
err = it.step(ctx)
if err != nil {
return err
}

if it.stop(it.curr) {
// past the end of the range
it.curr = nil
}

return
}

type orderedLeafSpanIter[K, V ~[]byte] struct {
// in-progress node
nd Node
Expand Down
50 changes: 50 additions & 0 deletions go/store/prolly/tree/node_large_cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0

package tree

type largeCursor struct {
nd LargeNode
idx int
parent *largeCursor
nrw NodeStore
}

func largeCursorFromCursor(c *cursor) (largeCursor, error) {
largeNode, err := LargeNodeFromNode(c.nd)
if err != nil {
return largeCursor{}, err
}
var largeParent *largeCursor
if c.parent != nil {
largeParent, err = largeCursorFromCursor(c.parent)
if err != nil {
return largeCursor{}, err
}
}

return largeCursor{
nd: largeNode,
idx: c.idx,
parent: largeParent,
nrw: c.nrw,
}, nil
}

0 comments on commit 43fd1e4

Please sign in to comment.