/
mem.go
275 lines (221 loc) · 7.03 KB
/
mem.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
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
/*
* Copyright 2018 Johannes Donath <johannesd@torchmind.com>
* and other copyright owners as documented in the project's IP log.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package storage
import (
"strings"
"time"
"github.com/dotStart/Stockpile/entity"
"github.com/dotStart/Stockpile/stockpile/server"
"github.com/google/uuid"
"github.com/op/go-logging"
)
type MemoryStorageBackend struct {
cfg *server.Config
logger *logging.Logger
profileId map[string][]expirationWrapper
nameHistory map[uuid.UUID]*expirationWrapper
profile map[uuid.UUID]*expirationWrapper
blacklist *expirationWrapper
}
// creates a new memory based storage backend
func NewMemoryStorageBackend(cfg *server.Config) (StorageBackend, error) {
return &MemoryStorageBackend{
cfg: cfg,
logger: logging.MustGetLogger("memdb"),
profileId: make(map[string][]expirationWrapper),
nameHistory: make(map[uuid.UUID]*expirationWrapper),
profile: make(map[uuid.UUID]*expirationWrapper),
}, nil
}
func (m *MemoryStorageBackend) Close() error {
return nil
}
func (m *MemoryStorageBackend) GetProfileId(name string, at time.Time) (*entity.ProfileId, error) {
m.clearExpiredEntries()
name = strings.ToLower(name)
m.logger.Debugf("checking profile associations for \"%s\" at time %s", name, at)
mappings := m.profileId[name]
if mappings == nil {
m.logger.Debugf("no associations for \"%s\"", name)
return nil, nil
}
for _, exp := range mappings {
association := exp.content.(*entity.ProfileId)
if association.IsValid(at) {
m.logger.Debugf("association to profile %s matches", association.Id)
return association, nil
} else {
m.logger.Debugf("association to profile %s is invalid", association.Id)
}
}
return nil, nil
}
func (m *MemoryStorageBackend) PutProfileId(profileId *entity.ProfileId) error {
m.clearExpiredEntries()
name := strings.ToLower(profileId.Name)
m.logger.Debugf("updating association for name \"%s\" to profile %s at time %s (valid until %s)", profileId.Name, profileId.Id, profileId.LastSeenAt, profileId.ValidUntil)
mappings := m.profileId[name]
found := false
if mappings != nil {
for _, e := range mappings {
entry := e.content.(*entity.ProfileId)
if entry.IsOverlappingWith(profileId) {
entry.UpdateExpiration(profileId.LastSeenAt)
found = true
}
}
} else {
mappings = make([]expirationWrapper, 0)
}
if !found {
mappings = append(mappings, expirationWrapper{
content: profileId,
createdAt: time.Now(),
})
}
m.profileId[name] = mappings
return nil
}
func (m *MemoryStorageBackend) PurgeProfileId(name string, at time.Time) error {
m.clearExpiredEntries()
m.logger.Debugf("purging profile associations for \"%s\" at time %s", name, at)
mappings := m.profileId[name]
if mappings == nil {
m.logger.Debugf("No associations for \"%s\"", name)
return nil
}
if at.Unix() == -1 {
delete(m.profileId, name)
return nil
}
for i := 0; i < len(mappings); {
exp := mappings[i]
association := exp.content.(*entity.ProfileId)
if association.IsValid(at) {
m.logger.Debugf("purging association to profile %s", association.Id)
mappings = append(mappings[:i], mappings[:i+1]...)
continue
}
i++
}
m.profileId[name] = mappings
return nil
}
func (m *MemoryStorageBackend) GetNameHistory(id uuid.UUID) (*entity.NameChangeHistory, error) {
m.clearExpiredEntries()
exp := m.nameHistory[id]
if exp == nil {
return nil, nil
}
return exp.content.(*entity.NameChangeHistory), nil
}
func (m *MemoryStorageBackend) PutNameHistory(id uuid.UUID, history *entity.NameChangeHistory) error {
m.clearExpiredEntries()
m.logger.Debugf("storing history for profile %s (consisting of %d elements)", id, len(history.History))
m.nameHistory[id] = &expirationWrapper{
content: history,
createdAt: time.Now(),
}
return nil
}
func (m *MemoryStorageBackend) PurgeNameHistory(id uuid.UUID) error {
m.clearExpiredEntries()
m.logger.Debugf("purging history for profile %s", id)
delete(m.nameHistory, id)
return nil
}
func (m *MemoryStorageBackend) GetProfile(id uuid.UUID) (*entity.Profile, error) {
m.clearExpiredEntries()
exp := m.profile[id]
if exp == nil {
return nil, nil
}
return exp.content.(*entity.Profile), nil
}
func (m *MemoryStorageBackend) PutProfile(profile *entity.Profile) error {
m.clearExpiredEntries()
m.logger.Debugf("storing profile %s", profile.Id)
m.profile[profile.Id] = &expirationWrapper{
content: profile,
createdAt: time.Now(),
}
return nil
}
func (m *MemoryStorageBackend) PurgeProfile(id uuid.UUID) error {
m.clearExpiredEntries()
m.logger.Debugf("purging profile %s", id)
delete(m.profile, id)
return nil
}
func (m *MemoryStorageBackend) GetBlacklist() (*entity.Blacklist, error) {
if m.blacklist == nil {
return nil, nil
}
return m.blacklist.content.(*entity.Blacklist), nil
}
func (m *MemoryStorageBackend) PutBlacklist(blacklist *entity.Blacklist) error {
m.blacklist = &expirationWrapper{
content: blacklist,
createdAt: time.Now(),
}
return nil
}
func (m *MemoryStorageBackend) PurgeBlacklist() error {
m.logger.Debugf("purging blacklist")
m.blacklist = nil
return nil
}
// clears all expired entries from the database
func (m *MemoryStorageBackend) clearExpiredEntries() { // TODO: run on a timer instead?
m.logger.Debug("purging expired data")
deletedProfileIds := 0
deletedNameHistories := 0
deletedProfiles := 0
deletedBlacklists := 0
for profileId, mappings := range m.profileId {
for i := 0; i < len(mappings); {
exp := mappings[i]
if !exp.isValid(m.cfg.Ttl.Name) {
deletedProfileIds++
if len(mappings) == 1 {
delete(m.profileId, profileId)
continue
}
mappings = append(mappings[:i], mappings[i+1:]...)
continue
}
i++
}
}
for key, history := range m.nameHistory {
if !history.isValid(m.cfg.Ttl.NameHistory) {
deletedNameHistories++
delete(m.nameHistory, key)
}
}
for key, profile := range m.profile {
if !profile.isValid(m.cfg.Ttl.Profile) {
deletedProfiles++
delete(m.profile, key)
}
}
if m.blacklist != nil && m.blacklist.isValid(m.cfg.Ttl.Blacklist) {
deletedBlacklists = 1
m.blacklist = nil
}
m.logger.Debugf("removed %d profile Ids, %d name histories, %d profiles and %d blacklists from memory", deletedProfileIds, deletedNameHistories, deletedProfiles, deletedBlacklists)
}