Skip to content

IterReverse does not return data when k is nil #1781

@buzzers

Description

@buzzers

When calling IterReverse with nil as parameter k, the returned data is empty.
Is this because TiKV does not support this type of scan or is it a driver bug?

The code to reproduce this is as follows:

package main

import (
	"context"
	"fmt"

	"github.com/tikv/client-go/v2/txnkv"
)

func PrefixNextKey(k []byte) []byte {
	buf := make([]byte, len(k))
	copy(buf, k)
	var i int
	for i = len(k) - 1; i >= 0; i-- {
		buf[i]++
		if buf[i] != 0 {
			break
		}
	}
	if i == -1 {
		buf = make([]byte, 0)
	}
	return buf
}

func main() {
	var err error
	pd, err := txnkv.NewClient([]string{"127.0.0.1:2379"})
	if err != nil {
		panic(err)
	}
	defer pd.Close()

	func() {
		txn, err := pd.Begin()
		if err != nil {
			panic(err)
		}
		txn.Set([]byte("b_i100005_00000001_08"), []byte("1"))
		txn.Set([]byte("b_i100005_00000002_07"), []byte("2"))
		txn.Set([]byte("b_i100005_00000003_06"), []byte("3"))
		txn.Set([]byte("b_i100005_00000004_05"), []byte("4"))
		txn.Set([]byte("b_i100005_00000005_04"), []byte("5"))
		txn.Set([]byte("b_i100005_00000006_03"), []byte("6"))
		txn.Set([]byte("b_i100005_00000007_02"), []byte("7"))
		txn.Set([]byte("b_i100005_00000008_01"), []byte("8"))
		defer func() {
			err := txn.Commit(context.TODO())
			if err != nil {
				panic(err)
			}
		}()
	}()

	func() {
		timestamp, err := pd.CurrentTimestamp("")
		if err != nil {
			panic(err)
		}
		snapshot := pd.GetSnapshot(timestamp)

		k := PrefixNextKey([]byte("b_i100005_00000005"))
		lowerBound := PrefixNextKey([]byte("b_i100005_00000001"))

		func() {
			fmt.Printf("================================\n..%v\n", string(k))
			iter, err := snapshot.IterReverse(k, nil)
			if err != nil {
				panic(err)
			}
			defer iter.Close()
			for iter.Valid() {
				fmt.Printf("%v=%v\n", string(iter.Key()), string(iter.Value()))
				iter.Next()
			}
			fmt.Println("================================")
		}()

		func() {
			fmt.Printf("================================\n%v..\n", string(lowerBound))
			iter, err := snapshot.IterReverse(nil, lowerBound)
			if err != nil {
				panic(err)
			}
			defer iter.Close()
			for iter.Valid() {
				fmt.Printf("%v=%v\n", string(iter.Key()), string(iter.Value()))
				iter.Next()
			}
			fmt.Println("================================")
		}()

		// output:
		// ================================
		// ..b_i100005_00000006
		// b_i100005_00000005_04=5
		// b_i100005_00000004_05=4
		// b_i100005_00000003_06=3
		// b_i100005_00000002_07=2
		// b_i100005_00000001_08=1
		// ================================
		// ================================
		// b_i100005_00000002..
		// ================================
	}()
}

TiKV: v8.5.3
client-go: v2.0.8-0.20251028065425-b7d4dfd8520e

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributionThis PR is from a community contributor.first-time-contributorIndicates that the PR was contributed by an external member and is a first-time contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions