From 44f96581e3cd347264b106dda846781a1c24af50 Mon Sep 17 00:00:00 2001 From: peitingwei Date: Wed, 23 Nov 2022 11:24:18 +0800 Subject: [PATCH] Optimize the vonvert of tikv key and s3 filename for hbr --- hbr/cmd/backup.go | 13 ++++++++++--- hbr/cmd/restore.go | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/hbr/cmd/backup.go b/hbr/cmd/backup.go index b6b22b8..2f6db60 100644 --- a/hbr/cmd/backup.go +++ b/hbr/cmd/backup.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "fmt" + "strconv" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" @@ -65,7 +66,6 @@ func runBackup(cmd *cobra.Command, args []string) { } snapshot := client.GetSnapshot(ts) - //iter, err := snapshot.Iter([]byte("foo"), []byte("foo2")) iter, err := snapshot.Iter([]byte(""), []byte("")) if err != nil { fmt.Printf("Iter Error!\n%v\n", err) @@ -74,7 +74,14 @@ func runBackup(cmd *cobra.Command, args []string) { defer iter.Close() for iter.Valid() { k, v := iter.Key(), iter.Value() - fmt.Printf("key:%v\tval:%v\n", string(k), string(v)) + var fileneme string = "" + for ki, vi := range k { + fileneme += strconv.Itoa(int(vi)) + if ki != len(k)-1 { + fileneme += "-" + } + } + fmt.Printf("fileneme:%s\tkey:%s\n", fileneme, string(k)) if err := iter.Next(); err != nil { fmt.Printf("Iter Next Error!\n%v\n", err) return @@ -82,7 +89,7 @@ func runBackup(cmd *cobra.Command, args []string) { _, err = s3_client.PutObject(&s3.PutObjectInput{ Bucket: aws.String(bucket), // bucket名称 - Key: aws.String(backup_name + "/" + string(k)), + Key: aws.String(backup_name + "/" + fileneme), Body: bytes.NewReader(v), }) if err != nil { diff --git a/hbr/cmd/restore.go b/hbr/cmd/restore.go index e5dc369..d9001c0 100644 --- a/hbr/cmd/restore.go +++ b/hbr/cmd/restore.go @@ -4,6 +4,8 @@ import ( //"bufio" "context" "fmt" + "strconv" + "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" @@ -87,7 +89,22 @@ func runRestore(cmd *cobra.Command, args []string) { fmt.Printf("Tikv Transaction Begin Error!\n%v\n", err) return } - if err := txn.Set([]byte((*keys.Key)[len(backup_name)+1:]), data); err != nil { + + substrings := strings.Split((*keys.Key)[len(backup_name)+1:], "-") + var tiKey []byte = make([]byte, len(substrings)) + + for ki, vi := range substrings { + temK, err := strconv.Atoi(vi) + if err != nil { + fmt.Printf("Strconv.Atoi Error!\n%v\n", err) + return + } + tiKey[ki] = byte(temK) + } + + fmt.Printf("fileneme:%s\ttikey:%v\n", (*keys.Key)[len(backup_name)+1:], tiKey) + + if err := txn.Set(tiKey, data); err != nil { fmt.Printf("Tikv Set Error!\n%v\n", err) return }