[#316] locode/csv: Scan subdivision csv-table into memory
Scanning subdivision csv-table entries one-by-one takes significant time and system resources. To speed up random access to table records, on the first call, the table is pumped into memory (map). On subsequent calls, I/O operations are not performed. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
307355f165
commit
977229eb5a
2 changed files with 57 additions and 16 deletions
|
@ -44,25 +44,61 @@ func (t *Table) IterateAll(f func(locode.Record) error) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubDivName scans table record one-by-one, and returns subdivision name
|
const (
|
||||||
// on country and subdivision codes match.
|
_ = iota - 1
|
||||||
|
|
||||||
|
subDivCountry
|
||||||
|
subDivSubdivision
|
||||||
|
subDivName
|
||||||
|
_ // subDivLevel
|
||||||
|
|
||||||
|
subDivFldNum
|
||||||
|
)
|
||||||
|
|
||||||
|
type subDivKey struct {
|
||||||
|
countryCode,
|
||||||
|
subDivCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
type subDivRecord struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubDivName scans table record to in-memory table (once),
|
||||||
|
// and returns subdivision name on country and subdivision codes match.
|
||||||
//
|
//
|
||||||
// Returns locodedb.ErrSubDivNotFound if no entry matches.
|
// Returns locodedb.ErrSubDivNotFound if no entry matches.
|
||||||
func (t *Table) SubDivName(countryCode *locodedb.CountryCode, name string) (subDiv string, err error) {
|
func (t *Table) SubDivName(countryCode *locodedb.CountryCode, code string) (string, error) {
|
||||||
const wordsPerRecord = 4
|
if err := t.initSubDiv(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
err = t.scanWords([]string{t.subDivPath}, wordsPerRecord, func(words []string) error {
|
rec, ok := t.mSubDiv[subDivKey{
|
||||||
if words[0] == countryCode.String() && words[1] == name {
|
countryCode: countryCode.String(),
|
||||||
subDiv = words[2]
|
subDivCode: code,
|
||||||
return errScanInt
|
}]
|
||||||
|
if !ok {
|
||||||
|
return "", locodedb.ErrSubDivNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return rec.name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table) initSubDiv() (err error) {
|
||||||
|
t.subDivOnce.Do(func() {
|
||||||
|
t.mSubDiv = make(map[subDivKey]subDivRecord)
|
||||||
|
|
||||||
|
err = t.scanWords([]string{t.subDivPath}, subDivFldNum, func(words []string) error {
|
||||||
|
t.mSubDiv[subDivKey{
|
||||||
|
countryCode: words[subDivCountry],
|
||||||
|
subDivCode: words[subDivSubdivision],
|
||||||
|
}] = subDivRecord{
|
||||||
|
name: words[subDivName],
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
})
|
||||||
if err == nil && subDiv == "" {
|
|
||||||
err = locodedb.ErrSubDivNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package csvlocode
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Prm groups the required parameters of the Table's constructor.
|
// Prm groups the required parameters of the Table's constructor.
|
||||||
|
@ -34,6 +35,10 @@ type Table struct {
|
||||||
mode os.FileMode
|
mode os.FileMode
|
||||||
|
|
||||||
subDivPath string
|
subDivPath string
|
||||||
|
|
||||||
|
subDivOnce sync.Once
|
||||||
|
|
||||||
|
mSubDiv map[subDivKey]subDivRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
|
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
|
||||||
|
|
Loading…
Reference in a new issue