Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tool,vfs: enable encrypted fs support for debug tool #1130

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tool/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func runTests(t *testing.T, path string) {
require.NoError(t, err)

fs := vfs.NewMem()
vfsForDir := func(dir string) (vfs.FS, error) {
return fs, nil
}
t.Run(name, func(t *testing.T) {
datadriven.RunTest(t, path, func(d *datadriven.TestData) string {
args := []string{d.Cmd}
Expand Down Expand Up @@ -112,7 +115,7 @@ func runTests(t *testing.T, path string) {
return &m
}()

tool := New(DefaultComparer(comparer), Comparers(altComparer), Mergers(merger), FS(fs))
tool := New(DefaultComparer(comparer), Comparers(altComparer), Mergers(merger), FS(vfsForDir))

c := &cobra.Command{}
c.AddCommand(tool.Commands...)
Expand Down
26 changes: 24 additions & 2 deletions tool/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cockroachdb/pebble/internal/manifest"
"github.com/cockroachdb/pebble/internal/record"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/spf13/cobra"
)

Expand All @@ -39,6 +40,7 @@ type dbT struct {
opts *pebble.Options
comparers sstable.Comparers
mergers sstable.Mergers
vfsForDir vfs.DirFSRetriever

// Flags.
comparerName string
Expand All @@ -51,7 +53,12 @@ type dbT struct {
verbose bool
}

func newDB(opts *pebble.Options, comparers sstable.Comparers, mergers sstable.Mergers) *dbT {
func newDB(
opts *pebble.Options,
comparers sstable.Comparers,
mergers sstable.Mergers,
vfsForDir vfs.DirFSRetriever,
) *dbT {
d := &dbT{
opts: opts,
comparers: comparers,
Expand Down Expand Up @@ -176,6 +183,15 @@ use by another process.
return d
}

func (d *dbT) determineFS(dir string) error {
fs, err := d.vfsForDir(dir)
if err != nil {
return err
}
d.opts.FS = fs
return nil
}

func (d *dbT) loadOptions(dir string) error {
ls, err := d.opts.FS.List(dir)
if err != nil || len(ls) == 0 {
Expand Down Expand Up @@ -350,7 +366,13 @@ func (d *dbT) runGet(cmd *cobra.Command, args []string) {
}

func (d *dbT) runLSM(cmd *cobra.Command, args []string) {
db, err := d.openDB(args[0])
dir := args[0]
err := d.determineFS(dir)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
}
db, err := d.openDB(dir)
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
return
Expand Down
8 changes: 5 additions & 3 deletions tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type T struct {
comparers sstable.Comparers
mergers sstable.Mergers
defaultComparer string
vfsForDir vfs.DirFSRetriever
}

// A Option configures the Pebble introspection tool.
Expand Down Expand Up @@ -84,9 +85,9 @@ func Filters(filters ...FilterPolicy) Option {
}

// FS sets the filesystem implementation to use by the introspection tools.
func FS(fs vfs.FS) Option {
func FS(f vfs.DirFSRetriever) Option {
return func(t *T) {
t.opts.FS = fs
t.vfsForDir = f
}
}

Expand All @@ -101,6 +102,7 @@ func New(opts ...Option) *T {
comparers: make(sstable.Comparers),
mergers: make(sstable.Mergers),
defaultComparer: base.DefaultComparer.Name,
vfsForDir: vfs.DefaultFSForDir,
}

opts = append(opts,
Expand All @@ -112,7 +114,7 @@ func New(opts ...Option) *T {
opt(t)
}

t.db = newDB(&t.opts, t.comparers, t.mergers)
t.db = newDB(&t.opts, t.comparers, t.mergers, t.vfsForDir)
t.find = newFind(&t.opts, t.comparers, t.defaultComparer, t.mergers)
t.lsm = newLSM(&t.opts, t.comparers)
t.manifest = newManifest(&t.opts, t.comparers)
Expand Down
8 changes: 8 additions & 0 deletions vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,11 @@ func Root(fs FS) FS {
}
return fs
}

// DirFSRetriever is a function that returns the FS for a directory.
type DirFSRetriever func(dir string) (FS, error)

// DefaultFSForDir is a DirFSRetriever that returns the default file system.
func DefaultFSForDir(dir string) (FS, error) {
return Default, nil
}