-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.c
1473 lines (1170 loc) · 37.5 KB
/
db.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "db.h"
#include "logger.h"
#include "misc.h"
#include "atomic.h"
static double round_mark(double x) {
return (double)((int64_t)(100000 * x + 0.5))/100000;
}
static int is_leap(struct tm *date)
{
if ((date->tm_year % 400) == 0)
return 1;
if ((date->tm_year % 100) == 0)
return 0;
if ((date->tm_year % 4) == 0)
return 1;
return 0;
}
static int get_adjusted_birth_day(struct tm *birth_date, struct tm *now)
{
int birth_day = birth_date->tm_yday;
int current_day = now->tm_yday;
if (is_leap(birth_date) && !is_leap(now) && birth_day >= 60) {
return birth_day - 1;
}
if (is_leap(now) && !is_leap(birth_date) && current_day >= 60) {
return birth_day + 1;
}
return birth_day;
}
static int age_at(struct tm *birth_date, struct tm *now, int *result)
{
int years;
int birth_day;
if (now->tm_year < birth_date->tm_year)
return EINVAL;
// Get the year number change since the player's birth.
years = now->tm_year - birth_date->tm_year;
// If the date is before the date of birth, then not that many years have elapsed.
birth_day = get_adjusted_birth_day(birth_date, now);
if (now->tm_yday < birth_day) {
if (years == 0)
return EINVAL;
years -= 1;
}
*result = years;
return 0;
}
static int db_get_age(struct db *db, int64_t birth_date_timestamp, uint32_t *result)
{
struct tm birth_date, now_date;
time_t timestamp;
int r;
int age;
timestamp = birth_date_timestamp;
if (!gmtime_r(×tamp, &birth_date))
return EINVAL;
timestamp = db->now;
if (!gmtime_r(×tamp, &now_date))
return EINVAL;
r = age_at(&birth_date, &now_date, &age);
if (r)
return r;
bug_on(age < 0);
*result = age;
return 0;
}
int db_init(struct db *db)
{
size_t i;
int r;
rwlock_init(&db->lock);
for (i = 0; i < ARRAY_SIZE(db->users); i++)
list_init(&db->users[i]);
for (i = 0; i < ARRAY_SIZE(db->locations); i++)
list_init(&db->locations[i]);
for (i = 0; i < ARRAY_SIZE(db->visits); i++)
list_init(&db->visits[i]);
for (i = 0; i < ARRAY_SIZE(db->user_visits); i++)
list_init(&db->user_visits[i]);
for (i = 0; i < ARRAY_SIZE(db->location_visits); i++)
list_init(&db->location_visits[i]);
r = memory_pool_init(&db->user_pool, sizeof(struct user), USERS_TABLE_SIZE);
if (r)
return r;
r = memory_pool_init(&db->location_pool, sizeof(struct location), LOCATIONS_TABLE_SIZE);
if (r)
goto free_user_pool;
r = memory_pool_init(&db->visit_pool, sizeof(struct visit), VISITS_TABLE_SIZE);
if (r)
goto free_location_pool;
r = memory_pool_init(&db->user_visits_pool, sizeof(struct user_visits), USERS_TABLE_SIZE);
if (r)
goto free_visit_pool;
r = memory_pool_init(&db->location_visits_pool, sizeof(struct location_visits), LOCATIONS_TABLE_SIZE);
if (r)
goto free_user_visits_pool;
memset(db->users_table, 0, sizeof(db->users_table));
memset(db->visits_table, 0, sizeof(db->users_table));
memset(db->locations_table, 0, sizeof(db->locations_table));
db->users_table_misses = 0;
db->visits_table_misses = 0;
db->locations_table_misses = 0;
db->users_count = 0;
db->visits_count = 0;
db->locations_count = 0;
db->now = 0;
return 0;
free_user_visits_pool:
memory_pool_deinit(&db->user_visits_pool);
free_visit_pool:
memory_pool_deinit(&db->visit_pool);
free_location_pool:
memory_pool_deinit(&db->location_pool);
free_user_pool:
memory_pool_deinit(&db->user_pool);
return r;
}
static struct user_visits *db_alloc_user_visits(struct db *db, uint32_t user_id)
{
struct user_visits *user_visits;
user_visits = memory_pool_alloc(&db->user_visits_pool);
if (!user_visits)
return NULL;
list_init(&user_visits->list);
list_init(&user_visits->visits);
user_visits->user_id = user_id;
return user_visits;
}
static struct location_visits *db_alloc_location_visits(struct db *db, uint32_t location_id)
{
struct location_visits *location_visits;
location_visits = memory_pool_alloc(&db->location_visits_pool);
if (!location_visits)
return NULL;
list_init(&location_visits->list);
list_init(&location_visits->visits);
location_visits->location_id = location_id;
return location_visits;
}
static void db_free_user_visits(struct db *db, struct user_visits *user_visits)
{
memory_pool_free(&db->user_visits_pool, user_visits);
}
static void db_free_location_visits(struct db *db, struct location_visits *location_visits)
{
memory_pool_free(&db->location_visits_pool, location_visits);
}
void db_free(struct db *db)
{
struct user *user, *user_tmp;
struct location *location, *location_tmp;
struct visit *visit, *visit_tmp;
struct user_visits *user_visits, *user_visits_tmp;
struct location_visits *location_visits, *location_visits_tmp;
size_t i;
for (i = 0; i < ARRAY_SIZE(db->users); i++) {
list_for_each_entry_safe(user, user_tmp, &db->users[i], list) {
list_del_init(&user->list);
memory_pool_free(&db->user_pool, user);
}
}
for (i = 0; i < ARRAY_SIZE(db->locations); i++) {
list_for_each_entry_safe(location, location_tmp, &db->locations[i], list) {
list_del_init(&location->list);
memory_pool_free(&db->location_pool, location);
}
}
for (i = 0; i < ARRAY_SIZE(db->visits); i++) {
list_for_each_entry_safe(visit, visit_tmp, &db->visits[i], list) {
list_del_init(&visit->list);
memory_pool_free(&db->visit_pool, visit);
}
}
for (i = 0; i < ARRAY_SIZE(db->user_visits); i++) {
list_for_each_entry_safe(user_visits, user_visits_tmp, &db->user_visits[i], list) {
list_del_init(&user_visits->list);
db_free_user_visits(db, user_visits);
}
}
for (i = 0; i < ARRAY_SIZE(db->location_visits); i++) {
list_for_each_entry_safe(location_visits, location_visits_tmp, &db->location_visits[i], list) {
list_del_init(&location_visits->list);
db_free_location_visits(db, location_visits);
}
}
memory_pool_deinit(&db->user_pool);
memory_pool_deinit(&db->visit_pool);
memory_pool_deinit(&db->location_pool);
memory_pool_deinit(&db->user_visits_pool);
memory_pool_deinit(&db->location_visits_pool);
log_info("users_table_misses %lld\n", atomic_read(&db->users_table_misses));
log_info("locations_table_misses %lld\n", atomic_read(&db->locations_table_misses));
log_info("visit_table_misses %lld\n", atomic_read(&db->visits_table_misses));
log_info("users_count %lld\n", atomic_read(&db->users_count));
log_info("visits_count %lld\n", atomic_read(&db->visits_count));
log_info("locations_count %lld\n", atomic_read(&db->locations_count));
}
static struct user* db_lookup_user(struct db *db, uint32_t user_id)
{
struct user *user;
size_t i;
if (user_id < ARRAY_SIZE(db->users_table)) {
user = db->users_table[user_id];
if (user)
return user;
}
return NULL;
i = hash_uint32(user_id) % ARRAY_SIZE(db->users);
list_for_each_entry(user, &db->users[i], list) {
if (user->id == user_id) {
atomic_inc(&db->users_table_misses);
return user;
}
}
return NULL;
}
static struct location* db_lookup_location(struct db *db, uint32_t location_id)
{
struct location *location;
size_t i;
if (location_id < ARRAY_SIZE(db->locations_table)) {
location = db->locations_table[location_id];
if (location)
return location;
}
return NULL;
i = hash_uint32(location_id) % ARRAY_SIZE(db->locations);
list_for_each_entry(location, &db->locations[i], list) {
if (location->id == location_id) {
atomic_inc(&db->locations_table_misses);
return location;
}
}
return NULL;
}
static struct visit* db_lookup_visit(struct db *db, uint32_t visit_id)
{
struct visit *visit;
size_t i;
if (visit_id < ARRAY_SIZE(db->visits_table)) {
visit = db->visits_table[visit_id];
if (visit)
return visit;
}
return NULL;
i = hash_uint32(visit_id) % ARRAY_SIZE(db->visits);
list_for_each_entry(visit, &db->visits[i], list) {
if (visit->id == visit_id) {
atomic_inc(&db->visits_table_misses);
return visit;
}
}
return NULL;
}
static struct user_visits* db_lookup_user_visits(struct db *db, uint32_t user_id)
{
struct user_visits *user_visits;
size_t i = hash_uint32(user_id) % ARRAY_SIZE(db->user_visits);
list_for_each_entry(user_visits, &db->user_visits[i], list) {
if (user_visits->user_id == user_id)
return user_visits;
}
return NULL;
}
static int db_insert_user_visits(struct db *db, struct user_visits *new_user_visits)
{
struct user_visits *user_visits;
uint32_t user_id = new_user_visits->user_id;
size_t i = hash_uint32(user_id) % ARRAY_SIZE(db->user_visits);
list_for_each_entry(user_visits, &db->user_visits[i], list) {
if (user_visits->user_id == user_id)
return EEXIST;
}
list_add_tail(&new_user_visits->list, &db->user_visits[i]);
return 0;
}
static struct location_visits* db_lookup_location_visits(struct db *db, uint32_t location_id)
{
struct location_visits *location_visits;
size_t i = hash_uint32(location_id) % ARRAY_SIZE(db->location_visits);
list_for_each_entry(location_visits, &db->location_visits[i], list) {
if (location_visits->location_id == location_id)
return location_visits;
}
return NULL;
}
static int db_insert_location_visits(struct db *db, struct location_visits *new_location_visits)
{
struct location_visits *location_visits;
uint32_t location_id = new_location_visits->location_id;
size_t i = hash_uint32(location_id) % ARRAY_SIZE(db->location_visits);
list_for_each_entry(location_visits, &db->location_visits[i], list) {
if (location_visits->location_id == location_id)
return EEXIST;
}
list_add_tail(&new_location_visits->list, &db->location_visits[i]);
return 0;
}
static struct location_visits *db_get_or_create_location_visits(struct db *db, uint32_t location_id)
{
struct location_visits *location_visits;
int r;
location_visits = db_lookup_location_visits(db, location_id);
if (!location_visits) {
location_visits = db_alloc_location_visits(db, location_id);
if (!location_visits) {
return NULL;
}
r = db_insert_location_visits(db, location_visits);
if (r) {
db_free_location_visits(db, location_visits);
return NULL;
}
}
return location_visits;
}
static struct user_visits *db_get_or_create_user_visits(struct db *db, uint32_t user_id)
{
struct user_visits *user_visits;
int r;
user_visits = db_lookup_user_visits(db, user_id);
if (!user_visits) {
user_visits = db_alloc_user_visits(db, user_id);
if (!user_visits) {
return NULL;
}
r = db_insert_user_visits(db, user_visits);
if (r) {
db_free_user_visits(db, user_visits);
return NULL;
}
}
return user_visits;
}
static void add_user_visit_ordered(struct user *user, struct visit *visit)
{
struct visit *curr_visit;
list_for_each_entry(curr_visit, &user->visits, user_list) {
if (visit->visited_at < curr_visit->visited_at) {
list_add_tail(&visit->user_list, &curr_visit->user_list);
return;
}
}
list_add_tail(&visit->user_list, &user->visits);
}
int db_new_user(struct db *db, struct user_data *data)
{
struct user *user;
struct user_visits *user_visits;
struct visit *visit;
size_t i;
int r;
if (!json_uint32_is_valid(&data->id) || !json_string_is_valid(&data->email)
|| !json_string_is_valid(&data->first_name) || !json_string_is_valid(&data->last_name)
|| !json_string_is_valid(&data->gender) || !json_int64_is_valid(&data->birth_date))
{
return EINVAL;
}
if (strlen(data->gender.value) != 1 ||
(data->gender.value[0] != 'm' && data->gender.value[0] != 'f')) {
return EINVAL;
}
rwlock_lock(&db->lock);
if (db_lookup_user(db, data->id.value)) {
r = EEXIST;
goto unlock;
}
user = memory_pool_alloc(&db->user_pool);
if (!user) {
r = ENOMEM;
goto unlock;
}
user->id = data->id.value;
strncpy(user->email, data->email.value, strlen(data->email.value) + 1);
strncpy(user->first_name, data->first_name.value, strlen(data->first_name.value) + 1);
strncpy(user->last_name, data->last_name.value, strlen(data->last_name.value) + 1);
strncpy(user->gender, data->gender.value, strlen(data->gender.value) + 1);
user->birth_date = data->birth_date.value;
list_init(&user->visits);
r = db_get_age(db, user->birth_date, &user->age);
if (r) {
log_error("get_age error %d\n", r);
goto unlock_free;
}
user_visits = db_get_or_create_user_visits(db, user->id);
if (!user_visits) {
r = ENOMEM;
goto unlock_free;
}
list_for_each_entry(visit, &user_visits->visits, user_visits_list) {
if (visit->user != user) {
bug_on(visit->user);
bug_on(!list_empty(&visit->user_list));
add_user_visit_ordered(user, visit);
visit->user = user;
}
}
if (user->id < ARRAY_SIZE(db->users_table))
db->users_table[user->id] = user;
atomic_inc(&db->users_count);
i = hash_uint32(user->id) % ARRAY_SIZE(db->users);
list_add_tail(&user->list, &db->users[i]);
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
unlock_free:
rwlock_unlock(&db->lock);
memory_pool_free(&db->user_pool, user);
return r;
}
int db_new_location(struct db *db, struct location_data *data)
{
struct location *location;
struct location_visits *location_visits;
struct visit *visit;
size_t i;
int r;
if (!json_uint32_is_valid(&data->id) || !json_string_is_valid(&data->place)
|| !json_string_is_valid(&data->country) || !json_string_is_valid(&data->city)
|| !json_uint32_is_valid(&data->distance))
return EINVAL;
rwlock_lock(&db->lock);
if (db_lookup_location(db, data->id.value)) {
r = EEXIST;
goto unlock;
}
location = memory_pool_alloc(&db->location_pool);
if (!location) {
r = ENOMEM;
goto unlock;
}
location->id = data->id.value;
location->place_len = strlen(data->place.value);
strncpy(location->place, data->place.value, location->place_len + 1);
strncpy(location->country, data->country.value, strlen(data->country.value) + 1);
strncpy(location->city, data->city.value, strlen(data->city.value) + 1);
location->distance = data->distance.value;
list_init(&location->visits);
location_visits = db_get_or_create_location_visits(db, location->id);
if (!location_visits) {
r = ENOMEM;
goto unlock_free;
}
list_for_each_entry(visit, &location_visits->visits, location_visits_list) {
if (visit->location != location) {
bug_on(visit->location);
list_add_tail(&visit->location_list, &location->visits);
visit->location = location;
}
}
if (location->id < ARRAY_SIZE(db->locations_table))
db->locations_table[location->id] = location;
atomic_inc(&db->locations_count);
i = hash_uint32(location->id) % ARRAY_SIZE(db->locations);
list_add_tail(&location->list, &db->locations[i]);
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
unlock_free:
rwlock_unlock(&db->lock);
memory_pool_free(&db->location_pool, location);
return r;
}
void db_link_visit_location(struct db *db, struct visit *visit)
{
if (!visit->location) {
visit->location = db_lookup_location(db, visit->location_id);
if (visit->location) {
list_add_tail(&visit->location_list, &visit->location->visits);
}
}
}
static void db_unlink_visit_location(struct db *db, struct visit *visit)
{
if (visit->location) {
list_del_init(&visit->location_list);
visit->location = NULL;
}
}
static void db_link_visit_user(struct db *db, struct visit *visit)
{
if (!visit->user) {
struct user *user;
user = db_lookup_user(db, visit->user_id);
if (user) {
add_user_visit_ordered(user, visit);
visit->user = user;
}
}
}
static void db_unlink_visit_user(struct db *db, struct visit *visit)
{
if (visit->user) {
list_del_init(&visit->user_list);
visit->user = NULL;
}
}
int db_new_visit(struct db *db, struct visit_data *data)
{
struct visit *visit;
struct user_visits *user_visits;
struct location_visits *location_visits;
size_t i;
int r;
if (!json_uint32_is_valid(&data->id) || !json_uint32_is_valid(&data->location)
|| !json_uint32_is_valid(&data->user) || !json_int64_is_valid(&data->visited_at)
|| !json_uint32_is_valid(&data->mark))
return EINVAL;
rwlock_lock(&db->lock);
if (db_lookup_visit(db, data->id.value)) {
r = EEXIST;
goto unlock;
}
visit = memory_pool_alloc(&db->visit_pool);
if (!visit) {
r = ENOMEM;
goto unlock;
}
visit->id = data->id.value;
visit->location_id = data->location.value;
visit->user_id = data->user.value;
visit->visited_at = data->visited_at.value;
visit->visited_at_s_len =
snprintf(visit->visited_at_s, sizeof(visit->visited_at_s), "%" PRId64, visit->visited_at);
visit->mark = data->mark.value;
visit->mark_s_len =
snprintf(visit->mark_s, sizeof(visit->mark_s), "%" PRIu32, visit->mark);
visit->user = NULL;
visit->location = NULL;
list_init(&visit->list);
list_init(&visit->user_list);
list_init(&visit->location_list);
list_init(&visit->user_visits_list);
list_init(&visit->location_visits_list);
user_visits = db_get_or_create_user_visits(db, visit->user_id);
if (!user_visits) {
r = ENOMEM;
goto unlock_free_visit;
}
location_visits = db_get_or_create_location_visits(db, visit->location_id);
if (!location_visits) {
r = ENOMEM;
goto unlock_free_visit;
}
db_link_visit_user(db, visit);
db_link_visit_location(db, visit);
list_add_tail(&visit->user_visits_list, &user_visits->visits);
list_add_tail(&visit->location_visits_list, &location_visits->visits);
if (visit->id < ARRAY_SIZE(db->visits_table))
db->visits_table[visit->id] = visit;
atomic_inc(&db->visits_count);
i = hash_uint32(visit->id) % ARRAY_SIZE(db->visits);
list_add_tail(&visit->list, &db->visits[i]);
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
unlock_free_visit:
rwlock_unlock(&db->lock);
db_unlink_visit_location(db, visit);
db_unlink_visit_user(db, visit);
memory_pool_free(&db->visit_pool, visit);
return r;
}
int db_update_user(struct db *db, uint32_t user_id, struct user_data *data)
{
struct user *user;
int r;
rwlock_lock(&db->lock);
user = db_lookup_user(db, user_id);
if (!user) {
r = ENOENT;
goto unlock;
}
if (data->email.is_null || data->first_name.is_null || data->last_name.is_null||
data->gender.is_null || data->birth_date.is_null) {
r = EINVAL;
goto unlock;
}
if (!json_string_is_valid(&data->email) && !json_string_is_valid(&data->first_name) &&
!json_string_is_valid(&data->last_name) && !json_string_is_valid(&data->gender) &&
!json_int64_is_valid(&data->birth_date)) {
r = EINVAL;
goto unlock;
}
if (json_string_is_valid(&data->gender)) {
if (strlen(data->gender.value) != 1 ||
(data->gender.value[0] != 'm' && data->gender.value[0] != 'f')) {
r = EINVAL;
goto unlock;
}
}
if (json_string_is_valid(&data->email)) {
strncpy(user->email, data->email.value, strlen(data->email.value) + 1);
}
if (json_string_is_valid(&data->first_name)) {
strncpy(user->first_name, data->first_name.value, strlen(data->first_name.value) + 1);
}
if (json_string_is_valid(&data->last_name)) {
strncpy(user->last_name, data->last_name.value, strlen(data->last_name.value) + 1);
}
if (json_int64_is_valid(&data->birth_date)) {
if (data->birth_date.value != user->birth_date) {
user->birth_date = data->birth_date.value;
r = db_get_age(db, user->birth_date, &user->age);
if (r) {
log_error("get_age error %d\n", r);
goto unlock;
}
}
}
if (json_string_is_valid(&data->gender)) {
strncpy(user->gender, data->gender.value, strlen(data->gender.value) + 1);
}
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
}
int db_update_location(struct db *db, uint32_t location_id, struct location_data *data)
{
struct location *location;
int r;
rwlock_lock(&db->lock);
location = db_lookup_location(db, location_id);
if (!location) {
r = ENOENT;
goto unlock;
}
if (data->place.is_null || data->country.is_null || data->city.is_null||
data->distance.is_null) {
r = EINVAL;
goto unlock;
}
if (!json_string_is_valid(&data->place) && !json_string_is_valid(&data->country) &&
!json_string_is_valid(&data->city) && !json_uint32_is_valid(&data->distance)) {
r = EINVAL;
goto unlock;
}
if (json_string_is_valid(&data->place)) {
location->place_len = strlen(data->place.value);
strncpy(location->place, data->place.value, location->place_len + 1);
}
if (json_string_is_valid(&data->country)) {
strncpy(location->country, data->country.value, strlen(data->country.value) + 1);
}
if (json_string_is_valid(&data->city)) {
strncpy(location->city, data->city.value, strlen(data->city.value) + 1);
}
if (json_uint32_is_valid(&data->distance)) {
location->distance = data->distance.value;
}
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
}
int db_update_visit(struct db *db, uint32_t visit_id, struct visit_data *data)
{
struct visit *visit;
struct location_visits *location_visits;
struct user_visits *user_visits;
int r;
rwlock_lock(&db->lock);
visit = db_lookup_visit(db, visit_id);
if (!visit) {
r = ENOENT;
goto unlock;
}
db_link_visit_location(db, visit);
db_link_visit_user(db, visit);
if (data->location.is_null || data->user.is_null || data->visited_at.is_null||
data->mark.is_null) {
r = EINVAL;
goto unlock;
}
if (!json_uint32_is_valid(&data->location) && !json_uint32_is_valid(&data->user) &&
!json_int64_is_valid(&data->visited_at) && !json_uint32_is_valid(&data->mark)) {
r = EINVAL;
goto unlock;
}
if (json_uint32_is_valid(&data->location)) {
if (data->location.value != visit->location_id) {
location_visits = db_get_or_create_location_visits(db, visit->location_id);
if (location_visits) {
list_del_init(&visit->location_visits_list);
}
db_unlink_visit_location(db, visit);
visit->location_id = data->location.value;
location_visits = db_get_or_create_location_visits(db, visit->location_id);
if (location_visits) {
list_add_tail(&visit->location_visits_list, &location_visits->visits);
}
db_link_visit_location(db, visit);
}
}
if (json_uint32_is_valid(&data->user)) {
if (data->user.value != visit->user_id) {
user_visits = db_get_or_create_user_visits(db, visit->user_id);
if (user_visits) {
list_del_init(&visit->user_visits_list);
}
db_unlink_visit_user(db, visit);
visit->user_id = data->user.value;
user_visits = db_get_or_create_user_visits(db, visit->user_id);
if (user_visits) {
list_add_tail(&visit->user_visits_list, &user_visits->visits);
}
db_link_visit_user(db, visit);
}
}
if (json_int64_is_valid(&data->visited_at)) {
if (visit->user)
list_del_init(&visit->user_list);
visit->visited_at = data->visited_at.value;
visit->visited_at_s_len =
snprintf(visit->visited_at_s, sizeof(visit->visited_at_s), "%" PRId64, visit->visited_at);
if (visit->user)
add_user_visit_ordered(visit->user, visit);
}
if (json_uint32_is_valid(&data->mark)) {
visit->mark = data->mark.value;
visit->mark_s_len = snprintf(visit->mark_s, sizeof(visit->mark_s), "%" PRId32, visit->mark);
}
r = 0;
unlock:
rwlock_unlock(&db->lock);
return r;
}
int db_get_user_visits(struct db *db, uint32_t user_id,
int64_t *from_date, int64_t *to_date,
const char *country,
uint32_t *to_distance,
struct sbuf *buf)
{
struct user *user;
struct visit *visit;
struct location *location;
int r, nr_visits;
rwlock_read_lock(&db->lock);
user = db_lookup_user(db, user_id);
if (!user) {
r = ENOENT;
goto unlock;
}
r = sbuf_append(buf, "{\"visits\":[\n", 12);
if (r)
goto unlock;
nr_visits = 0;
list_for_each_entry(visit, &user->visits, user_list) {
if (from_date) {
if (visit->visited_at <= *from_date) {
continue;
}
}
if (to_date) {
if (visit->visited_at >= *to_date) {
break;
}
}
location = visit->location;
if (unlikely(!location))
continue;
if (country) {
if (0 != strncmp(location->country, country, strlen(location->country) + 1)) {
continue;
}
}
if (to_distance) {
if (location->distance >= *to_distance) {
continue;
}
}
r = sbuf_append(buf, "{\"mark\":", 8);
if (r)
goto unlock;
r = sbuf_append(buf, visit->mark_s, visit->mark_s_len);
if (r)
goto unlock;
r = sbuf_append(buf, ",\"place\":\"", 10);
if (r)
goto unlock;
r = sbuf_append(buf, location->place, location->place_len);
if (r)
goto unlock;
r = sbuf_append(buf, "\",\"visited_at\":", 15);
if (r)
goto unlock;
r = sbuf_append(buf, visit->visited_at_s, visit->visited_at_s_len);
if (r)