github.com/m3db/m3@v1.5.0/src/cmd/tools/read_index_ids/main/main.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"log"
    27  	"os"
    28  
    29  	"github.com/m3db/m3/src/cmd/tools"
    30  	"github.com/m3db/m3/src/dbnode/persist/fs"
    31  	"github.com/m3db/m3/src/x/ident"
    32  	xtime "github.com/m3db/m3/src/x/time"
    33  
    34  	"github.com/pborman/getopt"
    35  	"go.uber.org/zap"
    36  )
    37  
    38  func main() {
    39  	var (
    40  		optPathPrefix = getopt.StringLong("path-prefix", 'p', "", "Path prefix [e.g. /var/lib/m3db]")
    41  		optNamespace  = getopt.StringLong("namespace", 'n', "", "Namespace [e.g. metrics]")
    42  		optShard      = getopt.Uint32Long("shard", 's', 0, "Shard ID [expected format uint32]")
    43  		optBlockstart = getopt.Int64Long("block-start", 'b', 0, "Block Start Time [in nsec]")
    44  	)
    45  	getopt.Parse()
    46  
    47  	rawLogger, err := zap.NewDevelopment()
    48  	if err != nil {
    49  		log.Fatalf("unable to create logger: %+v", err)
    50  	}
    51  	log := rawLogger.Sugar()
    52  
    53  	if *optPathPrefix == "" ||
    54  		*optNamespace == "" ||
    55  		*optShard < 0 ||
    56  		*optBlockstart <= 0 {
    57  		getopt.Usage()
    58  		os.Exit(1)
    59  	}
    60  
    61  	bytesPool := tools.NewCheckedBytesPool()
    62  	bytesPool.Init()
    63  
    64  	fsOpts := fs.NewOptions().SetFilePathPrefix(*optPathPrefix)
    65  	reader, err := fs.NewReader(bytesPool, fsOpts)
    66  	if err != nil {
    67  		log.Fatalf("could not create new reader: %v", err)
    68  	}
    69  	openOpts := fs.DataReaderOpenOptions{
    70  		Identifier: fs.FileSetFileIdentifier{
    71  			Namespace:  ident.StringID(*optNamespace),
    72  			Shard:      *optShard,
    73  			BlockStart: xtime.UnixNano(*optBlockstart),
    74  		},
    75  	}
    76  	err = reader.Open(openOpts)
    77  	if err != nil {
    78  		log.Fatalf("unable to open reader: %v", err)
    79  	}
    80  
    81  	for {
    82  		id, _, _, _, err := reader.ReadMetadata()
    83  		if err == io.EOF {
    84  			break
    85  		}
    86  		if err != nil {
    87  			log.Fatalf("err reading metadata: %v", err)
    88  		}
    89  		// Use fmt package so it goes to stdout instead of stderr
    90  		fmt.Println(id.String())
    91  	}
    92  }