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
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pingcap/kvproto/pkg/kvrpcpb"
  6. "github.com/tikv/client-go/v2/rawkv"
  7. )
  8. func main() {
  9. ctx := context.TODO()
  10. cli, err := rawkv.NewClientWithOpts(ctx, []string{"127.0.0.1:2379"},
  11. rawkv.WithAPIVersion(kvrpcpb.APIVersion_V2))
  12. if err != nil {
  13. panic(err)
  14. }
  15. defer cli.Close()
  16. fmt.Printf("Cluster ID: %d\n", cli.ClusterID())
  17. // put key into tikv
  18. cli.Put(ctx, []byte("k1"), []byte("v1"))
  19. cli.Put(ctx, []byte("k2"), []byte("v2"))
  20. cli.Put(ctx, []byte("k3"), []byte("v3"))
  21. cli.Put(ctx, []byte("k4"), []byte("v4"))
  22. cli.Put(ctx, []byte("k5"), []byte("v5"))
  23. checksum, err := cli.Checksum(ctx, []byte("k1"), []byte("k6"))
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Printf("Get checksum, Crc64Xor:%d, TotalKvs:%d, TotalBytes:%d.\n",
  28. checksum.Crc64Xor, checksum.TotalKvs, checksum.TotalBytes)
  29. }

You will get the result as following:

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