mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-24 03:16:12 -05:00
Merge pull request #5231 from dolthub/andy/last-key
go/store/tree: use constant-time root node access for last key lookup
This commit is contained in:
@@ -231,10 +231,7 @@ func nextSchemasTableIndex(ctx *sql.Context, root *doltdb.RootValue) (int64, err
|
||||
|
||||
if types.IsFormat_DOLT(tbl.Format()) {
|
||||
p := durable.ProllyMapFromIndex(rows)
|
||||
key, _, err := p.Last(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
key := p.LastKey(ctx)
|
||||
kd, _ := p.Descriptors()
|
||||
|
||||
i, _ := kd.GetInt64(0, key)
|
||||
|
||||
@@ -247,14 +247,12 @@ func (t StaticMap[K, V, O]) Has(ctx context.Context, query K) (ok bool, err erro
|
||||
return
|
||||
}
|
||||
|
||||
func (t StaticMap[K, V, O]) Last(ctx context.Context) (key K, value V, err error) {
|
||||
cur, err := NewCursorAtEnd(ctx, t.NodeStore, t.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if cur.Valid() {
|
||||
key, value = K(cur.CurrentKey()), V(cur.CurrentValue())
|
||||
func (t StaticMap[K, V, O]) LastKey(ctx context.Context) (key K) {
|
||||
if t.Root.count > 0 {
|
||||
// if |t.Root| is a leaf node, it represents the entire map
|
||||
// if |t.Root| is an internal node, its last key is the
|
||||
// delimiter for last subtree and is the last key in the map
|
||||
key = K(getLastKey(t.Root))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -445,7 +443,7 @@ func (t StaticMap[K, V, O]) getKeyRangeCursors(ctx context.Context, startInclusi
|
||||
return
|
||||
}
|
||||
|
||||
// getOrdinalForKey returns the smallest ordinal position at which the key >= |query|.
|
||||
// GetOrdinalForKey returns the smallest ordinal position at which the key >= |query|.
|
||||
func (t StaticMap[K, V, O]) GetOrdinalForKey(ctx context.Context, query K) (uint64, error) {
|
||||
cur, err := NewCursorAtKey(ctx, t.NodeStore, t.Root, query, t.Order)
|
||||
if err != nil {
|
||||
|
||||
@@ -200,6 +200,10 @@ func walkAddresses(ctx context.Context, nd Node, cb AddressCb) (err error) {
|
||||
return message.WalkAddresses(ctx, nd.msg, cb)
|
||||
}
|
||||
|
||||
func getLastKey(nd Node) Item {
|
||||
return nd.GetKey(int(nd.count) - 1)
|
||||
}
|
||||
|
||||
// OutputProllyNode writes the node given to the writer given in a semi-human-readable format, where values are still
|
||||
// displayed in hex-encoded byte strings, but are delineated into their fields. All nodes have keys displayed in this
|
||||
// manner. Interior nodes have their child hash references spelled out, leaf nodes have value tuples delineated like
|
||||
|
||||
@@ -43,7 +43,7 @@ func writeNewNode[S message.Serializer](ctx context.Context, ns NodeStore, bld *
|
||||
|
||||
var lastKey Item
|
||||
if node.count > 0 {
|
||||
k := node.GetKey(int(node.count) - 1)
|
||||
k := getLastKey(node)
|
||||
lastKey = ns.Pool().Get(uint64(len(k)))
|
||||
copy(lastKey, k)
|
||||
}
|
||||
|
||||
@@ -264,8 +264,8 @@ func (m Map) Has(ctx context.Context, key val.Tuple) (ok bool, err error) {
|
||||
return m.tuples.Has(ctx, key)
|
||||
}
|
||||
|
||||
func (m Map) Last(ctx context.Context) (key, value val.Tuple, err error) {
|
||||
return m.tuples.Last(ctx)
|
||||
func (m Map) LastKey(ctx context.Context) val.Tuple {
|
||||
return m.tuples.LastKey(ctx)
|
||||
}
|
||||
|
||||
// IterAll returns a MapIter that iterates over the entire Map.
|
||||
|
||||
Reference in New Issue
Block a user