From 451c358879e1f6f2fda26e4b85ef97982fbeaba5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jul 2016 16:49:08 +1200 Subject: Improve history clearing logic --- debug.xml | 4 ++-- src/org/traccar/Main.java | 9 ++++----- src/org/traccar/database/DataManager.java | 27 +++++---------------------- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/debug.xml b/debug.xml index 346eda292..304ae7f3d 100644 --- a/debug.xml +++ b/debug.xml @@ -323,8 +323,8 @@ DELETE FROM notifications WHERE id = :id; - - DELETE FROM positions WHERE id != :positionId and deviceid = :deviceId and servertime < :serverTime; + + DELETE FROM positions WHERE serverTime < :serverTime AND id NOT IN (SELECT positionId FROM devices); diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index e7e8d8ab9..e992691ad 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -23,8 +23,8 @@ import java.util.TimerTask; import java.util.Locale; public final class Main { - static final long CLEAN_DELAY = 0; - static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; + + private static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; private Main() { } @@ -40,8 +40,7 @@ public final class Main { Context.getWebServer().start(); } - Timer timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { + new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { @@ -50,7 +49,7 @@ public final class Main { Log.warning(error); } } - }, CLEAN_DELAY, CLEAN_PERIOD); + }, 0, CLEAN_PERIOD); Runtime.getRuntime().addShutdownHook(new Thread() { @Override diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 04d0d44ea..78f1b4109 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -305,28 +305,11 @@ public class DataManager { } public void clearPositionsHistory() throws SQLException { - int histDays = config.getInteger("database.positionsHistoryDays"); - if (histDays == 0) { - return; - } - - String sql = getQuery("database.clearPositionsHistory"); - if (sql == null) { - return; - } - - for (Device device : getAllDevices()) { - Date lastUpdate = device.getLastUpdate(); - if (lastUpdate != null) { - - Date dateBefore = new Date(lastUpdate.getTime() - histDays * 24 * 3600 * 1000); - - QueryBuilder.create(dataSource, sql) - .setLong("positionId", device.getPositionId()) - .setLong("deviceId", device.getId()) - .setDate("serverTime", dateBefore) - .executeUpdate(); - } + int historyDays = config.getInteger("database.positionsHistoryDays"); + if (historyDays != 0) { + QueryBuilder.create(dataSource, getQuery("database.deletePositions")) + .setDate("serverTime", new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000)) + .executeUpdate(); } } -- cgit v1.2.3