2021-02-08 17:27:19 +00:00
|
|
|
package locodedb
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-03 13:23:58 +00:00
|
|
|
"runtime"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode"
|
2023-05-03 13:23:58 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-02-08 17:27:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SourceTable is an interface of the UN/LOCODE table.
|
|
|
|
type SourceTable interface {
|
|
|
|
// Must iterate over all entries of the table
|
|
|
|
// and pass next entry to the handler.
|
|
|
|
//
|
|
|
|
// Must return handler's errors directly.
|
|
|
|
IterateAll(func(locode.Record) error) error
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// DB is an interface of FrostFS location database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type DB interface {
|
|
|
|
// Must save the record by key in the database.
|
|
|
|
Put(Key, Record) error
|
|
|
|
|
|
|
|
// Must return the record by key from the database.
|
|
|
|
Get(Key) (*Record, error)
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// AirportRecord represents the entry in FrostFS airport database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type AirportRecord struct {
|
|
|
|
// Name of the country where airport is located.
|
|
|
|
CountryName string
|
|
|
|
|
|
|
|
// Geo point where airport is located.
|
|
|
|
Point *Point
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrAirportNotFound is returned by AirportRecord readers
|
|
|
|
// when the required airport is not found.
|
|
|
|
var ErrAirportNotFound = errors.New("airport not found")
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// AirportDB is an interface of FrostFS airport database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type AirportDB interface {
|
|
|
|
// Must return the record by UN/LOCODE table record.
|
|
|
|
//
|
|
|
|
// Must return ErrAirportNotFound if there is no
|
|
|
|
// related airport in the database.
|
|
|
|
Get(locode.Record) (*AirportRecord, error)
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// ContinentsDB is an interface of FrostFS continent database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type ContinentsDB interface {
|
|
|
|
// Must return continent of the geo point.
|
|
|
|
PointContinent(*Point) (*Continent, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrSubDivNotFound = errors.New("subdivision not found")
|
|
|
|
|
|
|
|
var ErrCountryNotFound = errors.New("country not found")
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// NamesDB is an interface of the FrostFS location namespace.
|
2021-02-08 17:27:19 +00:00
|
|
|
type NamesDB interface {
|
2022-04-21 11:28:05 +00:00
|
|
|
// Must resolve a country code to a country name.
|
2021-02-08 17:27:19 +00:00
|
|
|
//
|
|
|
|
// Must return ErrCountryNotFound if there is no
|
2022-04-21 11:28:05 +00:00
|
|
|
// country with the provided code.
|
2021-02-08 17:27:19 +00:00
|
|
|
CountryName(*CountryCode) (string, error)
|
|
|
|
|
|
|
|
// Must resolve (country code, subdivision code) to
|
2022-04-21 11:28:05 +00:00
|
|
|
// a subdivision name.
|
2021-02-08 17:27:19 +00:00
|
|
|
//
|
|
|
|
// Must return ErrSubDivNotFound if either country or
|
|
|
|
// subdivision is not presented in database.
|
|
|
|
SubDivName(*CountryCode, string) (string, error)
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// FillDatabase generates the FrostFS location database based on the UN/LOCODE table.
|
2021-02-08 17:27:19 +00:00
|
|
|
func FillDatabase(table SourceTable, airports AirportDB, continents ContinentsDB, names NamesDB, db DB) error {
|
2023-05-03 13:23:58 +00:00
|
|
|
var errG errgroup.Group
|
|
|
|
|
|
|
|
// Pick some sane default, after this the performance stopped increasing.
|
|
|
|
errG.SetLimit(runtime.NumCPU() * 4)
|
|
|
|
_ = table.IterateAll(func(tableRecord locode.Record) error {
|
|
|
|
errG.Go(func() error {
|
|
|
|
return processTableRecord(tableRecord, airports, continents, names, db)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return errG.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func processTableRecord(tableRecord locode.Record, airports AirportDB, continents ContinentsDB, names NamesDB, db DB) error {
|
|
|
|
if tableRecord.LOCODE.LocationCode() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dbKey, err := NewKey(tableRecord.LOCODE)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbRecord, err := NewRecord(tableRecord)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errParseCoordinates) {
|
2021-02-08 17:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
geoPoint := dbRecord.GeoPoint()
|
|
|
|
countryName := ""
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
if geoPoint == nil {
|
|
|
|
airportRecord, err := airports.Get(tableRecord)
|
2021-02-08 17:27:19 +00:00
|
|
|
if err != nil {
|
2023-05-03 13:23:58 +00:00
|
|
|
if errors.Is(err, ErrAirportNotFound) {
|
2021-02-08 17:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
geoPoint = airportRecord.Point
|
|
|
|
countryName = airportRecord.CountryName
|
|
|
|
}
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
dbRecord.SetGeoPoint(geoPoint)
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
if countryName == "" {
|
|
|
|
countryName, err = names.CountryName(dbKey.CountryCode())
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrCountryNotFound) {
|
|
|
|
return nil
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
return err
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
2023-05-03 13:23:58 +00:00
|
|
|
}
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
dbRecord.SetCountryName(countryName)
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
if subDivCode := dbRecord.SubDivCode(); subDivCode != "" {
|
|
|
|
subDivName, err := names.SubDivName(dbKey.CountryCode(), subDivCode)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrSubDivNotFound) {
|
|
|
|
return nil
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
return err
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
dbRecord.SetSubDivName(subDivName)
|
|
|
|
}
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
continent, err := continents.PointContinent(geoPoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not calculate continent geo point: %w", err)
|
|
|
|
} else if continent.Is(ContinentUnknown) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-08 17:27:19 +00:00
|
|
|
|
2023-05-03 13:23:58 +00:00
|
|
|
dbRecord.SetContinent(continent)
|
|
|
|
|
|
|
|
return db.Put(*dbKey, *dbRecord)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// LocodeRecord returns the record from the FrostFS location database
|
2022-04-21 11:28:05 +00:00
|
|
|
// corresponding to the string representation of UN/LOCODE.
|
2021-02-08 17:27:19 +00:00
|
|
|
func LocodeRecord(db DB, sLocode string) (*Record, error) {
|
|
|
|
lc, err := locode.FromString(sLocode)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse locode: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := NewKey(*lc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.Get(*key)
|
|
|
|
}
|