This document walks you through how to use RawKV’s Checksum API.

Checksum API returns Crc64Xor, TotalKvs and TotalBytes from TiKV cluster.

  • Crc64Xor: The XOR of every key-value pair’s crc64 value.
  • TotalKVs: The number of key-value pairs.
  • TotalBytes: The size of key-value pairs in bytes.

Note: If API V2 is enabled, a 4 bytes prefix is encoded with keys, and also calculated by Checksum API.

Go

Checksum with range

Using the Checksum API, you can get {Crc64Xor, TotalKvs, TotalBytes} of a range from startKey (inclusive) to endKey (exclusive).

To calculate checksum of all keys, specify startKey and endKey as []byte("").

1. package main

3. import (
4. "context"
5. "fmt"

7. "github.com/pingcap/kvproto/pkg/kvrpcpb"
8. "github.com/tikv/client-go/v2/rawkv"
9. )

11. func main() {
12. ctx := context.TODO()
13. cli, err := rawkv.NewClientWithOpts(ctx, []string{"127.0.0.1:2379"},
14. rawkv.WithAPIVersion(kvrpcpb.APIVersion_V2))
15. if err != nil {
16. panic(err)
17. }
18. defer cli.Close()

20. fmt.Printf("Cluster ID: %d\n", cli.ClusterID())

22. // put key into tikv
23. cli.Put(ctx, []byte("k1"), []byte("v1"))
24. cli.Put(ctx, []byte("k2"), []byte("v2"))
25. cli.Put(ctx, []byte("k3"), []byte("v3"))
26. cli.Put(ctx, []byte("k4"), []byte("v4"))
27. cli.Put(ctx, []byte("k5"), []byte("v5"))

29. checksum, err := cli.Checksum(ctx, []byte("k1"), []byte("k6"))
30. if err != nil {
31. panic(err)
32. }

34. fmt.Printf("Get checksum, Crc64Xor:%d, TotalKvs:%d, TotalBytes:%d.\n",
35. checksum.Crc64Xor, checksum.TotalKvs, checksum.TotalBytes)
36. }

You will get the result as following:

1. Cluster ID: 7166545317297238572
2. Get checksum, Crc64Xor:7402990595130313958, TotalKvs:5, TotalBytes:40.