aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-07-25 15:31:03 +1200
committerGitHub <noreply@github.com>2017-07-25 15:31:03 +1200
commit6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4 (patch)
tree8cba8784f507140e871e290229ab9841034a0c07
parentc8e5c5e3da63646fd7c84d4f522bc8d7e5109982 (diff)
parentf35961c2f4a3104a009d6e3cd6bc862e6810b998 (diff)
downloadtrackermap-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.tar.gz
trackermap-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.tar.bz2
trackermap-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.zip
Merge pull request #3385 from Abyss777/optimize_managers
Few managers optimizations
-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/DataManager.java8
-rw-r--r--src/org/traccar/database/DeviceManager.java2
-rw-r--r--src/org/traccar/database/DriversManager.java41
-rw-r--r--src/org/traccar/database/ExtendedObjectManager.java21
-rw-r--r--src/org/traccar/database/GeofenceManager.java2
-rw-r--r--src/org/traccar/database/PermissionsManager.java28
-rw-r--r--src/org/traccar/database/QueryBuilder.java11
-rw-r--r--src/org/traccar/database/SimpleObjectManager.java55
10 files changed, 83 insertions, 93 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/DataManager.java b/src/org/traccar/database/DataManager.java
index 80b9f98e9..862efbc91 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -23,7 +23,6 @@ import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
-import java.util.Map;
import javax.naming.InitialContext;
import javax.sql.DataSource;
@@ -41,6 +40,7 @@ import org.traccar.helper.Log;
import org.traccar.model.AttributeAlias;
import org.traccar.model.Device;
import org.traccar.model.Event;
+import org.traccar.model.Permission;
import org.traccar.model.BaseModel;
import org.traccar.model.Position;
import org.traccar.model.Server;
@@ -293,10 +293,10 @@ public class DataManager {
return QueryBuilder.create(dataSource, getQuery(query)).executeQuery(clazz);
}
- public Collection<Map<String, Long>> getPermissions(Class<? extends BaseModel> owner,
- Class<? extends BaseModel> property) throws SQLException {
+ public Collection<Permission> getPermissions(Class<? extends BaseModel> owner,
+ Class<? extends BaseModel> property) throws SQLException, ClassNotFoundException {
String query = "database.select" + owner.getSimpleName() + property.getSimpleName() + "s";
- return QueryBuilder.create(dataSource, getQuery(query)).executeMapQuery(Long.class);
+ return QueryBuilder.create(dataSource, getQuery(query)).executePermissionsQuery();
}
public void addObject(BaseModel entity) throws SQLException {
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java
index ba360f165..6637df91c 100644
--- a/src/org/traccar/database/DeviceManager.java
+++ b/src/org/traccar/database/DeviceManager.java
@@ -306,7 +306,7 @@ public class DeviceManager implements IdentityManager {
}
for (Long cachedGroupId : groupsById.keySet()) {
if (!databaseGroupsIds.contains(cachedGroupId)) {
- devicesById.remove(cachedGroupId);
+ groupsById.remove(cachedGroupId);
}
}
databaseGroupsIds.clear();
diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java
index 0dc2b102d..14b74bab1 100644
--- a/src/org/traccar/database/DriversManager.java
+++ b/src/org/traccar/database/DriversManager.java
@@ -16,71 +16,54 @@
*/
package org.traccar.database;
-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;
public class DriversManager extends ExtendedObjectManager {
- private final Map<String, Driver> driversByUniqueId = new ConcurrentHashMap<>();
+ private Map<String, Driver> driversByUniqueId;
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);
- }
+ private void putUniqueDriverId(Driver driver) {
+ if (driversByUniqueId == null) {
+ driversByUniqueId = new ConcurrentHashMap<>();
}
- refreshUserItems();
+ driversByUniqueId.put(driver.getUniqueId(), driver);
}
@Override
- public void addItem(BaseModel item) throws SQLException {
- super.addItem(item);
- driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item);
+ protected void addNewItem(BaseModel item) {
+ super.addNewItem(item);
+ putUniqueDriverId((Driver) item);
}
@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())) {
driversByUniqueId.remove(cachedDriver.getUniqueId());
cachedDriver.setUniqueId(driver.getUniqueId());
- driversByUniqueId.put(cachedDriver.getUniqueId(), cachedDriver);
+ putUniqueDriverId(cachedDriver);
}
cachedDriver.setAttributes(driver.getAttributes());
}
@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);
+ super.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..2833b3bae 100644
--- a/src/org/traccar/database/ExtendedObjectManager.java
+++ b/src/org/traccar/database/ExtendedObjectManager.java
@@ -27,6 +27,7 @@ import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Group;
+import org.traccar.model.Permission;
import org.traccar.model.BaseModel;
public abstract class ExtendedObjectManager extends SimpleObjectManager {
@@ -37,6 +38,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) {
@@ -78,27 +80,24 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager {
if (getDataManager() != null) {
try {
- Collection<Map<String, Long>> databaseGroupPermissions =
+ Collection<Permission> databaseGroupPermissions =
getDataManager().getPermissions(Group.class, getBaseClass());
clearGroupItems();
- for (Map<String, Long> groupPermission : databaseGroupPermissions) {
- getGroupItems(groupPermission.get(DataManager.makeNameId(Group.class)))
- .add(groupPermission.get(getBaseClassIdName()));
+ for (Permission groupPermission : databaseGroupPermissions) {
+ getGroupItems(groupPermission.getOwnerId()).add(groupPermission.getPropertyId());
}
- Collection<Map<String, Long>> databaseDevicePermissions =
+ Collection<Permission> databaseDevicePermissions =
getDataManager().getPermissions(Device.class, getBaseClass());
Collection<Device> allDevices = Context.getDeviceManager().getAllDevices();
clearDeviceItems();
deviceItemsWithGroups.clear();
- for (Map<String, Long> devicePermission : databaseDevicePermissions) {
- getDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class)))
- .add(devicePermission.get(getBaseClassIdName()));
- getAllDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class)))
- .add(devicePermission.get(getBaseClassIdName()));
+ for (Permission devicePermission : databaseDevicePermissions) {
+ getDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
+ getAllDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
}
for (Device device : allDevices) {
@@ -113,7 +112,7 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager {
}
}
- } catch (SQLException error) {
+ } catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
}
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/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index b9cdfc36b..70969d749 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -115,11 +115,10 @@ public class PermissionsManager {
public final void refreshUserPermissions() {
userPermissions.clear();
try {
- for (Map<String, Long> permission : dataManager.getPermissions(User.class, User.class)) {
- getUserPermissions(permission.get(DataManager.makeNameId(User.class)))
- .add(permission.get(DataManager.makeNameId(ManagedUser.class)));
+ for (Permission permission : dataManager.getPermissions(User.class, User.class)) {
+ getUserPermissions(permission.getOwnerId()).add(permission.getPropertyId());
}
- } catch (SQLException error) {
+ } catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
}
@@ -130,23 +129,20 @@ public class PermissionsManager {
try {
GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(),
Context.getDeviceManager().getAllDevices());
- for (Map<String, Long> groupPermission : dataManager.getPermissions(User.class, Group.class)) {
- Set<Long> userGroupPermissions = getGroupPermissions(groupPermission
- .get(DataManager.makeNameId(User.class)));
- Set<Long> userDevicePermissions = getDevicePermissions(groupPermission
- .get(DataManager.makeNameId(User.class)));
- userGroupPermissions.add(groupPermission.get(DataManager.makeNameId(Group.class)));
- for (Group group : groupTree.getGroups(groupPermission.get(DataManager.makeNameId(Group.class)))) {
+ for (Permission groupPermission : dataManager.getPermissions(User.class, Group.class)) {
+ Set<Long> userGroupPermissions = getGroupPermissions(groupPermission.getOwnerId());
+ Set<Long> userDevicePermissions = getDevicePermissions(groupPermission.getOwnerId());
+ userGroupPermissions.add(groupPermission.getPropertyId());
+ for (Group group : groupTree.getGroups(groupPermission.getPropertyId())) {
userGroupPermissions.add(group.getId());
}
- for (Device device : groupTree.getDevices(groupPermission.get(DataManager.makeNameId(Group.class)))) {
+ for (Device device : groupTree.getDevices(groupPermission.getPropertyId())) {
userDevicePermissions.add(device.getId());
}
}
- for (Map<String, Long> devicePermission : dataManager.getPermissions(User.class, Device.class)) {
- getDevicePermissions(devicePermission.get(DataManager.makeNameId(User.class)))
- .add(devicePermission.get(DataManager.makeNameId(Device.class)));
+ for (Permission devicePermission : dataManager.getPermissions(User.class, Device.class)) {
+ getDevicePermissions(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
}
groupDevices.clear();
@@ -156,7 +152,7 @@ public class PermissionsManager {
}
}
- } catch (SQLException error) {
+ } catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java
index 440690a76..af33458a8 100644
--- a/src/org/traccar/database/QueryBuilder.java
+++ b/src/org/traccar/database/QueryBuilder.java
@@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.MiscFormatter;
+import org.traccar.model.Permission;
import javax.sql.DataSource;
import java.io.IOException;
@@ -488,19 +489,19 @@ public final class QueryBuilder {
return 0;
}
- public <T> Collection<Map<String, T>> executeMapQuery(Class<T> clazz) throws SQLException {
- List<Map<String, T>> result = new LinkedList<>();
+ public Collection<Permission> executePermissionsQuery() throws SQLException, ClassNotFoundException {
+ List<Permission> result = new LinkedList<>();
if (query != null) {
try {
try (ResultSet resultSet = statement.executeQuery()) {
ResultSetMetaData resultMetaData = resultSet.getMetaData();
while (resultSet.next()) {
- LinkedHashMap<String, T> map = new LinkedHashMap<>();
+ LinkedHashMap<String, Long> map = new LinkedHashMap<>();
for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
String label = resultMetaData.getColumnLabel(i);
- map.put(label, resultSet.getObject(label, clazz));
+ map.put(label, resultSet.getLong(label));
}
- result.add(map);
+ result.add(new Permission(map));
}
}
} finally {
diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java
index 0db8af658..282dea46e 100644
--- a/src/org/traccar/database/SimpleObjectManager.java
+++ b/src/org/traccar/database/SimpleObjectManager.java
@@ -27,13 +27,14 @@ import java.util.concurrent.ConcurrentHashMap;
import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.BaseModel;
+import org.traccar.model.Permission;
import org.traccar.model.User;
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 +44,7 @@ public abstract class SimpleObjectManager {
this.dataManager = dataManager;
this.baseClass = baseClass;
baseClassIdName = DataManager.makeNameId(baseClass);
+ refreshItems();
}
protected final DataManager getDataManager() {
@@ -65,14 +67,6 @@ public abstract class SimpleObjectManager {
items.clear();
}
- protected final void putItem(long itemId, BaseModel item) {
- items.put(itemId, item);
- }
-
- protected final void removeCachedItem(long itemId) {
- items.remove(itemId);
- }
-
public final Set<Long> getUserItems(long userId) {
if (!userItems.containsKey(userId)) {
userItems.put(userId, new HashSet<Long>());
@@ -91,9 +85,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 {
+ addNewItem(item);
+ }
+ }
+ for (Long cachedItemId : items.keySet()) {
+ if (!databaseItemIds.contains(cachedItemId)) {
+ removeCachedItem(cachedItemId);
+ }
}
} catch (SQLException error) {
Log.warning(error);
@@ -106,24 +114,35 @@ public abstract class SimpleObjectManager {
if (dataManager != null) {
try {
clearUserItems();
- for (Map<String, Long> permission : dataManager.getPermissions(User.class, baseClass)) {
- getUserItems(permission.get(DataManager.makeNameId(User.class)))
- .add(permission.get(baseClassIdName));
+ for (Permission permission : dataManager.getPermissions(User.class, baseClass)) {
+ getUserItems(permission.getOwnerId()).add(permission.getPropertyId());
}
- } catch (SQLException error) {
+ } catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
}
}
+ protected void addNewItem(BaseModel item) {
+ items.put(item.getId(), item);
+ }
+
public void addItem(BaseModel item) throws SQLException {
dataManager.addObject(item);
- putItem(item.getId(), item);
+ addNewItem(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);
+ }
+
+ protected void removeCachedItem(long itemId) {
+ items.remove(itemId);
}
public void removeItem(long itemId) throws SQLException {