diff --git a/cmd/influx_inspect/dumptsi/dumptsi.go b/cmd/influx_inspect/dumptsi/dumptsi.go index f58b4edabad..441aa98b581 100644 --- a/cmd/influx_inspect/dumptsi/dumptsi.go +++ b/cmd/influx_inspect/dumptsi/dumptsi.go @@ -127,11 +127,20 @@ func (cmd *Command) run() error { return err } + if cmd.showSeries { + if err := cmd.printSeries(sfile); err != nil { + return err + } + } + // If this is an ad-hoc fileset then process it and close afterward. if fs != nil { defer fs.Release() defer fs.Close() - return cmd.printFileSet(sfile, fs) + if cmd.showSeries || cmd.showMeasurements { + return cmd.printMeasurements(sfile, fs) + } + return cmd.printFileSummaries(fs) } // Otherwise iterate over each partition in the index. @@ -143,26 +152,15 @@ func (cmd *Command) run() error { return err } defer fs.Release() - return cmd.printFileSet(sfile, fs) - }(); err != nil { - return err - } - } - return nil -} -func (cmd *Command) printFileSet(sfile *tsdb.SeriesFile, fs *tsi1.FileSet) error { - // Show either raw data or summary stats. - if cmd.showSeries || cmd.showMeasurements { - if err := cmd.printMerged(sfile, fs); err != nil { - return err - } - } else { - if err := cmd.printFileSummaries(fs); err != nil { + if cmd.showSeries || cmd.showMeasurements { + return cmd.printMeasurements(sfile, fs) + } + return cmd.printFileSummaries(fs) + }(); err != nil { return err } } - return nil } @@ -173,6 +171,13 @@ func (cmd *Command) readFileSet(sfile *tsdb.SeriesFile) (*tsi1.Index, *tsi1.File if err != nil { return nil, nil, err } else if fi.IsDir() { + // Verify directory is an index before opening it. + if ok, err := tsi1.IsIndexDir(cmd.paths[0]); err != nil { + return nil, nil, err + } else if !ok { + return nil, nil, fmt.Errorf("Not an index directory: %q", cmd.paths[0]) + } + idx := tsi1.NewIndex(sfile, "", tsi1.WithPath(cmd.paths[0]), @@ -218,15 +223,6 @@ func (cmd *Command) readFileSet(sfile *tsdb.SeriesFile) (*tsi1.Index, *tsi1.File return nil, fs, nil } -func (cmd *Command) printMerged(sfile *tsdb.SeriesFile, fs *tsi1.FileSet) error { - if err := cmd.printSeries(sfile); err != nil { - return err - } else if err := cmd.printMeasurements(sfile, fs); err != nil { - return err - } - return nil -} - func (cmd *Command) printSeries(sfile *tsdb.SeriesFile) error { if !cmd.showSeries { return nil diff --git a/tsdb/index/tsi1/index.go b/tsdb/index/tsi1/index.go index 52347a00baf..c1576b45fdc 100644 --- a/tsdb/index/tsi1/index.go +++ b/tsdb/index/tsi1/index.go @@ -3,6 +3,7 @@ package tsi1 import ( "errors" "fmt" + "io/ioutil" "os" "path/filepath" "regexp" @@ -880,3 +881,21 @@ func (i *Index) SetFieldName(measurement []byte, name string) {} // Rebuild rebuilds an index. It's a no-op for this index. func (i *Index) Rebuild() {} + +// IsIndexDir returns true if directory contains at least one partition directory. +func IsIndexDir(path string) (bool, error) { + fis, err := ioutil.ReadDir(path) + if err != nil { + return false, err + } + for _, fi := range fis { + if !fi.IsDir() { + continue + } else if ok, err := IsPartitionDir(filepath.Join(path, fi.Name())); err != nil { + return false, err + } else if ok { + return true, nil + } + } + return false, nil +} diff --git a/tsdb/index/tsi1/partition.go b/tsdb/index/tsi1/partition.go index 02406ed9e86..31eedebf004 100644 --- a/tsdb/index/tsi1/partition.go +++ b/tsdb/index/tsi1/partition.go @@ -1276,3 +1276,13 @@ const MaxIndexMergeCount = 2 // MaxIndexFileSize is the maximum expected size of an index file. const MaxIndexFileSize = 4 * (1 << 30) + +// IsPartitionDir returns true if directory contains a MANIFEST file. +func IsPartitionDir(path string) (bool, error) { + if _, err := os.Stat(filepath.Join(path, ManifestFileName)); os.IsNotExist(err) { + return false, nil + } else if err != nil { + return false, err + } + return true, nil +}