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
|
/*
* Copyright 2022 Anton Tananaev (anton@traccar.org)
*
* 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 org.traccar.session.cache;
import org.traccar.broadcast.BroadcastInterface;
import org.traccar.broadcast.BroadcastService;
import org.traccar.config.Config;
import org.traccar.model.Attribute;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Driver;
import org.traccar.model.Geofence;
import org.traccar.model.Group;
import org.traccar.model.GroupedModel;
import org.traccar.model.Maintenance;
import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.Server;
import org.traccar.model.User;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
@Singleton
public class CacheManager implements BroadcastInterface {
private static final int GROUP_DEPTH_LIMIT = 3;
private static final Collection<Class<? extends BaseModel>> CLASSES = Arrays.asList(
Attribute.class, Driver.class, Geofence.class, Maintenance.class, Notification.class);
private final Config config;
private final Storage storage;
private final BroadcastService broadcastService;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<CacheKey, CacheValue> deviceCache = new HashMap<>();
private final Map<Long, Integer> deviceReferences = new HashMap<>();
private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>();
private final Map<Long, Position> devicePositions = new HashMap<>();
private Server server;
private final Map<Long, List<User>> notificationUsers = new HashMap<>();
@Inject
public CacheManager(Config config, Storage storage, BroadcastService broadcastService) throws StorageException {
this.config = config;
this.storage = storage;
this.broadcastService = broadcastService;
invalidateServer();
invalidateUsers();
broadcastService.registerListener(this);
}
public Config getConfig() {
return config;
}
public <T extends BaseModel> T getObject(Class<T> clazz, long id) {
try {
lock.readLock().lock();
var cacheValue = deviceCache.get(new CacheKey(clazz, id));
return cacheValue != null ? cacheValue.getValue() : null;
} finally {
lock.readLock().unlock();
}
}
public <T extends BaseModel> List<T> getDeviceObjects(long deviceId, Class<T> clazz) {
try {
lock.readLock().lock();
return deviceLinks.get(deviceId).get(clazz).stream()
.map(id -> deviceCache.get(new CacheKey(clazz, id)).<T>getValue())
.collect(Collectors.toList());
} finally {
lock.readLock().unlock();
}
}
public Position getPosition(long deviceId) {
try {
lock.readLock().lock();
return devicePositions.get(deviceId);
} finally {
lock.readLock().unlock();
}
}
public Server getServer() {
try {
lock.readLock().lock();
return server;
} finally {
lock.readLock().unlock();
}
}
public List<User> getNotificationUsers(long notificationId, long deviceId) {
try {
lock.readLock().lock();
var users = deviceLinks.get(deviceId).get(User.class).stream()
.collect(Collectors.toUnmodifiableSet());
return notificationUsers.get(notificationId).stream()
.filter(user -> users.contains(user.getId()))
.collect(Collectors.toUnmodifiableList());
} finally {
lock.readLock().unlock();
}
}
public Driver findDriverByUniqueId(long deviceId, String driverUniqueId) {
return getDeviceObjects(deviceId, Driver.class).stream()
.filter(driver -> driver.getUniqueId().equals(driverUniqueId))
.findFirst()
.orElse(null);
}
public void addDevice(long deviceId) throws StorageException {
try {
lock.writeLock().lock();
Integer references = deviceReferences.get(deviceId);
if (references != null) {
references += 1;
} else {
unsafeAddDevice(deviceId);
references = 1;
}
deviceReferences.put(deviceId, references);
} finally {
lock.writeLock().unlock();
}
}
public void removeDevice(long deviceId) {
try {
lock.writeLock().lock();
Integer references = deviceReferences.get(deviceId);
if (references != null) {
references -= 1;
if (references <= 0) {
unsafeRemoveDevice(deviceId);
deviceReferences.remove(deviceId);
} else {
deviceReferences.put(deviceId, references);
}
}
} finally {
lock.writeLock().unlock();
}
}
public void updatePosition(Position position) {
try {
lock.writeLock().lock();
if (deviceLinks.containsKey(position.getDeviceId())) {
devicePositions.put(position.getDeviceId(), position);
}
} finally {
lock.writeLock().unlock();
}
}
@Override
public void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) {
try {
var object = storage.getObject(clazz, new Request(
new Columns.All(), new Condition.Equals("id", "id", id)));
if (object != null) {
updateOrInvalidate(local, object);
} else {
invalidate(clazz, id);
}
} catch (StorageException e) {
throw new RuntimeException(e);
}
}
public <T extends BaseModel> void updateOrInvalidate(boolean local, T object) throws StorageException {
if (local) {
broadcastService.invalidateObject(true, object.getClass(), object.getId());
}
boolean invalidate = false;
var before = getObject(object.getClass(), object.getId());
if (before == null) {
return;
} else if (object instanceof GroupedModel) {
if (((GroupedModel) before).getGroupId() != ((GroupedModel) object).getGroupId()) {
invalidate = true;
}
}
if (invalidate) {
invalidate(object.getClass(), object.getId());
} else {
try {
lock.writeLock().lock();
deviceCache.get(new CacheKey(object.getClass(), object.getId())).setValue(object);
} finally {
lock.writeLock().unlock();
}
}
}
public <T extends BaseModel> void invalidate(Class<T> clazz, long id) throws StorageException {
invalidate(new CacheKey(clazz, id));
}
@Override
public void invalidatePermission(
boolean local,
Class<? extends BaseModel> clazz1, long id1,
Class<? extends BaseModel> clazz2, long id2) {
if (local) {
broadcastService.invalidatePermission(true, clazz1, id1, clazz2, id2);
}
try {
invalidate(new CacheKey(clazz1, id1), new CacheKey(clazz2, id2));
} catch (StorageException e) {
throw new RuntimeException(e);
}
}
private void invalidateServer() throws StorageException {
server = storage.getObject(Server.class, new Request(new Columns.All()));
}
private void invalidateUsers() throws StorageException {
notificationUsers.clear();
Map<Long, User> users = new HashMap<>();
storage.getObjects(User.class, new Request(new Columns.All()))
.forEach(user -> users.put(user.getId(), user));
storage.getPermissions(User.class, Notification.class).forEach(permission -> {
long notificationId = permission.getPropertyId();
var user = users.get(permission.getOwnerId());
notificationUsers.computeIfAbsent(notificationId, k -> new LinkedList<>()).add(user);
});
}
private void addObject(long deviceId, BaseModel object) {
deviceCache.computeIfAbsent(new CacheKey(object), k -> new CacheValue(object)).retain(deviceId);
}
private void unsafeAddDevice(long deviceId) throws StorageException {
Map<Class<? extends BaseModel>, List<Long>> links = new HashMap<>();
Device device = storage.getObject(Device.class, new Request(
new Columns.All(), new Condition.Equals("id", "id", deviceId)));
if (device != null) {
addObject(deviceId, device);
int groupDepth = 0;
long groupId = device.getGroupId();
while (groupDepth < GROUP_DEPTH_LIMIT && groupId > 0) {
Group group = storage.getObject(Group.class, new Request(
new Columns.All(), new Condition.Equals("id", "id", groupId)));
links.computeIfAbsent(Group.class, k -> new LinkedList<>()).add(group.getId());
addObject(deviceId, group);
groupId = group.getGroupId();
groupDepth += 1;
}
for (Class<? extends BaseModel> clazz : CLASSES) {
var objects = storage.getObjects(clazz, new Request(
new Columns.All(), new Condition.Permission(Device.class, deviceId, clazz)));
links.put(clazz, objects.stream().map(BaseModel::getId).collect(Collectors.toList()));
objects.forEach(object -> addObject(deviceId, object));
}
var users = storage.getObjects(User.class, new Request(
new Columns.All(), new Condition.Permission(User.class, Device.class, deviceId)));
links.put(User.class, users.stream().map(BaseModel::getId).collect(Collectors.toList()));
for (var user : users) {
addObject(deviceId, user);
var notifications = storage.getObjects(Notification.class, new Request(
new Columns.All(), new Condition.Permission(User.class, user.getId(), Notification.class)));
notifications.stream()
.filter(Notification::getAlways)
.forEach(object -> {
links.computeIfAbsent(Notification.class, k -> new LinkedList<>()).add(object.getId());
addObject(deviceId, object);
});
}
deviceLinks.put(deviceId, links);
if (device.getPositionId() > 0) {
devicePositions.put(deviceId, storage.getObject(Position.class, new Request(
new Columns.All(), new Condition.Equals("id", "id", device.getPositionId()))));
}
}
}
private void unsafeRemoveDevice(long deviceId) {
deviceCache.remove(new CacheKey(Device.class, deviceId));
deviceLinks.remove(deviceId).forEach((clazz, ids) -> ids.forEach(id -> {
var key = new CacheKey(clazz, id);
deviceCache.computeIfPresent(key, (k, value) -> {
value.release(deviceId);
return value.getReferences().size() > 0 ? value : null;
});
}));
devicePositions.remove(deviceId);
}
private void invalidate(CacheKey... keys) throws StorageException {
try {
lock.writeLock().lock();
unsafeInvalidate(keys);
} finally {
lock.writeLock().unlock();
}
}
private void unsafeInvalidate(CacheKey[] keys) throws StorageException {
boolean invalidateServer = false;
boolean invalidateUsers = false;
Set<Long> linkedDevices = new HashSet<>();
for (var key : keys) {
if (key.classIs(Server.class)) {
invalidateServer = true;
} else {
if (key.classIs(User.class) || key.classIs(Notification.class)) {
invalidateUsers = true;
}
deviceCache.computeIfPresent(key, (k, value) -> {
linkedDevices.addAll(value.getReferences());
return value;
});
}
}
for (long deviceId : linkedDevices) {
unsafeRemoveDevice(deviceId);
unsafeAddDevice(deviceId);
}
if (invalidateServer) {
invalidateServer();
}
if (invalidateUsers) {
invalidateUsers();
}
}
}
|