mirror of
https://github.com/luxfi/zapdb.git
synced 2026-10-30 15:12:20 +00:00
The module has been github.com/luxfi/zapdb for a while, but the package inside it still declared itself badger, so every consumer had to write zapdb "github.com/luxfi/zapdb" // package badger and the alias existed only to paper over the mismatch. 56 root files and the 134 qualified references inside the repo now say zapdb. Backward compatible in both directions: an existing consumer with the alias keeps compiling (the alias is now merely redundant), and one without it starts compiling instead of failing on an undefined zapdb. Directory badger/ and its cmd package are untouched — that is the CLI, and its binary is named badger. Build and vet clean; suite green except TestZRestore, which wants a /tmp/zb/src benchmark fixture and fails identically without this change. Co-authored-by: Hanzo Dev <[email protected]>
124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/luxfi/zapdb"
|
|
"github.com/luxfi/zapdb/options"
|
|
"github.com/luxfi/zapdb/y"
|
|
)
|
|
|
|
var streamCmd = &cobra.Command{
|
|
Use: "stream",
|
|
Short: "Stream DB into another DB with different options",
|
|
Long: `
|
|
This command streams the contents of this DB into another DB with the given options.
|
|
`,
|
|
RunE: stream,
|
|
}
|
|
|
|
var so = struct {
|
|
outDir string
|
|
outFile string
|
|
compressionType uint32
|
|
numVersions int
|
|
readOnly bool
|
|
keyPath string
|
|
}{}
|
|
|
|
func init() {
|
|
// TODO: Add more options.
|
|
RootCmd.AddCommand(streamCmd)
|
|
streamCmd.Flags().StringVarP(&so.outDir, "out", "o", "",
|
|
"Path to output DB. The directory should be empty.")
|
|
streamCmd.Flags().StringVarP(&so.outFile, "", "f", "",
|
|
"Run a backup to this file.")
|
|
streamCmd.Flags().BoolVarP(&so.readOnly, "read_only", "", true,
|
|
"Option to open input DB in read-only mode")
|
|
streamCmd.Flags().IntVarP(&so.numVersions, "num_versions", "", 0,
|
|
"Option to configure the maximum number of versions per key. "+
|
|
"Values <= 0 will be considered to have the max number of versions.")
|
|
streamCmd.Flags().Uint32VarP(&so.compressionType, "compression", "", 1,
|
|
"Option to configure the compression type in output DB. "+
|
|
"0 to disable, 1 for Snappy, and 2 for ZSTD.")
|
|
streamCmd.Flags().StringVarP(&so.keyPath, "encryption-key-file", "e", "",
|
|
"Path of the encryption key file.")
|
|
}
|
|
|
|
func stream(cmd *cobra.Command, args []string) error {
|
|
// Options for input DB.
|
|
if so.numVersions <= 0 {
|
|
so.numVersions = math.MaxInt32
|
|
}
|
|
encKey, err := getKey(so.keyPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
inOpt := zapdb.DefaultOptions(sstDir).
|
|
WithReadOnly(so.readOnly).
|
|
WithValueThreshold(1 << 10 /* 1KB */).
|
|
WithNumVersionsToKeep(so.numVersions).
|
|
WithBlockCacheSize(100 << 20).
|
|
WithIndexCacheSize(200 << 20).
|
|
WithEncryptionKey(encKey)
|
|
|
|
// Options for output DB.
|
|
if so.compressionType > 2 {
|
|
return errors.New(
|
|
"compression value must be one of 0 (disabled), 1 (Snappy), or 2 (ZSTD)")
|
|
}
|
|
inDB, err := zapdb.OpenManaged(inOpt)
|
|
if err != nil {
|
|
return y.Wrapf(err, "cannot open DB at %s", sstDir)
|
|
}
|
|
defer inDB.Close()
|
|
|
|
stream := inDB.NewStreamAt(math.MaxUint64)
|
|
|
|
if len(so.outDir) > 0 {
|
|
if _, err := os.Stat(so.outDir); err == nil {
|
|
f, err := os.Open(so.outDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
_, err = f.Readdirnames(1)
|
|
if err != io.EOF {
|
|
return fmt.Errorf(
|
|
"cannot run stream tool on non-empty output directory %s", so.outDir)
|
|
}
|
|
}
|
|
|
|
stream.LogPrefix = "DB.Stream"
|
|
outOpt := inOpt.
|
|
WithDir(so.outDir).
|
|
WithValueDir(so.outDir).
|
|
WithNumVersionsToKeep(so.numVersions).
|
|
WithCompression(options.CompressionType(so.compressionType)).
|
|
WithEncryptionKey(encKey).
|
|
WithReadOnly(false)
|
|
err = inDB.StreamDB(outOpt)
|
|
|
|
} else if len(so.outFile) > 0 {
|
|
stream.LogPrefix = "DB.Backup"
|
|
f, err := os.OpenFile(so.outFile, os.O_RDWR|os.O_CREATE, 0666)
|
|
y.Check(err)
|
|
_, err = stream.Backup(f, 0)
|
|
y.Check(err)
|
|
}
|
|
fmt.Println("Done.")
|
|
return err
|
|
}
|