-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocation.go
113 lines (90 loc) · 2.28 KB
/
location.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
package main
import (
"log"
"sync"
)
var _ = log.Println
type location_update struct {
Id * int
Place * string
Country * string
City * string
Distance * int
}
type location struct {
Id int
Place []byte
Country []byte
City []byte
Distance int
Idx LocationsAvgIndex
Deps map[*usersVisits]bool
}
var locations map[int]*location
var locationsMutex sync.RWMutex
const locationsMaxCount = 763407
var locationsCount int
var locations1[locationsMaxCount+1]location
// Note: as there are no write requests (POST) on phases 1 and 3, we may skip mutex locking
func getLocation(Location int) (*location) {
if Location <= locationsMaxCount {
if locations1[Location].Id == 0 {
return nil
}
return &locations1[Location]
}
return locations[Location]
}
func getLocationSync(Location int) (*location) {
if Location <= locationsMaxCount {
if locations1[Location].Id == 0 {
return nil
}
return &locations1[Location]
}
locationsMutex.RLock()
l := locations[Location]
locationsMutex.RUnlock()
return l
}
func getLocationInsert(Location int) (*location) {
var l * location
if Location > locationsMaxCount {
var ln location
l = &ln
locations[Location] = l
} else {
l = &locations1[Location]
}
return l
}
func getLocationInsertSync(Location int) (*location) {
var l * location
if Location > locationsMaxCount {
var ln location
l = &ln
locationsMutex.Lock()
locations[Location] = l
locationsMutex.Unlock()
} else {
l = &locations1[Location]
}
return l
}
func insertLocationData(l * location, lu * location_update) {
l.Id = *lu.Id
l.Place = []byte(*lu.Place)
l.Country = []byte(*lu.Country)
l.City = []byte(*lu.City)
l.Distance = *lu.Distance
l.Idx = NewLocationsAvgIndex()
l.Deps = make(map[*usersVisits]bool, 20)
}
func loadLocation(Location int, lu * location_update) {
l := getLocationInsert(Location)
insertLocationData(l, lu)
}
func insertLocation(Location int, lu * location_update) {
l := getLocationInsertSync(Location)
insertLocationData(l, lu)
}