Skip to content

Commit

Permalink
cmd/dump&load: support zstd compression, and adjust the compression l…
Browse files Browse the repository at this point in the history
…evel (#4921)
  • Loading branch information
SandyXSD authored Sep 25, 2024
1 parent a977aeb commit a7566ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
22 changes: 11 additions & 11 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package cmd

import (
"compress/gzip"
"errors"
"io"
"os"
"strings"

"github.com/DataDog/zstd"
"github.com/juicedata/juicefs/pkg/meta"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -78,15 +80,12 @@ func dumpMeta(m meta.Meta, dst string, threads int, keepSecret, fast, skipTrash
w = os.Stdout
} else {
tmp := dst + ".tmp"
fp, e := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
fp, e := os.Create(tmp)
if e != nil {
return e
}
defer func() {
e := fp.Close()
if err == nil {
err = e
}
err = errors.Join(err, fp.Close())
if err == nil {
err = os.Rename(tmp, dst)
} else {
Expand All @@ -95,14 +94,15 @@ func dumpMeta(m meta.Meta, dst string, threads int, keepSecret, fast, skipTrash
}()

if strings.HasSuffix(dst, ".gz") {
zw := gzip.NewWriter(fp)
w, _ = gzip.NewWriterLevel(fp, gzip.BestSpeed)
defer func() {
err = errors.Join(err, w.Close())
}()
} else if strings.HasSuffix(dst, ".zstd") {
w = zstd.NewWriterLevel(fp, zstd.BestSpeed)
defer func() {
e := zw.Close()
if err == nil {
err = e
}
err = errors.Join(err, w.Close())
}()
w = zw
} else {
w = fp
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"strings"

"github.com/DataDog/zstd"
"github.com/juicedata/juicefs/pkg/object"

"github.com/juicedata/juicefs/pkg/meta"
Expand Down Expand Up @@ -122,6 +123,9 @@ func load(ctx *cli.Context) error {
return err
}
defer r.Close()
} else if strings.HasSuffix(src, ".zstd") {
r = zstd.NewReader(fp)
defer r.Close()
} else {
r = fp
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/vfs/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func backup(m meta.Meta, blob object.ObjectStorage, now time.Time, fast, skipTra
}
defer os.Remove(fp.Name())
defer fp.Close()
zw := gzip.NewWriter(fp)
zw, _ := gzip.NewWriterLevel(fp, gzip.BestSpeed)
var threads = 2
if m.Name() == "tikv" {
threads = 10
Expand Down

0 comments on commit a7566ed

Please sign in to comment.