aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-07-11 23:23:21 +1200
committerGitHub <noreply@github.com>2016-07-11 23:23:21 +1200
commita4a127b5255cd62aae25419dbf289a926c5adbfd (patch)
tree05b99cf458f597806cde9617fe28c5bccd557c22 /src/org/traccar/database
parent817360eee7dba57cf8b58e858f7acf60f27b5e68 (diff)
parent0bda01815c51b1f1e1d3e3eda592219eb39bf78b (diff)
downloadtraccar-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.tar.gz
traccar-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.tar.bz2
traccar-server-a4a127b5255cd62aae25419dbf289a926c5adbfd.zip
Merge pull request #2099 from Abyss777/fix_groups_cache
Revert back groups cache and split refresh
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/DataManager.java4
-rw-r--r--src/org/traccar/database/GeofenceManager.java63
2 files changed, 53 insertions, 14 deletions
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();
}