<commit_before>/*
 * Copyright 2016 - 2017 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import org.traccar.BaseProtocol;
import org.traccar.Config;
import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Command;
import org.traccar.model.CommandType;
import org.traccar.model.Device;
import org.traccar.model.DeviceTotalDistance;
import org.traccar.model.Group;
import org.traccar.model.Position;
import org.traccar.model.Server;

public class DeviceManager implements IdentityManager {

    public static final long DEFAULT_REFRESH_DELAY = 300;

    private final Config config;
    private final DataManager dataManager;
    private final long dataRefreshDelay;
    private boolean lookupGroupsAttribute;

    private Map<Long, Device> devicesById;
    private Map<String, Device> devicesByUniqueId;
    private Map<String, Device> devicesByPhone;
    private AtomicLong devicesLastUpdate = new AtomicLong();

    private Map<Long, Group> groupsById;
    private AtomicLong groupsLastUpdate = new AtomicLong();

    private final Map<Long, Position> positions = new ConcurrentHashMap<>();

    private boolean fallbackToText;

    public DeviceManager(DataManager dataManager) {
        this.dataManager = dataManager;
        this.config = Context.getConfig();
        dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
        lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute");
        fallbackToText = config.getBoolean("command.fallbackToSms");
        if (dataManager != null) {
            try {
                updateGroupCache(true);
                updateDeviceCache(true);
                for (Position position : dataManager.getLatestPositions()) {
                    positions.put(position.getDeviceId(), position);
                }
            } catch (SQLException error) {
                Log.warning(error);
            }
        }
    }

    private void updateDeviceCache(boolean force) throws SQLException {

        long lastUpdate = devicesLastUpdate.get();
        if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay)
                && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) {
            GeofenceManager geofenceManager = Context.getGeofenceManager();
            Collection<Device> databaseDevices = dataManager.getAllDevices();
            if (devicesById == null) {
                devicesById = new ConcurrentHashMap<>(databaseDevices.size());
            }
            if (devicesByUniqueId == null) {
                devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size());
            }
            if (devicesByPhone == null) {
                devicesByPhone = new ConcurrentHashMap<>(databaseDevices.size());
            }
            Set<Long> databaseDevicesIds = new HashSet<>();
            Set<String> databaseDevicesUniqueIds = new HashSet<>();
            Set<String> databaseDevicesPhones = new HashSet<>();
            for (Device device : databaseDevices) {
                databaseDevicesIds.add(device.getId());
                databaseDevicesUniqueIds.add(device.getUniqueId());
                databaseDevicesPhones.add(device.getPhone());
                if (devicesById.containsKey(device.getId())) {
                    Device cachedDevice = devicesById.get(device.getId());
                    cachedDevice.setName(device.getName());
                    cachedDevice.setGroupId(device.getGroupId());
                    cachedDevice.setCategory(device.getCategory());
                    cachedDevice.setContact(device.getContact());
                    cachedDevice.setModel(device.getModel());
                    cachedDevice.setAttributes(device.getAttributes());
                    if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) {
                        devicesByUniqueId.put(device.getUniqueId(), cachedDevice);
                    }
                    cachedDevice.setUniqueId(device.getUniqueId());
                    if (device.getPhone() != null && !device.getPhone().isEmpty()
                            && !device.getPhone().equals(cachedDevice.getPhone())) {
                        devicesByPhone.put(device.getPhone(), cachedDevice);
                    }
                    cachedDevice.setPhone(device.getPhone());
                } else {
                    devicesById.put(device.getId(), device);
                    devicesByUniqueId.put(device.getUniqueId(), device);
                    if (device.getPhone() != null && !device.getPhone().isEmpty()) {
                        devicesByPhone.put(device.getPhone(), device);
                    }
                    if (geofenceManager != null) {
                        Position lastPosition = getLastPosition(device.getId());
                        if (lastPosition != null) {
                            device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition));
                        }
                    }
                    device.setStatus(Device.STATUS_OFFLINE);
                }
            }
            for (Long cachedDeviceId : devicesById.keySet()) {
                if (!databaseDevicesIds.contains(cachedDeviceId)) {
                    devicesById.remove(cachedDeviceId);
                }
            }
            for (String cachedDeviceUniqueId : devicesByUniqueId.keySet()) {
                if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqueId)) {
                    devicesByUniqueId.remove(cachedDeviceUniqueId);
                }
            }
            for (String cachedDevicePhone : devicesByPhone.keySet()) {
                if (!databaseDevicesPhones.contains(cachedDevicePhone)) {
                    devicesByPhone.remove(cachedDevicePhone);
                }
            }
        }
    }

    @Override
    public Device getDeviceById(long id) {
        return devicesById.get(id);
    }

    @Override<commit_msg>Use Iterator for maps cleanup<commit_after>/*
 * Copyright 2016 - 2017 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import org.traccar.BaseProtocol;
import org.traccar.Config;
import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Command;
import org.traccar.model.CommandType;
import org.traccar.model.Device;
import org.traccar.model.DeviceTotalDistance;
import org.traccar.model.Group;
import org.traccar.model.Position;
import org.traccar.model.Server;

public class DeviceManager implements IdentityManager {

    public static final long DEFAULT_REFRESH_DELAY = 300;

    private final Config config;
    private final DataManager dataManager;
    private final long dataRefreshDelay;
    private boolean lookupGroupsAttribute;

    private Map<Long, Device> devicesById;
    private Map<String, Device> devicesByUniqueId;
    private Map<String, Device> devicesByPhone;
    private AtomicLong devicesLastUpdate = new AtomicLong();

    private Map<Long, Group> groupsById;
    private AtomicLong groupsLastUpdate = new AtomicLong();

    private final Map<Long, Position> positions = new ConcurrentHashMap<>();

    private boolean fallbackToText;

    public DeviceManager(DataManager dataManager) {
        this.dataManager = dataManager;
        this.config = Context.getConfig();
        dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
        lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute");
        fallbackToText = config.getBoolean("command.fallbackToSms");
        if (dataManager != null) {
            try {
                updateGroupCache(true);
                updateDeviceCache(true);
                for (Position position : dataManager.getLatestPositions()) {
                    positions.put(position.getDeviceId(), position);
                }
            } catch (SQLException error) {
                Log.warning(error);
            }
        }
    }

    private void updateDeviceCache(boolean force) throws SQLException {

        long lastUpdate = devicesLastUpdate.get();
        if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay)
                && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) {
            GeofenceManager geofenceManager = Context.getGeofenceManager();
            Collection<Device> databaseDevices = dataManager.getAllDevices();
            if (devicesById == null) {
                devicesById = new ConcurrentHashMap<>(databaseDevices.size());
            }
            if (devicesByUniqueId == null) {
                devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size());
            }
            if (devicesByPhone == null) {
                devicesByPhone = new ConcurrentHashMap<>(databaseDevices.size());
            }
            Set<Long> databaseDevicesIds = new HashSet<>();
            Set<String> databaseDevicesUniqueIds = new HashSet<>();
            Set<String> databaseDevicesPhones = new HashSet<>();
            for (Device device : databaseDevices) {
                databaseDevicesIds.add(device.getId());
                databaseDevicesUniqueIds.add(device.getUniqueId());
                databaseDevicesPhones.add(device.getPhone());
                if (devicesById.containsKey(device.getId())) {
                    Device cachedDevice = devicesById.get(device.getId());
                    cachedDevice.setName(device.getName());
                    cachedDevice.setGroupId(device.getGroupId());
                    cachedDevice.setCategory(device.getCategory());
                    cachedDevice.setContact(device.getContact());
                    cachedDevice.setModel(device.getModel());
                    cachedDevice.setAttributes(device.getAttributes());
                    if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) {
                        devicesByUniqueId.put(device.getUniqueId(), cachedDevice);
                    }
                    cachedDevice.setUniqueId(device.getUniqueId());
                    if (device.getPhone() != null && !device.getPhone().isEmpty()
                            && !device.getPhone().equals(cachedDevice.getPhone())) {
                        devicesByPhone.put(device.getPhone(), cachedDevice);
                    }
                    cachedDevice.setPhone(device.getPhone());
                } else {
                    devicesById.put(device.getId(), device);
                    devicesByUniqueId.put(device.getUniqueId(), device);
                    if (device.getPhone() != null && !device.getPhone().isEmpty()) {
                        devicesByPhone.put(device.getPhone(), device);
                    }
                    if (geofenceManager != null) {
                        Position lastPosition = getLastPosition(device.getId());
                        if (lastPosition != null) {
                            device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition));
                        }
                    }
                    device.setStatus(Device.STATUS_OFFLINE);
                }
            }
            for (Iterator<Long> iterator = devicesById.keySet().iterator(); iterator.hasNext();) {
                if (!databaseDevicesIds.contains(iterator.next())) {
                    iterator.remove();
                }
            }
            for (Iterator<String> iterator = devicesByUniqueId.keySet().iterator(); iterator.hasNext();) {
                if (!databaseDevicesUniqueIds.contains(iterator.next())) {
                    iterator.remove();
                }
            }
            for (Iterator<String> iterator = devicesByPhone.keySet().iterator(); iterator.hasNext();) {
                if (!databaseDevicesPhones.contains(iterator.next())) {
                    iterator.remove();
                }
            }
        }
    }

    @Override
    public Device getDeviceById(long id) {
        return devicesById.get(id);
    }

    @Override
{'fraction_non_alphanumeric': 0.09909453096704093, 'fraction_numerical': 0.0027526258601955813, 'mean_word_length': 2.269239876864788, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5926382', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3327, 'n_words': 718}