Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: Tikv client Iterator和tidbkv Iterator的区别
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
?