-
Notifications
You must be signed in to change notification settings - Fork 1
/
visit.go
126 lines (99 loc) · 2.34 KB
/
visit.go
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
package main
import (
"log"
"sync"
)
var _ = log.Println
type visit_update struct {
Id * int
Location * int
User * int
Mark * uint8
Visited_at * int
}
type visit struct {
Id int
Location int
User int
Mark uint8
Visited_at int
}
var visits map[int]*visit
var visitsMutex sync.RWMutex
const visitsMaxCount = 10000700
var visitsCount int
var visits1[visitsMaxCount+1]visit
// Note: as there are no write requests (POST) on phases 1 and 3, we may skip mutex locking
func getVisit(Visit int) (*visit) {
if Visit <= visitsMaxCount {
if visits1[Visit].Id == 0 {
return nil
}
return &visits1[Visit]
}
return visits[Visit]
}
func getVisitSync(Visit int) (*visit) {
if Visit <= visitsMaxCount {
if visits1[Visit].Id == 0 {
return nil
}
return &visits1[Visit]
}
visitsMutex.RLock()
u := visits[Visit]
visitsMutex.RUnlock()
return u
}
func getVisitInsert(Visit int) (*visit) {
var v * visit
if Visit > visitsMaxCount {
var vn visit
v = &vn
visits[Visit] = v
} else {
v = &visits1[Visit]
}
return v
}
func getVisitInsertSync(Visit int) (*visit) {
var v * visit
if Visit > visitsMaxCount {
var vn visit
v = &vn
visitsMutex.Lock()
visits[Visit] = v
visitsMutex.Unlock()
} else {
v = &visits1[Visit]
}
return v
}
func insertVisitData(v * visit, vu * visit_update) {
Visit := *vu.Id
v.Id = Visit
v.Location = *vu.Location
v.User = *vu.User
v.Mark = *vu.Mark
v.Visited_at = *vu.Visited_at
// Add to index
User := v.User
Location := v.Location
u := getUser(User)
l := getLocation(Location)
z := usersVisits{Visit, l.Distance, string(l.Country), v.Mark, l.Place}
u.Idx.Insert(v.Visited_at, &z)
l.Deps[&z] = true
Age := (now - u.Birth_date) / (365.25 * 24 * 3600)
z2 := locationsAvg{v.Visited_at, Age, u.Gender, int(v.Mark)}
l.Idx.Insert(Visit, &z2)
u.Deps[&z2] = true
}
func loadVisit(Visit int, vu * visit_update) {
v := getVisitInsert(Visit)
insertVisitData(v, vu)
}
func insertVisit(Visit int, vu * visit_update) {
v := getVisitInsertSync(Visit)
insertVisitData(v, vu)
}