From f08bb5adf425269a00e760c669bdeeadc8c7e112 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 15 Jun 2016 14:22:23 +0500 Subject: - Work only with cached devices from everywhere - Some optimization --- src/org/traccar/BaseEventHandler.java | 8 +++++++- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/database/DataManager.java | 25 +++++++++++++++++++++++- src/org/traccar/database/GeofenceManager.java | 2 +- src/org/traccar/database/PermissionsManager.java | 2 +- src/org/traccar/events/GeofenceEventHandler.java | 10 ++++------ src/org/traccar/events/MotionEventHandler.java | 5 ++--- 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index 16d911dac..78542b33a 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -14,10 +14,16 @@ public abstract class BaseEventHandler extends BaseDataHandler { return isLastPosition; } + private Device device; + + public Device getDevice() { + return device; + } + @Override protected Position handlePosition(Position position) { - Device device = Context.getDataManager().getDeviceById(position.getDeviceId()); + device = Context.getDataManager().getDeviceById(position.getDeviceId()); if (device != null) { long lastPositionId = device.getPositionId(); if (position.getId() == lastPositionId) { diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 26880c1f8..fcbeed97b 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -44,7 +44,7 @@ public class DeviceResource extends BaseResource { @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { if (all) { Context.getPermissionsManager().checkAdmin(getUserId()); - return Context.getDataManager().getAllDevices(); + return Context.getDataManager().getAllDevicesCached(); } else { if (userId == 0) { userId = getUserId(); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a32d31d68..f5df1b84b 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -342,11 +342,34 @@ public class DataManager implements IdentityManager { .executeQuery(GroupPermission.class); } - public Collection getAllDevices() throws SQLException { + private Collection getAllDevices() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) .executeQuery(Device.class); } + public Collection getAllDevicesCached() { + boolean forceUpdate; + devicesLock.readLock().lock(); + try { + forceUpdate = devicesById.values().isEmpty(); + } finally { + devicesLock.readLock().unlock(); + } + + try { + updateDeviceCache(forceUpdate); + } catch (SQLException e) { + Log.warning(e); + } + + devicesLock.readLock().lock(); + try { + return devicesById.values(); + } finally { + devicesLock.readLock().unlock(); + } + } + public Collection getDevices(long userId) throws SQLException { Collection devices = new ArrayList<>(); for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) { diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index b551bc467..0119a62ca 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -113,7 +113,7 @@ public class GeofenceManager { .add(deviceGeofence.getGeofenceId()); } - for (Device device : dataManager.getAllDevices()) { + for (Device device : dataManager.getAllDevicesCached()) { long groupId = device.getGroupId(); while (groupId != 0) { getDeviceGeofences(deviceGeofencesWithGroups, diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 96a6488ef..b6dd2e2a9 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -78,7 +78,7 @@ public class PermissionsManager { users.put(user.getId(), user); } - GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), dataManager.getAllDevices()); + GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), dataManager.getAllDevicesCached()); for (GroupPermission permission : dataManager.getGroupPermissions()) { Set userGroupPermissions = getGroupPermissions(permission.getUserId()); Set userDevicePermissions = getDevicePermissions(permission.getUserId()); diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index ed63b6f7d..56029fced 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -10,7 +10,6 @@ import org.traccar.Context; import org.traccar.database.DataManager; import org.traccar.database.GeofenceManager; import org.traccar.helper.Log; -import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -32,21 +31,20 @@ public class GeofenceEventHandler extends BaseEventHandler { return null; } - Device device = dataManager.getDeviceById(position.getDeviceId()); - if (device == null) { + if (getDevice() == null) { return null; } List currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List oldGeofences = new ArrayList(); - if (device.getGeofenceIds() != null) { - oldGeofences.addAll(device.getGeofenceIds()); + if (getDevice().getGeofenceIds() != null) { + oldGeofences.addAll(getDevice().getGeofenceIds()); } List newGeofences = new ArrayList(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); - device.setGeofenceIds(currentGeofences); + getDevice().setGeofenceIds(currentGeofences); Collection events = new ArrayList<>(); try { diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 54b6b922d..2ba5979e3 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -29,11 +29,10 @@ public class MotionEventHandler extends BaseEventHandler { double speed = position.getSpeed(); boolean valid = position.getValid(); - Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); - if (device == null) { + if (getDevice() == null) { return null; } - String motion = device.getMotion(); + String motion = getDevice().getMotion(); if (motion == null) { motion = Device.STATUS_STOPPED; } -- cgit v1.2.3