diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-07-25 15:31:03 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-25 15:31:03 +1200 |
commit | 6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4 (patch) | |
tree | 8cba8784f507140e871e290229ab9841034a0c07 /src/org/traccar/database/DriversManager.java | |
parent | c8e5c5e3da63646fd7c84d4f522bc8d7e5109982 (diff) | |
parent | f35961c2f4a3104a009d6e3cd6bc862e6810b998 (diff) | |
download | traccar-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.tar.gz traccar-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.tar.bz2 traccar-server-6253fa291c3a2962fef2076c0cbb0f4e1ca8a3b4.zip |
Merge pull request #3385 from Abyss777/optimize_managers
Few managers optimizations
Diffstat (limited to 'src/org/traccar/database/DriversManager.java')
-rw-r--r-- | src/org/traccar/database/DriversManager.java | 41 |
1 files changed, 12 insertions, 29 deletions
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) { |