aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-07-24 16:25:34 +0500
committerAbyss777 <abyss@fox5.ru>2017-07-24 16:25:34 +0500
commitd6c7cdf1e103c977ed94b737dc838d7ac22b9fa7 (patch)
tree12c3b57771236977cedde9651f7c5a44e67ec23f /src/org/traccar
parent4b2372336d0496c85befe099914434e5b68f05b3 (diff)
downloadtrackermap-server-d6c7cdf1e103c977ed94b737dc838d7ac22b9fa7.tar.gz
trackermap-server-d6c7cdf1e103c977ed94b737dc838d7ac22b9fa7.tar.bz2
trackermap-server-d6c7cdf1e103c977ed94b737dc838d7ac22b9fa7.zip
Optimize caching and class structure
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/database/AttributesManager.java7
-rw-r--r--src/org/traccar/database/CalendarManager.java1
-rw-r--r--src/org/traccar/database/DriversManager.java27
-rw-r--r--src/org/traccar/database/ExtendedObjectManager.java1
-rw-r--r--src/org/traccar/database/GeofenceManager.java2
-rw-r--r--src/org/traccar/database/SimpleObjectManager.java39
6 files changed, 31 insertions, 46 deletions
diff --git a/src/org/traccar/database/AttributesManager.java b/src/org/traccar/database/AttributesManager.java
index 266fc5526..4dc70eeb7 100644
--- a/src/org/traccar/database/AttributesManager.java
+++ b/src/org/traccar/database/AttributesManager.java
@@ -16,8 +16,6 @@
*/
package org.traccar.database;
-import java.sql.SQLException;
-
import org.traccar.model.Attribute;
import org.traccar.model.BaseModel;
@@ -25,14 +23,11 @@ public class AttributesManager extends ExtendedObjectManager {
public AttributesManager(DataManager dataManager) {
super(dataManager, Attribute.class);
- refreshItems();
- refreshExtendedPermissions();
}
@Override
- public void updateItem(BaseModel item) throws SQLException {
+ public void updateCachedItem(BaseModel item) {
Attribute attribute = (Attribute) item;
- getDataManager().updateObject(attribute);
Attribute cachedAttribute = (Attribute) getById(item.getId());
cachedAttribute.setDescription(attribute.getDescription());
cachedAttribute.setAttribute(attribute.getAttribute());
diff --git a/src/org/traccar/database/CalendarManager.java b/src/org/traccar/database/CalendarManager.java
index 80bb79db2..6874020ec 100644
--- a/src/org/traccar/database/CalendarManager.java
+++ b/src/org/traccar/database/CalendarManager.java
@@ -22,7 +22,6 @@ public class CalendarManager extends SimpleObjectManager {
public CalendarManager(DataManager dataManager) {
super(dataManager, Calendar.class);
- refreshItems();
}
}
diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java
index 0dc2b102d..e3ae07a65 100644
--- a/src/org/traccar/database/DriversManager.java
+++ b/src/org/traccar/database/DriversManager.java
@@ -20,7 +20,6 @@ import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import org.traccar.helper.Log;
import org.traccar.model.Driver;
import org.traccar.model.BaseModel;
@@ -30,24 +29,6 @@ public class DriversManager extends ExtendedObjectManager {
public DriversManager(DataManager dataManager) {
super(dataManager, Driver.class);
- refreshItems();
- refreshExtendedPermissions();
- }
-
- @Override
- public void refreshItems() {
- if (getDataManager() != null) {
- try {
- clearItems();
- for (BaseModel item : getDataManager().getObjects(getBaseClass())) {
- putItem(item.getId(), item);
- driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item);
- }
- } catch (SQLException error) {
- Log.warning(error);
- }
- }
- refreshUserItems();
}
@Override
@@ -57,9 +38,8 @@ public class DriversManager extends ExtendedObjectManager {
}
@Override
- public void updateItem(BaseModel item) throws SQLException {
+ protected void updateCachedItem(BaseModel item) {
Driver driver = (Driver) item;
- getDataManager().updateObject(driver);
Driver cachedDriver = (Driver) getById(driver.getId());
cachedDriver.setName(driver.getName());
if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) {
@@ -71,16 +51,13 @@ public class DriversManager extends ExtendedObjectManager {
}
@Override
- public void removeItem(long driverId) throws SQLException {
+ protected void removeCachedItem(long driverId) {
Driver cachedDriver = (Driver) getById(driverId);
- getDataManager().removeObject(Driver.class, driverId);
if (cachedDriver != null) {
String driverUniqueId = cachedDriver.getUniqueId();
removeCachedItem(driverId);
driversByUniqueId.remove(driverUniqueId);
}
- refreshUserItems();
- refreshExtendedPermissions();
}
public Driver getDriverByUniqueId(String uniqueId) {
diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java
index 483c3a09e..a9dc7d28a 100644
--- a/src/org/traccar/database/ExtendedObjectManager.java
+++ b/src/org/traccar/database/ExtendedObjectManager.java
@@ -37,6 +37,7 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager {
protected ExtendedObjectManager(DataManager dataManager, Class<? extends BaseModel> baseClass) {
super(dataManager, baseClass);
+ refreshExtendedPermissions();
}
public final Set<Long> getGroupItems(long groupId) {
diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java
index bc2c27a65..2c3c32c56 100644
--- a/src/org/traccar/database/GeofenceManager.java
+++ b/src/org/traccar/database/GeofenceManager.java
@@ -27,8 +27,6 @@ public class GeofenceManager extends ExtendedObjectManager {
public GeofenceManager(DataManager dataManager) {
super(dataManager, Geofence.class);
- refreshItems();
- refreshExtendedPermissions();
}
@Override
diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java
index 0db8af658..49ad4707c 100644
--- a/src/org/traccar/database/SimpleObjectManager.java
+++ b/src/org/traccar/database/SimpleObjectManager.java
@@ -33,7 +33,7 @@ public abstract class SimpleObjectManager {
private final DataManager dataManager;
- private final Map<Long, BaseModel> items = new ConcurrentHashMap<>();
+ private Map<Long, BaseModel> items;
private final Map<Long, Set<Long>> userItems = new ConcurrentHashMap<>();
private Class<? extends BaseModel> baseClass;
@@ -43,6 +43,7 @@ public abstract class SimpleObjectManager {
this.dataManager = dataManager;
this.baseClass = baseClass;
baseClassIdName = DataManager.makeNameId(baseClass);
+ refreshItems();
}
protected final DataManager getDataManager() {
@@ -65,11 +66,7 @@ public abstract class SimpleObjectManager {
items.clear();
}
- protected final void putItem(long itemId, BaseModel item) {
- items.put(itemId, item);
- }
-
- protected final void removeCachedItem(long itemId) {
+ protected void removeCachedItem(long itemId) {
items.remove(itemId);
}
@@ -91,9 +88,23 @@ public abstract class SimpleObjectManager {
public void refreshItems() {
if (dataManager != null) {
try {
- clearItems();
- for (BaseModel item : dataManager.getObjects(this.baseClass)) {
- putItem(item.getId(), item);
+ Collection<? extends BaseModel> databaseItems = dataManager.getObjects(baseClass);
+ if (items == null) {
+ items = new ConcurrentHashMap<>(databaseItems.size());
+ }
+ Set<Long> databaseItemIds = new HashSet<>();
+ for (BaseModel item : databaseItems) {
+ databaseItemIds.add(item.getId());
+ if (items.containsKey(item.getId())) {
+ updateCachedItem(item);
+ } else {
+ items.put(item.getId(), item);
+ }
+ }
+ for (Long cachedItemId : items.keySet()) {
+ if (!databaseItemIds.contains(cachedItemId)) {
+ items.remove(cachedItemId);
+ }
}
} catch (SQLException error) {
Log.warning(error);
@@ -118,12 +129,16 @@ public abstract class SimpleObjectManager {
public void addItem(BaseModel item) throws SQLException {
dataManager.addObject(item);
- putItem(item.getId(), item);
+ items.put(item.getId(), item);
+ }
+
+ protected void updateCachedItem(BaseModel item) {
+ items.put(item.getId(), item);
}
public void updateItem(BaseModel item) throws SQLException {
dataManager.updateObject(item);
- putItem(item.getId(), item);
+ updateCachedItem(item);
}
public void removeItem(long itemId) throws SQLException {
@@ -143,7 +158,7 @@ public abstract class SimpleObjectManager {
return result;
}
- public final Set<Long> getAllItems() {
+ public Set<Long> getAllItems() {
return items.keySet();
}