diff options
Diffstat (limited to 'pkg/localstore')
| -rw-r--r-- | pkg/localstore/import.go | 484 | ||||
| -rw-r--r-- | pkg/localstore/store.go | 155 |
2 files changed, 36 insertions, 603 deletions
diff --git a/pkg/localstore/import.go b/pkg/localstore/import.go deleted file mode 100644 index eb008ba..0000000 --- a/pkg/localstore/import.go +++ /dev/null @@ -1,484 +0,0 @@ -package localstore - -import ( - "encoding/csv" - "errors" - "fmt" - "io" - "os" - "path/filepath" - "strconv" - "strings" - "time" - - "airlines/pkg/model" - - "github.com/schollz/progressbar/v3" -) - -func (s *Store) ImportAllCSVs(dir string) error { - if dir == "" { - return errors.New("empty directory path") - } - if !strings.HasSuffix(dir, string(filepath.Separator)) { - dir += string(filepath.Separator) - } - - // lock for writes while rebuilding everything - s.mu.Lock() - defer s.mu.Unlock() - - // reset containers - s.users = nil - s.cards = nil - s.flights = nil - if s.userFlights == nil { - s.userFlights = make(map[uint64]map[uint64]struct{}) - } else { - for k := range s.userFlights { - delete(s.userFlights, k) - } - } - if s.cardFlights == nil { - s.cardFlights = make(map[uint64]map[uint64]struct{}) - } else { - for k := range s.cardFlights { - delete(s.cardFlights, k) - } - } - // (Re)build helper indices when possible - if s.cardsByUser == nil { - s.cardsByUser = make(map[uint64]map[uint64]struct{}) - } else { - for k := range s.cardsByUser { - delete(s.cardsByUser, k) - } - } - // We cannot reconstruct codesByUser / countriesByUser from CSVs here; leave as-is or empty. - // Initialize if nil so your code using them won't panic. - if s.codesByUser == nil { - s.codesByUser = make(map[uint64]map[string]struct{}) - } - if s.countriesByUser == nil { - s.countriesByUser = make(map[uint64]map[string]struct{}) - } - - // 1) users.csv - if err := s.loadUsersCSV(dir + "users.csv"); err != nil { - return fmt.Errorf("load users.csv: %w", err) - } - fmt.Println("loaed users") - - // 2) cards.csv - if err := s.loadCardsCSV(dir + "cards.csv"); err != nil { - return fmt.Errorf("load cards.csv: %w", err) - } - - // 3) flights.csv - if err := s.loadFlightsCSV(dir + "flights.csv"); err != nil { - return fmt.Errorf("load flights.csv: %w", err) - } - - // 4) user_flights.csv - if err := s.loadUserFlightsCSV(dir + "user_flights.csv"); err != nil { - return fmt.Errorf("load user_flights.csv: %w", err) - } - - // 5) card_flights.csv - if err := s.loadCardFlightsCSV(dir + "card_flights.csv"); err != nil { - return fmt.Errorf("load card_flights.csv: %w", err) - } - - return nil -} - -func (s *Store) loadUsersCSV(path string) error { - r, closer, err := openCSV(path) - if err != nil { - return err - } - defer closer.Close() - - // header - if _, err := r.Read(); err != nil { - return fmt.Errorf("users header: %w", err) - } - - bar := progressbar.Default(int64(150000), "reading users") - - for { - bar.Add(1) - rec, err := r.Read() - if errors.Is(err, io.EOF) { - break - } - if errors.Is(err, io.EOF) { - break - } - if err != nil { - return fmt.Errorf("users read: %w", err) - } - // columns: id, nick, name, surname, fathersname, sex, birthday, total_flights, total_codes, total_countries, total_cards - if len(rec) < 11 { - return fmt.Errorf("users row has %d columns, expected >=11", len(rec)) - } - - id, err := parseUint(rec[0]) - if err != nil { - return fmt.Errorf("user id: %w", err) - } - sexInt, err := parseInt(rec[5]) - if err != nil { - return fmt.Errorf("user sex: %w", err) - } - - var bday time.Time - if strings.TrimSpace(rec[6]) != "" { - // "2006-01-02" in UTC - t, err := time.Parse("2006-01-02", rec[6]) - if err != nil { - return fmt.Errorf("user birthday: %w", err) - } - bday = t.UTC() - } else { - // keep zero time; or: - // bday = model.SentinelBirthday() // <-- if you prefer sentinel - } - - u := &model.User{ - ID: id, - Nick: rec[1], - Name: rec[2], - Surname: rec[3], - Fathersname: strings.TrimSpace(rec[4]), - Sex: model.Sex(sexInt), // adjust if your type differs - Birthday: bday, - } - - s.putUser(u) - } - return nil -} - -func (s *Store) loadCardsCSV(path string) error { - r, closer, err := openCSV(path) - if err != nil { - return err - } - defer closer.Close() - - // header - if _, err := r.Read(); err != nil { - return fmt.Errorf("cards header: %w", err) - } - - bar := progressbar.Default(int64(177000), "reading cards") - for { - bar.Add(1) - rec, err := r.Read() - if errors.Is(err, io.EOF) { - break - } - if err != nil { - return fmt.Errorf("cards read: %w", err) - } - // columns: id, prefix, number, bonusprogramm, user_id - if len(rec) < 5 { - return fmt.Errorf("cards row has %d columns, expected >=5", len(rec)) - } - - id, err := parseUint(rec[0]) - if err != nil { - return fmt.Errorf("card id: %w", err) - } - num, err := parseUint(rec[2]) - if err != nil { - return fmt.Errorf("card number: %w", err) - } - uid, err := parseUint(rec[4]) - if err != nil { - return fmt.Errorf("card user_id: %w", err) - } - - c := &model.Card{ - ID: id, - Prefix: rec[1], - Number: num, - Bonusprogramm: rec[3], - UserID: uid, - } - s.putCard(c) - - // index: cardsByUser - if _, ok := s.cardsByUser[uid]; !ok { - s.cardsByUser[uid] = make(map[uint64]struct{}) - } - s.cardsByUser[uid][id] = struct{}{} - } - return nil -} - -func (s *Store) loadFlightsCSV(path string) error { - r, closer, err := openCSV(path) - if err != nil { - return err - } - defer closer.Close() - - // header - if _, err := r.Read(); err != nil { - return fmt.Errorf("flights header: %w", err) - } - - bar := progressbar.Default(int64(2000000), "reading flights") - for { - bar.Add(1) - rec, err := r.Read() - if errors.Is(err, io.EOF) { - break - } - if err != nil { - return fmt.Errorf("flights read: %w", err) - } - // columns: - // id, number, from, to, fromlat, fromlon, tolat, tolon, dep_date, has_time, dep_time, dep_iso - if len(rec) < 12 { - return fmt.Errorf("flights row has %d columns, expected >=12", len(rec)) - } - - id, err := parseUint(rec[0]) - if err != nil { - return fmt.Errorf("flight id: %w", err) - } - - fromLat, err := parseFloat(rec[4]) - if err != nil { - return fmt.Errorf("fromlat: %w", err) - } - fromLon, err := parseFloat(rec[5]) - if err != nil { - return fmt.Errorf("fromlon: %w", err) - } - toLat, err := parseFloat(rec[6]) - if err != nil { - return fmt.Errorf("tolat: %w", err) - } - toLon, err := parseFloat(rec[7]) - if err != nil { - return fmt.Errorf("tolon: %w", err) - } - - depDateStr := strings.TrimSpace(rec[8]) // "2006-01-02" - hasTime, err := strconv.ParseBool(rec[9]) - if err != nil { - return fmt.Errorf("has_time: %w", err) - } - - var dep time.Time - if hasTime { - // When exported with time present, dep_iso is RFC3339; prefer it for full fidelity. - depISO := strings.TrimSpace(rec[11]) - if depISO != "" { - t, err := time.Parse(time.RFC3339, depISO) - if err != nil { - return fmt.Errorf("dep_iso: %w", err) - } - dep = t - } else { - // Fallback: combine dep_date + dep_time in local (treat as UTC if not specified) - depTime := strings.TrimSpace(rec[10]) // "15:04:05" - t, err := time.Parse("2006-01-02 15:04:05", depDateStr+" "+depTime) - if err != nil { - return fmt.Errorf("dep_date+dep_time: %w", err) - } - dep = t.UTC() - } - } else { - // Date only → set at UTC midnight of that date - if depDateStr == "" { - return fmt.Errorf("dep_date is empty while has_time=false") - } - t, err := time.Parse("2006-01-02", depDateStr) - if err != nil { - return fmt.Errorf("dep_date: %w", err) - } - dep = t.UTC() - } - - f := &model.Flight{ - ID: id, - Number: rec[1], - From: rec[2], - To: rec[3], - FromCoords: model.LatLong{ - Lat: fromLat, - Long: fromLon, - }, - ToCoords: model.LatLong{ - Lat: toLat, - Long: toLon, - }, - Date: dep, - HasTime: hasTime, - } - - s.putFlight(f) - } - return nil -} - -func (s *Store) loadUserFlightsCSV(path string) error { - r, closer, err := openCSV(path) - if err != nil { - return err - } - defer closer.Close() - - // header - if _, err := r.Read(); err != nil { - return fmt.Errorf("user_flights header: %w", err) - } - - bar := progressbar.Default(int64(3200000), "reading u-flights") - for { - bar.Add(1) - rec, err := r.Read() - if errors.Is(err, io.EOF) { - break - } - if err != nil { - return fmt.Errorf("user_flights read: %w", err) - } - // columns: user_id, flight_id - if len(rec) < 2 { - return fmt.Errorf("user_flights row has %d columns, expected >=2", len(rec)) - } - uid, err := parseUint(rec[0]) - if err != nil { - return fmt.Errorf("user_id: %w", err) - } - fid, err := parseUint(rec[1]) - if err != nil { - return fmt.Errorf("flight_id: %w", err) - } - - // guard against missing references (mirror your exporter’s checks) - if !s.validFlightID(fid) { - continue - } - if _, ok := s.userFlights[uid]; !ok { - s.userFlights[uid] = make(map[uint64]struct{}) - } - s.userFlights[uid][fid] = struct{}{} - } - return nil -} - -func (s *Store) loadCardFlightsCSV(path string) error { - r, closer, err := openCSV(path) - if err != nil { - return err - } - defer closer.Close() - - // header - if _, err := r.Read(); err != nil { - return fmt.Errorf("card_flights header: %w", err) - } - - for { - rec, err := r.Read() - if errors.Is(err, io.EOF) { - break - } - if err != nil { - return fmt.Errorf("card_flights read: %w", err) - } - // columns: card_id, flight_id - if len(rec) < 2 { - return fmt.Errorf("card_flights row has %d columns, expected >=2", len(rec)) - } - cid, err := parseUint(rec[0]) - if err != nil { - return fmt.Errorf("card_id: %w", err) - } - fid, err := parseUint(rec[1]) - if err != nil { - return fmt.Errorf("flight_id: %w", err) - } - - if !s.validFlightID(fid) { - continue - } - if _, ok := s.cardFlights[cid]; !ok { - s.cardFlights[cid] = make(map[uint64]struct{}) - } - s.cardFlights[cid][fid] = struct{}{} - } - return nil -} - -// --- helpers --- - -func openCSV(path string) (*csv.Reader, io.Closer, error) { - f, err := os.Open(path) - if err != nil { - return nil, nil, err - } - r := csv.NewReader(f) - r.ReuseRecord = true - // r.FieldsPerRecord = -1 // allow variable columns per row (comment out if you want strictness) - return r, f, nil -} - -func parseUint(s string) (uint64, error) { - return strconv.ParseUint(strings.TrimSpace(s), 10, 64) -} -func parseInt(s string) (int64, error) { - return strconv.ParseInt(strings.TrimSpace(s), 10, 64) -} -func parseFloat(s string) (float64, error) { - if strings.TrimSpace(s) == "" { - return 0, nil - } - return strconv.ParseFloat(strings.TrimSpace(s), 64) -} - -// Ensure slices are large enough and place item by ID (1-based supported) -func ensureLen[T any](slice []*T, id uint64) []*T { - needed := int(id) + 1 // keep index == id; index 0 unused per your exporters - if len(slice) <= needed { - newSlice := make([]*T, needed) - copy(newSlice, slice) - return newSlice - } - return slice -} - -func (s *Store) putUser(u *model.User) { - if u == nil { - return - } - s.users = ensureLen[model.User](s.users, u.ID) - s.users[u.ID] = u -} - -func (s *Store) putCard(c *model.Card) { - if c == nil { - return - } - s.cards = ensureLen[model.Card](s.cards, c.ID) - s.cards[c.ID] = c -} - -func (s *Store) putFlight(f *model.Flight) { - if f == nil { - return - } - s.flights = ensureLen[model.Flight](s.flights, f.ID) - s.flights[f.ID] = f -} - -func (s *Store) validFlightID(fid uint64) bool { - return fid != 0 && int(fid) < len(s.flights) && s.flights[fid] != nil -} diff --git a/pkg/localstore/store.go b/pkg/localstore/store.go index 151b2b4..dad2ffe 100644 --- a/pkg/localstore/store.go +++ b/pkg/localstore/store.go @@ -7,6 +7,7 @@ import ( "time" "unicode/utf8" + "airlines/pkg/airports" "airlines/pkg/model" ) @@ -51,7 +52,6 @@ type flightDayKey struct { DateYMD int32 } - type Store struct { mu sync.RWMutex @@ -97,7 +97,6 @@ func NewLocalStore() *Store { } } - func isZeroCoord(c model.LatLong) bool { return c.Lat == 0 && c.Long == 0 } func ymdUTC(t time.Time) int32 { @@ -122,8 +121,6 @@ func ensureSet(m map[uint64]map[uint64]struct{}, k uint64) map[uint64]struct{} { return s } -/* ============================== users ============================= */ - func fatherInitial(s string) string { s = strings.TrimSpace(s) if s == "" { @@ -133,7 +130,7 @@ func fatherInitial(s string) string { if r == utf8.RuneError { return "" } - return string(r) // your pipeline keeps it UPPER + return string(r) } func addUserInitIndex(m map[userInitKey]uint64, u *model.User) { @@ -148,7 +145,6 @@ func delUserInitIndex(m map[userInitKey]uint64, u *model.User) { delete(m, k) } -// --- merge helper (unchanged; keeps initial→full, birthday, nick, sex upgrades) --- func (s *Store) mergeUserFields(id uint64, in *model.User) *model.User { ex := s.users[id] // fathersname: initial -> full (same initial), move indexes @@ -186,12 +182,12 @@ func (s *Store) mergeUserFields(id uint64, in *model.User) *model.User { return ex } -// --- FIXED SaveUser: initial-key lookup tries BOTH (birth, 0) --- func (s *Store) SaveUser(u *model.User) (*model.User, error) { if u == nil { return nil, errors.New("nil user") } - // normalize (names already UPPER) + + // normalize just for sure u.Nick = strings.TrimSpace(u.Nick) u.Name = strings.TrimSpace(u.Name) u.Surname = strings.TrimSpace(u.Surname) @@ -205,14 +201,14 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { s.mu.Lock() defer s.mu.Unlock() - // 1) by Nick + // by Nick if u.Nick != "" { if id, ok := s.nickToUID[u.Nick]; ok { return s.mergeUserFields(id, u), nil } } - // 2) exact tuple + // exact tuple if id, ok := s.nameToUID[inKey]; ok { if u.Nick != "" && s.users[id].Nick == "" { s.users[id].Nick = u.Nick @@ -221,7 +217,7 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { return s.mergeUserFields(id, u), nil } - // 3) initial-based match (try with incoming birth, then with 0) + // try with incoming birth, then with 0 init := fatherInitial(u.Fathersname) tryInits := []userInitKey{ {Surname: u.Surname, Name: u.Name, Init: init, BirthYMD: inBirth}, @@ -233,14 +229,14 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { if id, ok := s.nameInitToUID[ik]; ok { ex := s.users[id] - // If ex has initial-only and incoming has full (same initial) → upgrade fathers + move indexes + // If ex has initial-only and incoming has full → upgrade fathers + move indexes if ex.Fathersname == fatherInitial(ex.Fathersname) && u.Fathersname != "" && fatherInitial(u.Fathersname) == fatherInitial(ex.Fathersname) { // move name index oldNameKey := userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)} delete(s.nameToUID, oldNameKey) - // remove old init index (birth may differ) + // remove old init index delUserInitIndex(s.nameInitToUID, ex) ex.Fathersname = u.Fathersname @@ -276,7 +272,7 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { } } - // 4) relaxed: fathersname empty, same birth + // fathersname empty, same birth if id, ok := s.nameToUID[userNameKey{Surname: u.Surname, Name: u.Name, Fathersname: "", BirthYMD: inBirth}]; ok { ex := s.users[id] if ex.Fathersname == "" && u.Fathersname != "" { @@ -298,7 +294,7 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { return ex, nil } - // 5) same fathersname, no birth + // same fathersname, no birth if id, ok := s.nameToUID[userNameKey{Surname: u.Surname, Name: u.Name, Fathersname: u.Fathersname, BirthYMD: 0}]; ok { delete(s.nameToUID, userNameKey{Surname: u.Surname, Name: u.Name, Fathersname: u.Fathersname, BirthYMD: 0}) ex := s.users[id] @@ -316,7 +312,7 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { return ex, nil } - // 6) fully unspecific existing (fathers="", birth=0) + // fathers="", birth=0 if id, ok := s.nameToUID[userNameKey{Surname: u.Surname, Name: u.Name, Fathersname: "", BirthYMD: 0}]; ok { ex := s.users[id] delete(s.nameToUID, userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: "", BirthYMD: 0}) @@ -338,7 +334,7 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { return ex, nil } - // 7) create + // not found -> create u.ID = uint64(len(s.users)) s.users = append(s.users, u) if u.Nick != "" { @@ -349,15 +345,6 @@ func (s *Store) SaveUser(u *model.User) (*model.User, error) { return u, nil } -/* ============================== cards ============================= */ -/* -Match order: - 1) exact (Prefix, Number, Bonus) - 2) pair (Prefix, Number) → if stored bonus=="" and incoming bonus!="", upgrade in place (move triple index) - 3) else create new -Never steal UserID: only set if existing has 0 and incoming non-zero. -*/ - func (s *Store) SaveCard(c *model.Card) (*model.Card, error) { if c == nil { return nil, errors.New("nil card") @@ -404,7 +391,7 @@ func (s *Store) SaveCard(c *model.Card) (*model.Card, error) { s.cardsByUser[ex.UserID] = v switch { case ex.Bonusprogramm == "" && c.Bonusprogramm != "": - // move triple index from empty -> new bonus + // move index from empty -> new bonus oldTri := cardKey{Prefix: ex.Prefix, Number: ex.Number, Bonus: ex.Bonusprogramm} delete(s.cardToCID, oldTri) ex.Bonusprogramm = c.Bonusprogramm @@ -415,8 +402,8 @@ func (s *Store) SaveCard(c *model.Card) (*model.Card, error) { return ex, nil case ex.Bonusprogramm != "" && c.Bonusprogramm == "": return ex, nil - case ex.Bonusprogramm != "" && c.Bonusprogramm != "" && ex.Bonusprogramm != c.Bonusprogramm: - // different program → create new card record + // different program → create new card record + // case ex.Bonusprogramm != "" && c.Bonusprogramm != "" && ex.Bonusprogramm != c.Bonusprogramm: default: return ex, nil } @@ -426,7 +413,7 @@ func (s *Store) SaveCard(c *model.Card) (*model.Card, error) { c.ID = uint64(len(s.cards)) s.cards = append(s.cards, c) s.cardPairToCID[pair] = c.ID - s.cardToCID[tri] = c.ID // even if bonus == "", we still index triple + s.cardToCID[tri] = c.ID if s.cardsByUser[c.UserID] == nil { s.cardsByUser[c.UserID] = make(map[uint64]struct{}, 1024) @@ -438,18 +425,6 @@ func (s *Store) SaveCard(c *model.Card) (*model.Card, error) { return c, nil } -/* ============================== flights =========================== */ -/* -Identity: - - date-only: (Number, From, To, DateYMD, false, 0) - - timed : (Number, From, To, DateYMD, true, SecSinceMidnight) -Upgrade: - - if a date-only exists and a timed arrives for the same day, upgrade in place -Merge: - - coords: fill when missing - - relations: add (dedup via sets) -*/ - func (s *Store) SaveFlight(f *model.Flight) (*model.Flight, error) { if f == nil { return nil, errors.New("nil flight") @@ -474,14 +449,14 @@ func (s *Store) SaveFlight(f *model.Flight) (*model.Flight, error) { s.mu.Lock() defer s.mu.Unlock() - // 1) exact (precise) key + // precise key if id, ok := s.flightToFID[pKey]; ok { ex := s.flights[id] s.mergeFlightFields(id, ex, f) return ex, nil } - // 2) same day exists -> maybe upgrade date-only to timed + // same day exists -> maybe upgrade date-only to timed if id, ok := s.flightByDay[dayKey]; ok { ex := s.flights[id] exKey := s.keyOfFlight(ex) @@ -489,7 +464,7 @@ func (s *Store) SaveFlight(f *model.Flight) (*model.Flight, error) { // move map key to timed delete(s.flightToFID, exKey) ex.HasTime = true - // set clock from incoming (keep same calendar date) + // set clock from incoming ex.Date = time.Date(dayUTC.Year(), dayUTC.Month(), dayUTC.Day(), f.Date.Hour(), f.Date.Minute(), f.Date.Second(), f.Date.Nanosecond(), f.Date.Location()) s.flightToFID[s.keyOfFlight(ex)] = id @@ -500,20 +475,20 @@ func (s *Store) SaveFlight(f *model.Flight) (*model.Flight, error) { return ex, nil } - // 3) brand new + // brand new f.ID = uint64(len(s.flights)) s.flights = append(s.flights, f) s.flightToFID[pKey] = f.ID s.flightByDay[dayKey] = f.ID - // if s.countriesByUser[f.UserID] == nil { - // s.countriesByUser[f.UserID] = make(map[string]struct{}, 1024) - // } + if s.countriesByUser[f.UserID] == nil { + s.countriesByUser[f.UserID] = make(map[string]struct{}, 1024) + } - // v := s.countriesByUser[f.UserID] - // dd, _ := airports.LookupIATA(f.From) - // v[dd.Country] = struct{}{} - // s.countriesByUser[f.UserID] = v + v := s.countriesByUser[f.UserID] + dd, _ := airports.LookupIATA(f.From) + v[dd.Country] = struct{}{} + s.countriesByUser[f.UserID] = v if f.Code != "" { if s.codesByUser[f.UserID] == nil { @@ -543,7 +518,7 @@ func (s *Store) keyOfFlight(f *model.Flight) flightKey { } func (s *Store) mergeFlightFields(id uint64, ex, in *model.Flight) { - // coords: fill when empty + // coords fill when empty if isZeroCoord(ex.FromCoords) && !isZeroCoord(in.FromCoords) { ex.FromCoords = in.FromCoords } @@ -560,17 +535,15 @@ func (s *Store) mergeFlightFields(id uint64, ex, in *model.Flight) { if in.Code != "" && ex.Code == "" { ex.Code = in.Code - // if s.codesByUser[in.UserID] == nil { - // s.codesByUser[in.UserID] = make(map[string]struct{}, 1024) - // } - // codesByUser := s.codesByUser[in.UserID] - // codesByUser[in.Code] = struct{}{} - // s.codesByUser[in.UserID] = codesByUser + if s.codesByUser[in.UserID] == nil { + s.codesByUser[in.UserID] = make(map[string]struct{}, 1024) + } + codesByUser := s.codesByUser[in.UserID] + codesByUser[in.Code] = struct{}{} + s.codesByUser[in.UserID] = codesByUser } } -/* ============================== finders =========================== */ - func (s *Store) FindUserByNick(nick string) (*model.User, bool) { s.mu.RLock() defer s.mu.RUnlock() @@ -579,60 +552,4 @@ func (s *Store) FindUserByNick(nick string) (*model.User, bool) { return nil, false } return s.users[id], true -} - -func (s *Store) FindUserByName(name, surname, fathers string, bday time.Time) (*model.User, bool) { - key := userNameKey{ - Surname: strings.TrimSpace(surname), - Name: strings.TrimSpace(name), - Fathersname: strings.TrimSpace(fathers), - BirthYMD: ymdUTC(bday), - } - s.mu.RLock() - defer s.mu.RUnlock() - id, ok := s.nameToUID[key] - if !ok || id == 0 || int(id) >= len(s.users) { - return nil, false - } - return s.users[id], true -} - -func (s *Store) FindCard(prefix string, number uint64, bonus string) (*model.Card, bool) { - tri := cardKey{Prefix: strings.TrimSpace(prefix), Number: number, Bonus: strings.TrimSpace(bonus)} - s.mu.RLock() - defer s.mu.RUnlock() - if id, ok := s.cardToCID[tri]; ok && id != 0 && int(id) < len(s.cards) { - return s.cards[id], true - } - // fall back to pair if no exact - pair := cardPairKey{Prefix: strings.TrimSpace(prefix), Number: number} - if id, ok := s.cardPairToCID[pair]; ok && id != 0 && int(id) < len(s.cards) { - return s.cards[id], true - } - return nil, false -} - -func (s *Store) FindFlight(number, from, to string, date time.Time, hasTime bool) (*model.Flight, bool) { - number = strings.TrimSpace(number) - from = strings.TrimSpace(from) - to = strings.TrimSpace(to) - - ymd := ymdUTC(time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)) - var k flightKey - if hasTime { - k = flightKey{Number: number, From: from, To: to, DateYMD: ymd, HasTime: true, Sec: secSinceMidnight(date)} - } else { - k = flightKey{Number: number, From: from, To: to, DateYMD: ymd, HasTime: false, Sec: 0} - } - - s.mu.RLock() - defer s.mu.RUnlock() - if id, ok := s.flightToFID[k]; ok && id != 0 && int(id) < len(s.flights) { - return s.flights[id], true - } - // day-level fallback (returns best precision for the day if exact key absent) - if id, ok := s.flightByDay[flightDayKey{Number: number, From: from, To: to, DateYMD: ymd}]; ok && id != 0 && int(id) < len(s.flights) { - return s.flights[id], true - } - return nil, false -} +}
\ No newline at end of file |
