aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-09 06:39:10 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-09 06:39:10 -0700
commitc4ff983ffe9a03a57d3ab0596abe8bce08c1ae2e (patch)
tree0cb36557ca3cbad928099edb37e7a8d2101b4f19 /src/main
parent65d3654dcca878cfd839e40c0dd020da199be09e (diff)
downloadtrackermap-server-c4ff983ffe9a03a57d3ab0596abe8bce08c1ae2e.tar.gz
trackermap-server-c4ff983ffe9a03a57d3ab0596abe8bce08c1ae2e.tar.bz2
trackermap-server-c4ff983ffe9a03a57d3ab0596abe8bce08c1ae2e.zip
Add position cache
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/traccar/MainEventHandler.java2
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java3
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java33
3 files changed, 36 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java
index f04307fdb..bb84a09d2 100644
--- a/src/main/java/org/traccar/MainEventHandler.java
+++ b/src/main/java/org/traccar/MainEventHandler.java
@@ -28,6 +28,7 @@ import org.traccar.database.StatisticsManager;
import org.traccar.helper.DateUtil;
import org.traccar.helper.NetworkUtil;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
import javax.inject.Inject;
@@ -60,6 +61,7 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
Position position = (Position) msg;
try {
Context.getDeviceManager().updateLatestPosition(position);
+ Main.getInjector().getInstance(CacheManager.class).updatePosition(position);
} catch (StorageException error) {
LOGGER.warn("Failed to update device", error);
}
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index b1ea0b8b7..0bac8f642 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.Main;
import org.traccar.config.Config;
import org.traccar.Context;
import org.traccar.config.Keys;
@@ -36,6 +37,7 @@ import org.traccar.model.DeviceAccumulators;
import org.traccar.model.Group;
import org.traccar.model.Position;
import org.traccar.model.Server;
+import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
public class DeviceManager extends BaseObjectManager<Device> implements IdentityManager {
@@ -406,6 +408,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
}
getDataManager().addObject(last);
updateLatestPosition(last);
+ Main.getInjector().getInstance(CacheManager.class).updatePosition(last);
} else {
throw new IllegalArgumentException();
}
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index a934431be..4b42ea4e5 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -23,6 +23,7 @@ import org.traccar.model.Geofence;
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;
@@ -57,6 +58,7 @@ public class CacheManager {
private final Map<CacheKey, CacheValue> deviceCache = 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<>();
@@ -89,6 +91,15 @@ public class CacheManager {
}
}
+ public Position getPosition(long deviceId) {
+ try {
+ lock.readLock().lock();
+ return devicePositions.get(deviceId);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
public Server getServer() {
try {
lock.readLock().lock();
@@ -136,6 +147,17 @@ public class CacheManager {
}
}
+ public void updatePosition(Position position) {
+ try {
+ lock.writeLock().lock();
+ if (deviceLinks.containsKey(position.getDeviceId())) {
+ devicePositions.put(position.getDeviceId(), position);
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
public <T extends BaseModel> void updateOrInvalidate(Class<T> clazz, long id) throws StorageException {
var object = storage.getObject(clazz, new Request(
new Columns.All(), new Condition.Equals("id", "id", id)));
@@ -201,8 +223,9 @@ public class CacheManager {
private void unsafeAddDevice(long deviceId) throws StorageException {
Map<Class<? extends BaseModel>, List<Long>> links = new HashMap<>();
- addObject(deviceId, storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", deviceId))));
+ Device device = storage.getObject(Device.class, new Request(
+ new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ addObject(deviceId, device);
for (Class<? extends BaseModel> clazz : CLASSES) {
var objects = storage.getObjects(clazz, new Request(
@@ -226,6 +249,11 @@ public class CacheManager {
}
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) {
@@ -237,6 +265,7 @@ public class CacheManager {
return value.getReferences().size() > 0 ? value : null;
});
}));
+ devicePositions.remove(deviceId);
}
private void invalidate(CacheKey... keys) throws StorageException {