-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
153b95d
commit 1dc036f
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (d *dbTool) newGetCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "get <key>", | ||
Short: "Retrieves key value and dumps it to stdout", | ||
RunE: d.runGet, | ||
Args: cobra.MinimumNArgs(1), | ||
PreRunE: d.openDB(true), | ||
} | ||
} | ||
|
||
func (d *dbTool) runGet(_ *cobra.Command, args []string) error { | ||
return d.db.View(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte(args[0])) | ||
if err != nil { | ||
return err | ||
} | ||
v, err := item.ValueCopy(nil) | ||
if err != nil { | ||
return err | ||
} | ||
_, _ = io.Copy(os.Stdout, bytes.NewBuffer(v)) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (d *dbTool) newListCommand() *cobra.Command { | ||
cmd := cobra.Command{ | ||
Use: "list", | ||
Short: "Lists db keys", | ||
RunE: d.runList, | ||
PreRunE: d.openDB(true), | ||
} | ||
cmd.Flags().StringVarP(&d.prefix, "prefix", "p", "", "key prefix") | ||
return &cmd | ||
} | ||
|
||
func (d *dbTool) runList(_ *cobra.Command, _ []string) error { | ||
return d.db.View(func(txn *badger.Txn) error { | ||
opts := badger.DefaultIteratorOptions | ||
opts.PrefetchValues = false | ||
opts.Prefix = []byte(d.prefix) | ||
it := txn.NewIterator(opts) | ||
defer it.Close() | ||
for it.Rewind(); it.Valid(); it.Next() { | ||
_, _ = fmt.Fprintf(os.Stdout, "%s\n", it.Item().Key()) | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/dgraph-io/badger/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (d *dbTool) newRemoveCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "remove <key>", | ||
Short: "Removes key value from the database", | ||
RunE: d.runRemove, | ||
Args: cobra.MinimumNArgs(1), | ||
PreRunE: d.openDB(false), | ||
} | ||
} | ||
|
||
func (d *dbTool) runRemove(_ *cobra.Command, args []string) error { | ||
return d.db.Update(func(txn *badger.Txn) error { | ||
return txn.Delete([]byte(args[0])) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/dgraph-io/badger/v2" | ||
"github.com/dgraph-io/badger/v2/options" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type dbTool struct { | ||
dir string | ||
prefix string | ||
|
||
db *badger.DB | ||
} | ||
|
||
func Execute() error { | ||
return newRootCommand().Execute() | ||
} | ||
|
||
func newRootCommand() *cobra.Command { | ||
d := new(dbTool) | ||
root := cobra.Command{ | ||
Use: "dbtool [command]", | ||
Short: "DB helper tool", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
PersistentPostRunE: d.closeDB(), | ||
} | ||
|
||
root.PersistentFlags().StringVarP(&d.dir, "dir", "d", ".", "database directory path") | ||
root.AddCommand( | ||
d.newListCommand(), | ||
d.newRemoveCommand(), | ||
d.newGetCommand()) | ||
|
||
return &root | ||
} | ||
|
||
func (d *dbTool) openDB(ro bool) func(cmd *cobra.Command, args []string) error { | ||
return func(c *cobra.Command, _ []string) error { | ||
db, err := openDB(d.dir, ro) | ||
if err != nil { | ||
return fmt.Errorf("failed to open %s: %v", d.dir, err) | ||
} | ||
d.db = db | ||
return nil | ||
} | ||
} | ||
|
||
func (d *dbTool) closeDB() func(cmd *cobra.Command, args []string) error { | ||
return func(c *cobra.Command, _ []string) error { | ||
if d.db == nil { | ||
return nil | ||
} | ||
if err := d.db.Close(); err != nil { | ||
return fmt.Errorf("closing database: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func openDB(dir string, ro bool) (*badger.DB, error) { | ||
if !ro { | ||
// When DB is opened for RW, badger.Open does not fail | ||
// if the directory is not a badger database, instead it | ||
// creates one. | ||
f, err := os.Open(filepath.Join(dir, badger.ManifestFilename)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_ = f.Close() | ||
} | ||
return badger.Open(badger.DefaultOptions(dir). | ||
WithCompression(options.ZSTD). | ||
WithReadOnly(ro)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
|
||
"github.com/pyroscope-io/pyroscope/scripts/dbtool/cmd" | ||
) | ||
|
||
func main() { | ||
if err := cmd.Execute(); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, color.RedString("Error:"), err) | ||
os.Exit(1) | ||
} | ||
} |