Differences between Tikv client Iterator and tidbkv Iterator

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: Tikv client Iterator和tidbkv Iterator的区别

| username: Timber

Development Issue

Recently, while developing an application, I used both the tikv txn client (“github.com/tikv/client-go/v2/txnkv/transaction”) Iterator and the tidbkv (“github.com/pingcap/tidb/kv”) Iterator and found that their functionalities are similar. For example, if I want to iterate over a KeyRange from a certain checkpoint, for the former, I can write code like this:

import (
  "github.com/tikv/client-go/v2/txnkv"
  "github.com/tikv/client-go/v2/tikv"
)
 
  client, _ := txnkv.NewClient(pdAddrs)
  txnOptions := txnOption{tikv.WithStartTS(checkpoint)}
  txn, _ := client.Begin(txnOptions...)
  iterator := txn.Iter(keyRange.Begin, keyRange.End)

For the latter, I can write code like this:

import (
  tidbkv "github.com/pingcap/tidb/kv"

  cdckv "github.com/pingcap/tiflow/cdc/kv"
)
storage := cdckv.CreateTiStore("", xxx)
snap := storage.GetSnapShot(checkpoint)
iterator := snap.Iter(keyRange.Begin, keyRange.End)

The functionalities of both seem quite similar. My question is whether there are any differences in their internal implementations and whether there is any impact on performance.

Additionally, the Iterator interfaces of these two are not consistent. In the txnClient definition, the Iterator is defined as:

type Iterator interface {
	Valid() bool
	Key() []byte
	Value() []byte
	Next() error
	Close()
}

While in tidbkv, the Iterator is defined as:

type Iterator interface {
	Valid() bool
	Key() Key
	Value() []byte
	Next() error
	Close()
}

In the latter, the Key is defined as type Key []byte. Since there is no =, the two Iterators cannot be converted to each other in external usage. If there are no differences between the two, should they be defined as the same type, or should the Key type be defined as type Key = []byte?

| username: TiDBer_jYQINSnf | Original post link

My understanding is that the definition of this interface in TiDB means that any KV that satisfies this interface can be used by me. TiKV provides this interface.

When the KV is TiKV, you can debug it yourself to see if it is the same. Your code doesn’t seem to be the storage provided by TiKV.

| username: Timber | Original post link

The current situation is that the TiKV client’s interface is not compatible with TiDB’s interface.

It doesn’t look like you’ve tried it yourself.

| username: TiDBer_jYQINSnf | Original post link

After thinking about it, I realized I was wrong. The communication between TiDB and TiKV uses gRPC, not direct interface calls, so it’s normal that the interface types cannot be directly converted.

| username: Timber | Original post link

Thank you for the explanation.

| username: system | Original post link

This topic will be automatically closed 60 days after the last reply. No new replies are allowed.