1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
package localstore
import (
"errors"
"strings"
"sync"
"time"
"unicode/utf8"
"airlines/pkg/airports"
"airlines/pkg/model"
)
type userNameKey struct {
Surname string
Name string
Fathersname string // "" allowed
BirthYMD int32 // 0 if unknown
}
type userInitKey struct {
Surname string
Name string
Init string // one letter
BirthYMD int32
}
type cardKey struct {
Prefix string
Bonus string // may be ""
Number uint64
}
type cardPairKey struct {
Prefix string
Number uint64
}
type flightKey struct {
Number string
From string
To string
DateYMD int32
HasTime bool
Sec int32 // seconds since local midnight if HasTime
}
type flightDayKey struct {
Number string
From string
To string
DateYMD int32
}
type Store struct {
mu sync.RWMutex
users []*model.User
cards []*model.Card
flights []*model.Flight
nickToUID map[string]uint64
nameToUID map[userNameKey]uint64
nameInitToUID map[userInitKey]uint64
cardToCID map[cardKey]uint64
cardPairToCID map[cardPairKey]uint64
flightToFID map[flightKey]uint64
flightByDay map[flightDayKey]uint64
userFlights map[uint64]map[uint64]struct{} // user_id -> set(flight_id)
cardFlights map[uint64]map[uint64]struct{} // card_id -> set(flight_id)
codesByUser map[uint64]map[string]struct{} // user_id -> set(code)
countriesByUser map[uint64]map[string]struct{} // user_id -> set(country)
cardsByUser map[uint64]map[uint64]struct{} // user_id -> set(card_id)
}
func NewLocalStore() *Store {
return &Store{
users: make([]*model.User, 1),
cards: make([]*model.Card, 1),
flights: make([]*model.Flight, 1),
nickToUID: make(map[string]uint64, 1<<12),
nameToUID: make(map[userNameKey]uint64, 1<<12),
cardToCID: make(map[cardKey]uint64, 1<<14),
cardPairToCID: make(map[cardPairKey]uint64, 1<<14),
flightToFID: make(map[flightKey]uint64, 1<<15),
flightByDay: make(map[flightDayKey]uint64, 1<<15),
userFlights: make(map[uint64]map[uint64]struct{}, 1<<12),
cardFlights: make(map[uint64]map[uint64]struct{}, 1<<12),
nameInitToUID: make(map[userInitKey]uint64, 1<<12),
codesByUser: make(map[uint64]map[string]struct{}, 1<<12),
countriesByUser: make(map[uint64]map[string]struct{}, 1<<12),
cardsByUser: make(map[uint64]map[uint64]struct{}, 1<<12),
}
}
func isZeroCoord(c model.LatLong) bool { return c.Lat == 0 && c.Long == 0 }
func ymdUTC(t time.Time) int32 {
if t.IsZero() || t.Year() == model.SentinelBirthday().Year() {
return 0
}
u := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
return int32(u.Year()*10000 + int(u.Month())*100 + u.Day())
}
func secSinceMidnight(t time.Time) int32 {
h, m, s := t.Clock()
return int32(h*3600 + m*60 + s)
}
func ensureSet(m map[uint64]map[uint64]struct{}, k uint64) map[uint64]struct{} {
s, ok := m[k]
if !ok {
s = make(map[uint64]struct{}, 8)
m[k] = s
}
return s
}
func fatherInitial(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
r, _ := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return ""
}
return string(r)
}
func addUserInitIndex(m map[userInitKey]uint64, u *model.User) {
k := userInitKey{Surname: u.Surname, Name: u.Name, Init: fatherInitial(u.Fathersname), BirthYMD: ymdUTC(u.Birthday)}
if _, exists := m[k]; !exists {
m[k] = u.ID
}
}
func delUserInitIndex(m map[userInitKey]uint64, u *model.User) {
k := userInitKey{Surname: u.Surname, Name: u.Name, Init: fatherInitial(u.Fathersname), BirthYMD: ymdUTC(u.Birthday)}
delete(m, k)
}
func (s *Store) mergeUserFields(id uint64, in *model.User) *model.User {
ex := s.users[id]
// fathersname: initial -> full (same initial), move indexes
if ex.Fathersname != "" && len([]rune(ex.Fathersname)) == 1 &&
in.Fathersname != "" && fatherInitial(ex.Fathersname) == fatherInitial(in.Fathersname) &&
ex.Fathersname != in.Fathersname {
oldNameKey := userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)}
if _, ok := s.nameToUID[oldNameKey]; ok {
delete(s.nameToUID, oldNameKey)
}
delUserInitIndex(s.nameInitToUID, ex)
ex.Fathersname = in.Fathersname
s.nameToUID[userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)}] = id
addUserInitIndex(s.nameInitToUID, ex)
}
// birthday upgrade
if ymdUTC(ex.Birthday) == 0 && ymdUTC(in.Birthday) != 0 {
oldNameKey := userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: 0}
if _, ok := s.nameToUID[oldNameKey]; ok {
delete(s.nameToUID, oldNameKey)
}
delUserInitIndex(s.nameInitToUID, ex)
ex.Birthday = in.Birthday
s.nameToUID[userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)}] = id
addUserInitIndex(s.nameInitToUID, ex)
}
// nick/sex
if ex.Nick == "" && in.Nick != "" {
ex.Nick = in.Nick
s.nickToUID[in.Nick] = id
}
if ex.Sex == model.SexUnknown && in.Sex != model.SexUnknown {
ex.Sex = in.Sex
}
return ex
}
func (s *Store) SaveUser(u *model.User) (*model.User, error) {
if u == nil {
return nil, errors.New("nil user")
}
// normalize just for sure
u.Nick = strings.TrimSpace(u.Nick)
u.Name = strings.TrimSpace(u.Name)
u.Surname = strings.TrimSpace(u.Surname)
u.Fathersname = strings.Trim(strings.TrimSpace(u.Fathersname), ".,")
if u.Birthday.IsZero() {
u.Birthday = model.SentinelBirthday()
}
inBirth := ymdUTC(u.Birthday)
inKey := userNameKey{Surname: u.Surname, Name: u.Name, Fathersname: u.Fathersname, BirthYMD: inBirth}
s.mu.Lock()
defer s.mu.Unlock()
// by Nick
if u.Nick != "" {
if id, ok := s.nickToUID[u.Nick]; ok {
return s.mergeUserFields(id, u), nil
}
}
// exact tuple
if id, ok := s.nameToUID[inKey]; ok {
if u.Nick != "" && s.users[id].Nick == "" {
s.users[id].Nick = u.Nick
s.nickToUID[u.Nick] = id
}
return s.mergeUserFields(id, u), nil
}
// try with incoming birth, then with 0
init := fatherInitial(u.Fathersname)
tryInits := []userInitKey{
{Surname: u.Surname, Name: u.Name, Init: init, BirthYMD: inBirth},
}
if inBirth != 0 {
tryInits = append(tryInits, userInitKey{Surname: u.Surname, Name: u.Name, Init: init, BirthYMD: 0})
}
for _, ik := range tryInits {
if id, ok := s.nameInitToUID[ik]; ok {
ex := s.users[id]
// 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
delUserInitIndex(s.nameInitToUID, ex)
ex.Fathersname = u.Fathersname
newNameKey := userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)}
s.nameToUID[newNameKey] = id
addUserInitIndex(s.nameInitToUID, ex)
}
// Upgrade birthday if needed
if ymdUTC(ex.Birthday) == 0 && inBirth != 0 {
// move name key 0 -> inBirth
oldNameKey := userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: 0}
if _, ok2 := s.nameToUID[oldNameKey]; ok2 {
delete(s.nameToUID, oldNameKey)
}
// move init index 0 -> inBirth
delUserInitIndex(s.nameInitToUID, ex)
ex.Birthday = u.Birthday
s.nameToUID[userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: inBirth}] = id
addUserInitIndex(s.nameInitToUID, ex)
}
// nick/sex upgrades
if u.Nick != "" && ex.Nick == "" {
ex.Nick = u.Nick
s.nickToUID[u.Nick] = id
}
if ex.Sex == model.SexUnknown && u.Sex != model.SexUnknown {
ex.Sex = u.Sex
}
return ex, nil
}
}
// 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 != "" {
delete(s.nameToUID, userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: "", BirthYMD: inBirth})
ex.Fathersname = u.Fathersname
s.nameToUID[userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: inBirth}] = id
addUserInitIndex(s.nameInitToUID, ex)
}
if u.Nick != "" && ex.Nick == "" {
ex.Nick = u.Nick
s.nickToUID[u.Nick] = id
}
if ex.Sex == model.SexUnknown && u.Sex != model.SexUnknown {
ex.Sex = u.Sex
}
if ymdUTC(ex.Birthday) == 0 && inBirth != 0 {
ex.Birthday = u.Birthday
}
return ex, nil
}
// 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]
ex.Birthday = u.Birthday
s.nameToUID[inKey] = id
delUserInitIndex(s.nameInitToUID, ex)
addUserInitIndex(s.nameInitToUID, ex)
if u.Nick != "" && ex.Nick == "" {
ex.Nick = u.Nick
s.nickToUID[u.Nick] = id
}
if ex.Sex == model.SexUnknown && u.Sex != model.SexUnknown {
ex.Sex = u.Sex
}
return ex, nil
}
// 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})
if ex.Fathersname == "" && u.Fathersname != "" {
ex.Fathersname = u.Fathersname
}
if ymdUTC(ex.Birthday) == 0 && inBirth != 0 {
ex.Birthday = u.Birthday
}
s.nameToUID[userNameKey{Surname: ex.Surname, Name: ex.Name, Fathersname: ex.Fathersname, BirthYMD: ymdUTC(ex.Birthday)}] = id
addUserInitIndex(s.nameInitToUID, ex)
if u.Nick != "" && ex.Nick == "" {
ex.Nick = u.Nick
s.nickToUID[u.Nick] = id
}
if ex.Sex == model.SexUnknown && u.Sex != model.SexUnknown {
ex.Sex = u.Sex
}
return ex, nil
}
// not found -> create
u.ID = uint64(len(s.users))
s.users = append(s.users, u)
if u.Nick != "" {
s.nickToUID[u.Nick] = u.ID
}
s.nameToUID[inKey] = u.ID
addUserInitIndex(s.nameInitToUID, u)
return u, nil
}
func (s *Store) SaveCard(c *model.Card) (*model.Card, error) {
if c == nil {
return nil, errors.New("nil card")
}
c.Prefix = strings.TrimSpace(c.Prefix)
c.Bonusprogramm = strings.TrimSpace(c.Bonusprogramm)
if c.Prefix == "" {
return nil, errors.New("invalid card: empty prefix")
}
tri := cardKey{Prefix: c.Prefix, Number: c.Number, Bonus: c.Bonusprogramm}
pair := cardPairKey{Prefix: c.Prefix, Number: c.Number}
s.mu.Lock()
defer s.mu.Unlock()
// exact triple
if id, ok := s.cardToCID[tri]; ok {
ex := s.cards[id]
if ex.UserID == 0 && c.UserID != 0 {
ex.UserID = c.UserID
if s.cardsByUser[ex.UserID] == nil {
s.cardsByUser[ex.UserID] = make(map[uint64]struct{}, 1024)
}
v := s.cardsByUser[ex.UserID]
v[ex.ID] = struct{}{}
s.cardsByUser[ex.UserID] = v
}
return ex, nil
}
// by pair
if id, ok := s.cardPairToCID[pair]; ok {
ex := s.cards[id]
// link user once
if ex.UserID == 0 && c.UserID != 0 {
ex.UserID = c.UserID
}
if s.cardsByUser[ex.UserID] == nil {
s.cardsByUser[ex.UserID] = make(map[uint64]struct{}, 1024)
}
v := s.cardsByUser[ex.UserID]
v[ex.ID] = struct{}{}
s.cardsByUser[ex.UserID] = v
switch {
case ex.Bonusprogramm == "" && c.Bonusprogramm != "":
// 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
newTri := cardKey{Prefix: ex.Prefix, Number: ex.Number, Bonus: ex.Bonusprogramm}
s.cardToCID[newTri] = id
return ex, nil
case ex.Bonusprogramm == "" && c.Bonusprogramm == "":
return ex, nil
case ex.Bonusprogramm != "" && c.Bonusprogramm == "":
return ex, nil
// different program → create new card record
// case ex.Bonusprogramm != "" && c.Bonusprogramm != "" && ex.Bonusprogramm != c.Bonusprogramm:
default:
return ex, nil
}
}
// create
c.ID = uint64(len(s.cards))
s.cards = append(s.cards, c)
s.cardPairToCID[pair] = c.ID
s.cardToCID[tri] = c.ID
if s.cardsByUser[c.UserID] == nil {
s.cardsByUser[c.UserID] = make(map[uint64]struct{}, 1024)
}
v := s.cardsByUser[c.UserID]
v[c.ID] = struct{}{}
s.cardsByUser[c.UserID] = v
return c, nil
}
func (s *Store) SaveFlight(f *model.Flight) (*model.Flight, error) {
if f == nil {
return nil, errors.New("nil flight")
}
f.Number = strings.TrimSpace(f.Number)
f.From = strings.TrimSpace(f.From)
f.To = strings.TrimSpace(f.To)
// normalize day
dayUTC := time.Date(f.Date.Year(), f.Date.Month(), f.Date.Day(), 0, 0, 0, 0, time.UTC)
ymd := ymdUTC(dayUTC)
var pKey flightKey
if f.HasTime {
pKey = flightKey{Number: f.Number, From: f.From, To: f.To, DateYMD: ymd, HasTime: true, Sec: secSinceMidnight(f.Date)}
} else {
f.Date = dayUTC // store as date-only
pKey = flightKey{Number: f.Number, From: f.From, To: f.To, DateYMD: ymd, HasTime: false, Sec: 0}
}
dayKey := flightDayKey{Number: f.Number, From: f.From, To: f.To, DateYMD: ymd}
s.mu.Lock()
defer s.mu.Unlock()
// precise key
if id, ok := s.flightToFID[pKey]; ok {
ex := s.flights[id]
s.mergeFlightFields(id, ex, f)
return ex, nil
}
// same day exists -> maybe upgrade date-only to timed
if id, ok := s.flightByDay[dayKey]; ok {
ex := s.flights[id]
exKey := s.keyOfFlight(ex)
if !ex.HasTime && f.HasTime {
// move map key to timed
delete(s.flightToFID, exKey)
ex.HasTime = true
// 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
// day index already points to best precision
}
// merge fields/relations
s.mergeFlightFields(id, ex, f)
return ex, nil
}
// 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)
}
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 {
s.codesByUser[f.UserID] = make(map[string]struct{}, 1024)
}
codesByUser := s.codesByUser[f.UserID]
codesByUser[f.Code] = struct{}{}
s.codesByUser[f.UserID] = codesByUser
}
// relations
if f.UserID != 0 {
ensureSet(s.userFlights, f.UserID)[f.ID] = struct{}{}
}
if f.CardID != 0 {
ensureSet(s.cardFlights, f.CardID)[f.ID] = struct{}{}
}
return f, nil
}
func (s *Store) keyOfFlight(f *model.Flight) flightKey {
ymd := ymdUTC(time.Date(f.Date.Year(), f.Date.Month(), f.Date.Day(), 0, 0, 0, 0, time.UTC))
if f.HasTime {
return flightKey{Number: f.Number, From: f.From, To: f.To, DateYMD: ymd, HasTime: true, Sec: secSinceMidnight(f.Date)}
}
return flightKey{Number: f.Number, From: f.From, To: f.To, DateYMD: ymd, HasTime: false, Sec: 0}
}
func (s *Store) mergeFlightFields(id uint64, ex, in *model.Flight) {
// coords fill when empty
if isZeroCoord(ex.FromCoords) && !isZeroCoord(in.FromCoords) {
ex.FromCoords = in.FromCoords
}
if isZeroCoord(ex.ToCoords) && !isZeroCoord(in.ToCoords) {
ex.ToCoords = in.ToCoords
}
// relations
if in.UserID != 0 {
ensureSet(s.userFlights, in.UserID)[id] = struct{}{}
}
if in.CardID != 0 {
ensureSet(s.cardFlights, in.CardID)[id] = struct{}{}
}
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
}
}
func (s *Store) FindUserByNick(nick string) (*model.User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
id, ok := s.nickToUID[strings.TrimSpace(nick)]
if !ok || id == 0 || int(id) >= len(s.users) {
return nil, false
}
return s.users[id], true
}
|