From bff6b17a8e4c5ce601717a3673e33c530deb021a Mon Sep 17 00:00:00 2001 From: Matthew Jaffee Date: Wed, 2 Feb 2022 09:32:48 -0600 Subject: [PATCH] remove check command (was for roaring backend files) --- cmd/check.go | 33 ------------- cmd/check_test.go | 23 --------- cmd/root.go | 1 - ctl/check.go | 122 ---------------------------------------------- ctl/check_test.go | 95 ------------------------------------ 5 files changed, 274 deletions(-) delete mode 100644 cmd/check.go delete mode 100644 cmd/check_test.go delete mode 100644 ctl/check.go delete mode 100644 ctl/check_test.go diff --git a/cmd/check.go b/cmd/check.go deleted file mode 100644 index f22dcd3a0..000000000 --- a/cmd/check.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2021 Molecula Corp. All rights reserved. -package cmd - -import ( - "context" - "fmt" - "io" - - "github.com/spf13/cobra" - - "github.com/molecula/featurebase/v3/ctl" -) - -var checker *ctl.CheckCommand - -func newCheckCommand(stdin io.Reader, stdout io.Writer, stderr io.Writer) *cobra.Command { - checker = ctl.NewCheckCommand(stdin, stdout, stderr) - checkCmd := &cobra.Command{ - Use: "check [path2]...", - Short: "Do a consistency check on a FeatureBase data file.", - Long: ` -Performs a consistency check on data files. -`, - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return fmt.Errorf("path required") - } - checker.Paths = args - return checker.Run(context.Background()) - }, - } - return checkCmd -} diff --git a/cmd/check_test.go b/cmd/check_test.go deleted file mode 100644 index a6abf0529..000000000 --- a/cmd/check_test.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2021 Molecula Corp. All rights reserved. -package cmd_test - -import ( - "strings" - "testing" -) - -func TestCheckHelp(t *testing.T) { - output, err := ExecNewRootCommand(t, "check", "--help") - if !strings.Contains(output, "Usage:") || - !strings.Contains(output, "Flags:") || - !strings.Contains(output, "featurebase check") || err != nil { - t.Fatalf("Command 'check --help' not working, err: '%v', output: '%s'", err, output) - } -} - -func TestCheckNoPath(t *testing.T) { - output, err := ExecNewRootCommand(t, "check") - if !strings.Contains(err.Error(), "path required") { - t.Fatalf("Command 'check' without args should error but: err: '%v', output: '%v'", err, output) - } -} diff --git a/cmd/root.go b/cmd/root.go index f32cc5875..c6c6492b3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,7 +53,6 @@ at https://docs.molecula.cloud/. rc.AddCommand(newChkSumCommand(stdin, stdout, stderr)) rc.AddCommand(newBackupCommand(stdin, stdout, stderr)) rc.AddCommand(newRestoreCommand(stdin, stdout, stderr)) - rc.AddCommand(newCheckCommand(stdin, stdout, stderr)) rc.AddCommand(newConfigCommand(stdin, stdout, stderr)) rc.AddCommand(newExportCommand(stdin, stdout, stderr)) rc.AddCommand(newGenerateConfigCommand(stdin, stdout, stderr)) diff --git a/ctl/check.go b/ctl/check.go deleted file mode 100644 index 655394757..000000000 --- a/ctl/check.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2021 Molecula Corp. All rights reserved. -package ctl - -import ( - "context" - "fmt" - "io" - "os" - "path/filepath" - "syscall" - - "github.com/molecula/featurebase/v3" - "github.com/molecula/featurebase/v3/roaring" - "github.com/pkg/errors" -) - -// CheckCommand represents a command for performing consistency checks on data files. -type CheckCommand struct { - // Data file paths. - Paths []string - - // Standard input/output - *pilosa.CmdIO -} - -// NewCheckCommand returns a new instance of CheckCommand. -func NewCheckCommand(stdin io.Reader, stdout, stderr io.Writer) *CheckCommand { - return &CheckCommand{ - CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), - } -} - -// Run executes the check command. -func (cmd *CheckCommand) Run(_ context.Context) error { - for _, path := range cmd.Paths { - switch filepath.Ext(path) { - case "": - if err := cmd.checkBitmapFile(path); err != nil { - return errors.Wrap(err, "checking bitmap") - } - - case ".cache": - if err := cmd.checkCacheFile(path); err != nil { - return errors.Wrap(err, "checking cache") - } - - case ".snapshotting": - if err := cmd.checkSnapshotFile(path); err != nil { - return errors.Wrap(err, "checking snapshot") - } - } - } - - return nil -} - -// checkBitmapFile performs a consistency check on path for a roaring bitmap file. -func (cmd *CheckCommand) checkBitmapFile(path string) (err error) { - // Open file handle. - f, err := os.Open(path) - if err != nil { - return errors.Wrap(err, "opening file") - } - defer f.Close() - - fi, err := f.Stat() - if err != nil { - return errors.Wrap(err, "statting file") - } - - // Memory map the file. - data, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()), syscall.PROT_READ, syscall.MAP_SHARED) - if err != nil { - return errors.Wrap(err, "mmapping") - } - defer func() { - e := syscall.Munmap(data) - if e != nil { - fmt.Fprintf(cmd.Stderr, "WARNING: munmap failed: %v", e) - } - // don't overwrite another error with this, but also indicate - // this error. - if err == nil { - err = e - } - }() - // Attach the mmap file to the bitmap. - bm := roaring.NewBitmap() - if err := bm.UnmarshalBinary(data); err != nil { - return errors.Wrap(err, "unmarshalling") - } - - // Perform consistency check. - if err := bm.Check(); err != nil { - // Print returned errors. - switch err := err.(type) { - case roaring.ErrorList: - for i := range err { - fmt.Fprintf(cmd.Stdout, "%s: %s\n", path, err[i].Error()) - } - default: - fmt.Fprintf(cmd.Stdout, "%s: %s\n", path, err.Error()) - } - } - - // Print success message if no errors were found. - fmt.Fprintf(cmd.Stdout, "%s: ok\n", path) - - return nil -} - -// checkCacheFile performs a consistency check on path for a cache file. -func (cmd *CheckCommand) checkCacheFile(path string) error { - fmt.Fprintf(cmd.Stderr, "%s: ignoring cache file\n", path) - return nil -} - -// checkSnapshotFile performs a consistency check on path for a snapshot file. -func (cmd *CheckCommand) checkSnapshotFile(path string) error { - fmt.Fprintf(cmd.Stderr, "%s: ignoring snapshot file\n", path) - return nil -} diff --git a/ctl/check_test.go b/ctl/check_test.go deleted file mode 100644 index 229fcff44..000000000 --- a/ctl/check_test.go +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2021 Molecula Corp. All rights reserved. -package ctl - -import ( - "bytes" - "io" - "os" - "strings" - "testing" - - "context" - - "github.com/molecula/featurebase/v3/testhook" -) - -func TestCheckCommand_RunCacheFile(t *testing.T) { - fi, err := testhook.TempFile(t, "test*.cache") - if err != nil { - t.Fatalf("creating test file: %v", err) - } - cacheFile := fi.Name() - - rder := []byte{} - stdin := bytes.NewReader(rder) - r, w, _ := os.Pipe() - cm := NewCheckCommand(stdin, w, w) - cm.Paths = []string{cacheFile} - - err = cm.Run(context.Background()) - w.Close() - var buf bytes.Buffer - if _, err := io.Copy(&buf, r); err != nil { - t.Fatalf("copy: %v", err) - } - - if !strings.Contains(buf.String(), "ignoring cache file") { - t.Fatalf("expect: ignoring cache file, actual: '%s'", err) - } -} - -func TestCheckCommand_RunSnapshot(t *testing.T) { - fi, err := testhook.TempFile(t, "test*.snapshotting") - if err != nil { - t.Fatalf("creating test file: %v", err) - } - snapshotFile := fi.Name() - - rder := []byte{} - stdin := bytes.NewReader(rder) - r, w, _ := os.Pipe() - cm := NewCheckCommand(stdin, w, w) - cm.Paths = []string{snapshotFile} - - err = cm.Run(context.Background()) - w.Close() - var buf bytes.Buffer - if _, err := io.Copy(&buf, r); err != nil { - t.Fatalf("copy: %v", err) - } - - if !strings.Contains(buf.String(), "ignoring snapshot file") { - t.Fatalf("expect: ignoring snapshot file, actual: '%s'", err) - } -} - -func TestCheckCommand_Run(t *testing.T) { - file, err := testhook.TempFile(t, "run-command") - if err != nil { - t.Fatal(err) - } - fname := file.Name() - if _, err := file.Write([]byte("1234,1223")); err != nil { - t.Fatalf("writing to temp file: %v", err) - } - file.Close() - - rder := []byte{} - stdin := bytes.NewReader(rder) - r, w, _ := os.Pipe() - cm := NewCheckCommand(stdin, w, w) - cm.Paths = []string{fname} - - err = cm.Run(context.Background()) - w.Close() - var buf bytes.Buffer - if _, err := io.Copy(&buf, r); err != nil { - t.Fatalf("copy: %v", err) - } - - expectedPrefix := "checking bitmap: unmarshalling: " - if !strings.HasPrefix(err.Error(), expectedPrefix) { - t.Fatalf("expect error: '%s...', actual: '%s'", expectedPrefix, err) - } - // Todo: need correct roaring file for happy path -}