locode: Add command to list all locodes #24

Merged
fyrchik merged 2 commits from acid-ant/frostfs-locode-db:feature/list-all-locodes into master 2025-03-12 08:48:24 +00:00
4 changed files with 66 additions and 4 deletions

View file

@ -7,8 +7,9 @@ import (
)
const (
locodeInfoDBFlag = "db"
locodeInfoCodeFlag = "locode"
locodeInfoDBFlag = "db"
locodeInfoDBFlagDesc = "Path to FrostFS UN/LOCODE database"
locodeInfoCodeFlag = "locode"
)
var (
@ -47,7 +48,7 @@ var (
func initUtilLocodeInfoCmd() {
flags := locodeInfoCmd.Flags()
flags.StringVar(&locodeInfoDBPath, locodeInfoDBFlag, "", "Path to FrostFS UN/LOCODE database")
flags.StringVar(&locodeInfoDBPath, locodeInfoDBFlag, "", locodeInfoDBFlagDesc)
_ = locodeInfoCmd.MarkFlagRequired(locodeInfoDBFlag)
flags.StringVar(&locodeInfoCode, locodeInfoCodeFlag, "", "UN/LOCODE")

37
locode_list.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
locodedb "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode/db"
locodebolt "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode/db/boltdb"
"github.com/spf13/cobra"
)
var (
locodeListCmd = &cobra.Command{
Use: "list",
Short: "Print all locodes from FrostFS database",
Run: func(cmd *cobra.Command, _ []string) {
targetDB := locodebolt.New(locodebolt.Prm{
Path: locodeInfoDBPath,
}, locodebolt.ReadOnly())
err := targetDB.Open()
ExitOnErr(cmd, "", err)
defer targetDB.Close()
err = targetDB.IterateOverLocodes(func(locode string, geoPoint locodedb.Point) {
cmd.Printf("%s\t %0.2f %0.2f\n", locode, geoPoint.Latitude(), geoPoint.Longitude())
})
ExitOnErr(cmd, "", err)
},
}
)
func initUtilLocodeListCmd() {
flags := locodeListCmd.Flags()
flags.StringVar(&locodeInfoDBPath, locodeInfoDBFlag, "", locodeInfoDBFlagDesc)
_ = locodeListCmd.MarkFlagRequired(locodeInfoDBFlag)
}

View file

@ -35,9 +35,13 @@ func ExitOnErr(cmd *cobra.Command, errFmt string, err error) {
}
func main() {
// use stdout as default output for cmd.Print()
rootCmd.SetOut(os.Stdout)
initUtilLocodeGenerateCmd()
initUtilLocodeInfoCmd()
rootCmd.AddCommand(locodeGenerateCmd, locodeInfoCmd)
initUtilLocodeListCmd()
rootCmd.AddCommand(locodeGenerateCmd, locodeInfoCmd, locodeListCmd)
err := rootCmd.Execute()
if err != nil {
ExitOnErr(rootCmd, "", err)

View file

@ -164,3 +164,23 @@ func (db *DB) Get(key locodedb.Key) (rec *locodedb.Record, err error) {
return
}
// IterateOverLocodes iterates over all locodes.
//
// Returns an error if unable to unmarshal data from DB.
//
// Must not be called before successful Open call.
func (db *DB) IterateOverLocodes(f func(string, locodedb.Point)) error {

locodedb.Point required for geoDistance metric in frostfs-node.

`locodedb.Point` required for `geoDistance` metric in `frostfs-node`.
return db.bolt.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(cname []byte, bktCountry *bbolt.Bucket) error {
return bktCountry.ForEach(func(k, v []byte) error {
rec, err := recordFromValue(v)
if err != nil {
return err
}
f(fmt.Sprintf("%s %s", cname, k), *rec.GeoPoint())
return nil
})
})
})
}