-
Notifications
You must be signed in to change notification settings - Fork 9
/
location.go
173 lines (147 loc) · 4.11 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
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
package main
import (
"bytes"
"log"
"strconv"
)
type (
Location struct {
Id int32
Place []byte
CountryIdx int32
City []byte
Distance int32
cache LocationsAvg
}
)
func (l *Location) Reset() {
l.Id = 0
l.Place = l.Place[:0]
l.CountryIdx = 0
l.City = l.City[:0]
l.Distance = 0
l.cache.locations = l.cache.locations[:0]
}
func (l *Location) Parse(buf []byte) bool {
changed := false
err := ParseItem(buf, func(key, value []byte, valueType JSValueType) bool {
changed = true // проверка, что не совсем пустой buf пришел
if bytes.Equal(key, strId) {
if valueType != jsValueTypeNumeric {
return false
} else if i64, ok := byteSliceToInt64(value); !ok {
return false
} else {
l.Id = int32(i64)
}
} else if bytes.Equal(key, strPlace) {
if valueType != jsValueTypeString {
return false
}
l.Place = append(l.Place[0:0], value...)
} else if bytes.Equal(key, strCountry) {
if valueType != jsValueTypeString {
return false
}
country := utf8Unescaped(value)
idx, _ := indexCountry.Add(country)
l.CountryIdx = int32(idx)
} else if bytes.Equal(key, strCity) {
if valueType != jsValueTypeString {
return false
}
l.City = append(l.City[0:0], value...)
} else if bytes.Equal(key, strDistance) {
if valueType != jsValueTypeNumeric {
return false
} else if i64, ok := byteSliceToInt64(value); !ok {
return false
} else {
l.Distance = int32(i64)
}
} // else { // неизвестный ключ
return true
})
return (err == nil) && changed
}
func (l *Location) Serialize(buf []byte) []byte {
country := indexCountry.GetByIdx(int(l.CountryIdx))
buf = append(buf, `{"id":`...)
buf = strconv.AppendInt(buf, int64(l.Id), 10)
buf = append(buf, `,"place":"`...)
buf = append(buf, l.Place...)
buf = append(buf, `","country":"`...)
buf = append(buf, country...)
buf = append(buf, `","city":"`...)
buf = append(buf, l.City...)
buf = append(buf, `","distance":`...)
buf = strconv.AppendInt(buf, int64(l.Distance), 10)
buf = append(buf, '}')
return buf
}
func (l *Location) CheckFields(update bool) bool {
/*if utf8EscapedStringLen(l.Country) > 50 {
return false
} else if utf8EscapedStringLen(l.City) > 50 {
return false
}*/
if !update && (len(l.City) == 0 || l.CountryIdx == 0 || l.Distance == 0 || len(l.Place) == 0) {
// при создании должны передаваться все поля
return false
}
if update && (l.Id != 0) {
// id не может обновляться
return false
}
return true
}
func (l *Location) Update(update *Location) bool {
/*
Если меняется Distance:
- в UserVisits (Visit(Location.cache.visitId) => User(visit.User).cache.distance)
поменять поля distance, countryIdx
*/
placeChanged := (len(update.Place) != 0) && !bytes.Equal(l.Place, update.Place)
if placeChanged {
l.Place = append(l.Place[:0], update.Place...)
}
countryChanged := (update.CountryIdx != 0) && (l.CountryIdx != update.CountryIdx)
if countryChanged {
l.CountryIdx = update.CountryIdx
}
if len(update.City) != 0 {
l.City = append(l.City[:0], update.City...)
}
distanceChanged := (update.Distance != 0) && (l.Distance != update.Distance)
if distanceChanged {
l.Distance = update.Distance
}
if placeChanged || distanceChanged || countryChanged {
l.cacheUpdateDistanceAndCountryIdxAndPlace()
}
return true
}
func (l *Location) cacheUpdateDistanceAndCountryIdxAndPlace() {
for _, la := range l.cache.locations {
if visit := indexVisit.Get(la.visitId); visit == nil {
log.Println(`WTF visit nil in location cache`, la.visitId)
} else if user := indexUser.Get(visit.User); user == nil {
log.Println(`WTF user nil in location cache`, la.visitId, visit.User)
} else {
for i, uv := range user.cache.visits {
if uv.visitId != visit.Id {
continue
}
if l.Distance != 0 {
user.cache.visits[i].distance = l.Distance
}
if l.CountryIdx != 0 {
user.cache.visits[i].countryIdx = l.CountryIdx
}
if len(l.Place) != 0 {
user.cache.visits[i].place = l.Place
}
}
}
}
}