diff options
author | ninioe <ninioe@gmail.com> | 2016-07-19 09:07:23 +0300 |
---|---|---|
committer | ninioe <ninioe@gmail.com> | 2016-07-19 09:07:23 +0300 |
commit | ef40f3317392068692606b484c6782b95b554cbf (patch) | |
tree | 41c48e6c87620a6444aed0ad5706a94e26b1662d /src/org/traccar/database/DataManager.java | |
parent | 04627d7473e4ea21d6ecd950cbb3fdd6fa249429 (diff) | |
parent | d3e063cf9de8a45bed4dfe87b73aadfd8d2edeab (diff) | |
download | trackermap-server-ef40f3317392068692606b484c6782b95b554cbf.tar.gz trackermap-server-ef40f3317392068692606b484c6782b95b554cbf.tar.bz2 trackermap-server-ef40f3317392068692606b484c6782b95b554cbf.zip |
Merge remote-tracking branch 'refs/remotes/tananaev/master'
Diffstat (limited to 'src/org/traccar/database/DataManager.java')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 133 |
1 files changed, 3 insertions, 130 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index bdd2e9adf..e5a245568 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -62,9 +62,9 @@ import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; -public class DataManager implements IdentityManager { +public class DataManager { - private static final long DEFAULT_REFRESH_DELAY = 300; + public static final long DEFAULT_REFRESH_DELAY = 300; private final Config config; @@ -72,11 +72,6 @@ public class DataManager implements IdentityManager { private final long dataRefreshDelay; - private final ReadWriteLock devicesLock = new ReentrantReadWriteLock(); - private final Map<Long, Device> devicesById = new HashMap<>(); - private final Map<String, Device> devicesByUniqueId = new HashMap<>(); - private long devicesLastUpdate; - private final ReadWriteLock groupsLock = new ReentrantReadWriteLock(); private final Map<Long, Group> groupsById = new HashMap<>(); private long groupsLastUpdate; @@ -136,85 +131,6 @@ public class DataManager implements IdentityManager { } } - private void updateDeviceCache(boolean force) throws SQLException { - boolean needWrite; - devicesLock.readLock().lock(); - try { - needWrite = force || System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay; - } finally { - devicesLock.readLock().unlock(); - } - - if (needWrite) { - devicesLock.writeLock().lock(); - try { - if (force || System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay) { - devicesById.clear(); - devicesByUniqueId.clear(); - ConnectionManager connectionManager = Context.getConnectionManager(); - GeofenceManager geofenceManager = Context.getGeofenceManager(); - for (Device device : getAllDevices()) { - devicesById.put(device.getId(), device); - devicesByUniqueId.put(device.getUniqueId(), device); - if (connectionManager != null && geofenceManager != null) { - Position lastPosition = connectionManager.getLastPosition(device.getId()); - if (lastPosition != null) { - device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition)); - } - } - } - devicesLastUpdate = System.currentTimeMillis(); - } - } finally { - devicesLock.writeLock().unlock(); - } - } - } - - @Override - public Device getDeviceById(long id) { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = !devicesById.containsKey(id); - } finally { - devicesLock.readLock().unlock(); - } - - try { - updateDeviceCache(forceUpdate); - } catch (SQLException e) { - Log.warning(e); - } - - devicesLock.readLock().lock(); - try { - return devicesById.get(id); - } finally { - devicesLock.readLock().unlock(); - } - } - - @Override - public Device getDeviceByUniqueId(String uniqueId) throws SQLException { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = !devicesByUniqueId.containsKey(uniqueId) && !config.getBoolean("database.ignoreUnknown"); - } finally { - devicesLock.readLock().unlock(); - } - - updateDeviceCache(forceUpdate); - - devicesLock.readLock().lock(); - try { - return devicesByUniqueId.get(uniqueId); - } finally { - devicesLock.readLock().unlock(); - } - } - private void updateGroupCache(boolean force) throws SQLException { boolean needWrite; groupsLock.readLock().lock(); @@ -347,72 +263,33 @@ public class DataManager implements IdentityManager { .executeQuery(GroupPermission.class); } - private Collection<Device> getAllDevices() throws SQLException { + public Collection<Device> getAllDevices() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) .executeQuery(Device.class); } - public Collection<Device> getAllDevicesCached() { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = devicesById.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<Device> getDevices(long userId) throws SQLException { - Collection<Device> devices = new ArrayList<>(); - for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) { - devices.add(getDeviceById(id)); - } - return devices; - } - public void addDevice(Device device) throws SQLException { device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true) .setObject(device) .executeUpdate()); - updateDeviceCache(true); } public void updateDevice(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateDevice")) .setObject(device) .executeUpdate(); - updateDeviceCache(true); } public void updateDeviceStatus(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); - if (devicesById.containsKey(device.getId())) { - Device cachedDevice = devicesById.get(device.getId()); - cachedDevice.setStatus(device.getStatus()); - cachedDevice.setMotion(device.getMotion()); - } } public void removeDevice(long deviceId) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) .setLong("id", deviceId) .executeUpdate(); - updateDeviceCache(true); } public void linkDevice(long userId, long deviceId) throws SQLException { @@ -514,10 +391,6 @@ public class DataManager implements IdentityManager { .setDate("now", new Date()) .setObject(position) .executeUpdate(); - if (devicesById.containsKey(position.getDeviceId())) { - Device cachedDevice = devicesById.get(position.getDeviceId()); - cachedDevice.setPositionId(position.getId()); - } } public Collection<Position> getLatestPositions() throws SQLException { |