2021-02-08 18:25:17 +00:00
|
|
|
package locodebolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-02-08 18:25:17 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
|
|
|
locodedb "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode/db"
|
2021-02-08 18:25:17 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Open opens an underlying BoltDB instance.
|
2021-02-09 15:12:09 +00:00
|
|
|
//
|
|
|
|
// Timeout of BoltDB opening is 3s (only for Linux or Darwin).
|
2021-02-09 15:18:29 +00:00
|
|
|
//
|
|
|
|
// Opens BoltDB in read-only mode if DB is read-only.
|
2021-02-08 18:25:17 +00:00
|
|
|
func (db *DB) Open() error {
|
|
|
|
// copy-paste from metabase:
|
|
|
|
// consider universal Open/Close for BoltDB wrappers
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
err := util.MkdirAllX(filepath.Dir(db.path), db.mode)
|
2021-02-08 18:25:17 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not create dir for BoltDB: %w", err)
|
2021-02-08 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.bolt, err = bbolt.Open(db.path, db.mode, db.boltOpts)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not open BoltDB: %w", err)
|
2021-02-08 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Close closes an underlying BoltDB instance.
|
2021-02-08 18:25:17 +00:00
|
|
|
//
|
|
|
|
// Must not be called before successful Open call.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.bolt.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func countryBucketKey(cc *locodedb.CountryCode) ([]byte, error) {
|
|
|
|
return []byte(cc.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func locationBucketKey(lc *locodedb.LocationCode) ([]byte, error) {
|
|
|
|
return []byte(lc.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type recordJSON struct {
|
2021-02-10 10:20:39 +00:00
|
|
|
CountryName string
|
|
|
|
LocationName string
|
|
|
|
SubDivName string
|
|
|
|
SubDivCode string
|
|
|
|
Latitude float64
|
|
|
|
Longitude float64
|
|
|
|
Continent string
|
2021-02-08 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func recordValue(r locodedb.Record) ([]byte, error) {
|
|
|
|
p := r.GeoPoint()
|
|
|
|
|
|
|
|
rj := &recordJSON{
|
2021-02-10 10:20:39 +00:00
|
|
|
CountryName: r.CountryName(),
|
|
|
|
LocationName: r.LocationName(),
|
|
|
|
SubDivName: r.SubDivName(),
|
|
|
|
SubDivCode: r.SubDivCode(),
|
|
|
|
Latitude: p.Latitude(),
|
|
|
|
Longitude: p.Longitude(),
|
|
|
|
Continent: r.Continent().String(),
|
2021-02-08 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(rj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func recordFromValue(data []byte) (*locodedb.Record, error) {
|
|
|
|
rj := new(recordJSON)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, rj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(locodedb.Record)
|
|
|
|
r.SetCountryName(rj.CountryName)
|
2021-02-10 10:20:39 +00:00
|
|
|
r.SetLocationName(rj.LocationName)
|
2021-02-08 18:25:17 +00:00
|
|
|
r.SetSubDivName(rj.SubDivName)
|
|
|
|
r.SetSubDivCode(rj.SubDivCode)
|
|
|
|
r.SetGeoPoint(locodedb.NewPoint(rj.Latitude, rj.Longitude))
|
|
|
|
|
|
|
|
cont := locodedb.ContinentFromString(rj.Continent)
|
|
|
|
r.SetContinent(&cont)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put saves the record by key in an underlying BoltDB instance.
|
2021-02-08 18:25:17 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Country code from the key is used for allocating the 1st level buckets.
|
|
|
|
// Records are stored in country buckets by the location code from the key.
|
2021-02-08 18:25:17 +00:00
|
|
|
// The records are stored in internal binary JSON format.
|
|
|
|
//
|
|
|
|
// Must not be called before successful Open call.
|
2021-02-09 15:18:29 +00:00
|
|
|
// Must not be called in read-only mode: behavior is undefined.
|
2021-02-08 18:25:17 +00:00
|
|
|
func (db *DB) Put(key locodedb.Key, rec locodedb.Record) error {
|
2023-05-03 13:23:58 +00:00
|
|
|
return db.bolt.Batch(func(tx *bbolt.Tx) error {
|
2021-02-08 18:25:17 +00:00
|
|
|
countryKey, err := countryBucketKey(key.CountryCode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bktCountry, err := tx.CreateBucketIfNotExists(countryKey)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not create country bucket: %w", err)
|
2021-02-08 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
locationKey, err := locationBucketKey(key.LocationCode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cont, err := recordValue(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bktCountry.Put(locationKey, cont)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var errRecordNotFound = errors.New("record not found")
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// Get reads the record by key from underlying BoltDB instance.
|
2021-02-08 18:25:17 +00:00
|
|
|
//
|
|
|
|
// Returns an error if no record is presented by key in DB.
|
|
|
|
//
|
|
|
|
// Must not be called before successful Open call.
|
|
|
|
func (db *DB) Get(key locodedb.Key) (rec *locodedb.Record, err error) {
|
|
|
|
err = db.bolt.View(func(tx *bbolt.Tx) error {
|
|
|
|
countryKey, err := countryBucketKey(key.CountryCode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bktCountry := tx.Bucket(countryKey)
|
|
|
|
if bktCountry == nil {
|
|
|
|
return errRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
locationKey, err := locationBucketKey(key.LocationCode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := bktCountry.Get(locationKey)
|
|
|
|
if data == nil {
|
|
|
|
return errRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
rec, err = recordFromValue(data)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|