diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-07-11 23:23:21 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-11 23:23:21 +1200 |
commit | a4a127b5255cd62aae25419dbf289a926c5adbfd (patch) | |
tree | 05b99cf458f597806cde9617fe28c5bccd557c22 /src | |
parent | 817360eee7dba57cf8b58e858f7acf60f27b5e68 (diff) | |
parent | 0bda01815c51b1f1e1d3e3eda592219eb39bf78b (diff) | |
download | trackermap-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.tar.gz trackermap-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.tar.bz2 trackermap-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.zip |
Merge pull request #2099 from Abyss777/fix_groups_cache
Revert back groups cache and split refresh
Diffstat (limited to 'src')
4 files changed, 57 insertions, 18 deletions
diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java index 329c72b07..3a82845f5 100644 --- a/src/org/traccar/api/resource/GeofencePermissionResource.java +++ b/src/org/traccar/api/resource/GeofencePermissionResource.java @@ -39,7 +39,7 @@ public class GeofencePermissionResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); + Context.getGeofenceManager().refreshUserGeofences(); return Response.ok(entity).build(); } @@ -49,7 +49,7 @@ public class GeofencePermissionResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); + Context.getGeofenceManager().refreshUserGeofences(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index 44f6fc2f5..960ab813f 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -78,7 +78,7 @@ public class GeofenceResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDataManager().addGeofence(entity); Context.getDataManager().linkGeofence(getUserId(), entity.getId()); - Context.getGeofenceManager().refresh(); + Context.getGeofenceManager().refreshGeofences(); return Response.ok(entity).build(); } @@ -97,7 +97,7 @@ public class GeofenceResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGeofence(getUserId(), id); Context.getDataManager().removeGeofence(id); - Context.getGeofenceManager().refresh(); + Context.getGeofenceManager().refreshGeofences(); return Response.noContent().build(); } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 2d3b75db7..b3f24383f 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -241,7 +241,7 @@ public class DataManager implements IdentityManager { } public Group getGroupById(long id) { - /*boolean forceUpdate; + boolean forceUpdate; groupsLock.readLock().lock(); try { forceUpdate = !groupsById.containsKey(id); @@ -253,7 +253,7 @@ public class DataManager implements IdentityManager { updateGroupCache(forceUpdate); } catch (SQLException e) { Log.warning(e); - }*/ + } groupsLock.readLock().lock(); try { diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index e1eb042c8..32c9e2368 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -33,7 +33,6 @@ import org.traccar.model.Device; import org.traccar.model.DeviceGeofence; import org.traccar.model.Geofence; import org.traccar.model.GeofencePermission; -import org.traccar.model.Group; import org.traccar.model.GroupGeofence; import org.traccar.model.Position; @@ -51,19 +50,29 @@ public class GeofenceManager { private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); private final ReadWriteLock groupGeofencesLock = new ReentrantReadWriteLock(); + private final ReadWriteLock userGeofencesLock = new ReentrantReadWriteLock(); public GeofenceManager(DataManager dataManager) { this.dataManager = dataManager; - refresh(); + refreshGeofences(); } - public Set<Long> getUserGeofencesIds(long userId) { + private Set<Long> getUserGeofences(long userId) { if (!userGeofences.containsKey(userId)) { userGeofences.put(userId, new HashSet<Long>()); } return userGeofences.get(userId); } + public Set<Long> getUserGeofencesIds(long userId) { + userGeofencesLock.readLock().lock(); + try { + return getUserGeofences(userId); + } finally { + userGeofencesLock.readLock().unlock(); + } + } + private Set<Long> getGroupGeofences(long groupId) { if (!groupGeofences.containsKey(groupId)) { groupGeofences.put(groupId, new HashSet<Long>()); @@ -105,23 +114,50 @@ public class GeofenceManager { return deviceGeofences.get(deviceId); } - public final void refresh() { + public final void refreshGeofences() { if (dataManager != null) { try { geofencesLock.writeLock().lock(); - groupGeofencesLock.writeLock().lock(); - deviceGeofencesLock.writeLock().lock(); try { geofences.clear(); for (Geofence geofence : dataManager.getGeofences()) { geofences.put(geofence.getId(), geofence); } + } finally { + geofencesLock.writeLock().unlock(); + } + } catch (SQLException error) { + Log.warning(error); + } + } + refreshUserGeofences(); + refresh(); + } + public final void refreshUserGeofences() { + if (dataManager != null) { + try { + userGeofencesLock.writeLock().lock(); + try { userGeofences.clear(); for (GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) { - getUserGeofencesIds(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId()); + getUserGeofences(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId()); } + } finally { + userGeofencesLock.writeLock().unlock(); + } + } catch (SQLException error) { + Log.warning(error); + } + } + } + public final void refresh() { + if (dataManager != null) { + try { + groupGeofencesLock.writeLock().lock(); + deviceGeofencesLock.writeLock().lock(); + try { groupGeofences.clear(); for (GroupGeofence groupGeofence : dataManager.getGroupGeofences()) { getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId()); @@ -138,11 +174,15 @@ public class GeofenceManager { } for (Device device : dataManager.getAllDevicesCached()) { - Group group = dataManager.getGroupById(device.getGroupId()); - while (group != null) { + long groupId = device.getGroupId(); + while (groupId != 0) { getDeviceGeofences(deviceGeofencesWithGroups, - device.getId()).addAll(getGroupGeofences(group.getGroupId())); - group = dataManager.getGroupById(group.getGroupId()); + device.getId()).addAll(getGroupGeofences(groupId)); + if (dataManager.getGroupById(groupId) != null) { + groupId = dataManager.getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } } List<Long> deviceGeofenceIds = device.getGeofenceIds(); if (deviceGeofenceIds == null) { @@ -164,7 +204,6 @@ public class GeofenceManager { } } finally { - geofencesLock.writeLock().unlock(); groupGeofencesLock.writeLock().unlock(); deviceGeofencesLock.writeLock().unlock(); } |