github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/show_ranges.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/keys"
    17  	"github.com/cockroachdb/cockroach/pkg/kv"
    18  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    19  )
    20  
    21  // ScanMetaKVs returns the meta KVs for the ranges that touch the given span.
    22  func ScanMetaKVs(ctx context.Context, txn *kv.Txn, span roachpb.Span) ([]kv.KeyValue, error) {
    23  	metaStart := keys.RangeMetaKey(keys.MustAddr(span.Key).Next())
    24  	metaEnd := keys.RangeMetaKey(keys.MustAddr(span.EndKey))
    25  
    26  	kvs, err := txn.Scan(ctx, metaStart, metaEnd, 0)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	if len(kvs) == 0 || !kvs[len(kvs)-1].Key.Equal(metaEnd.AsRawKey()) {
    31  		// Normally we need to scan one more KV because the ranges are addressed by
    32  		// the end key.
    33  		extraKV, err := txn.Scan(ctx, metaEnd, keys.Meta2Prefix.PrefixEnd(), 1 /* one result */)
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  		kvs = append(kvs, extraKV[0])
    38  	}
    39  	return kvs, nil
    40  }