Search in sources :

Example 1 with VehicleLocationCacheEntry

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry in project onebusaway-application-modules by camsys.

the class VehicleLocationRecordCacheImpl method getRecordsForBlockInstance.

@Override
public List<VehicleLocationCacheElements> getRecordsForBlockInstance(BlockInstance blockInstance) {
    Set<AgencyAndId> vehicleIds = _vehicleIdsByBlockInstance.get(blockInstance);
    List<VehicleLocationCacheElements> records = new ArrayList<VehicleLocationCacheElements>();
    if (vehicleIds != null) {
        for (AgencyAndId vehicleId : vehicleIds) {
            VehicleLocationCacheEntry record = _entriesByVehicleId.get(vehicleId);
            if (record != null && record.getBlockInstance().equals(blockInstance))
                records.add(record.getElements());
        }
    }
    return records;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry) VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) ArrayList(java.util.ArrayList)

Example 2 with VehicleLocationCacheEntry

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry in project onebusaway-application-modules by camsys.

the class VehicleLocationRecordCacheImpl method clearStaleRecords.

public void clearStaleRecords(long time) {
    Iterator<Entry<AgencyAndId, VehicleLocationCacheEntry>> it = _entriesByVehicleId.entrySet().iterator();
    while (it.hasNext()) {
        Entry<AgencyAndId, VehicleLocationCacheEntry> entry = it.next();
        AgencyAndId vehicleId = entry.getKey();
        VehicleLocationCacheEntry cacheEntry = entry.getValue();
        if (cacheEntry.closeIfStale(time)) {
            if (_log.isDebugEnabled())
                _log.debug("pruning block location record cache for vehicle=" + vehicleId + " block=" + cacheEntry.getBlockInstance());
            it.remove();
            ConcurrentCollectionsLibrary.removeFromMapValueSet(_vehicleIdsByBlockInstance, cacheEntry.getBlockInstance(), vehicleId);
        }
    }
}
Also used : VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry) Entry(java.util.Map.Entry) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry)

Example 3 with VehicleLocationCacheEntry

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry in project onebusaway-application-modules by camsys.

the class VehicleLocationRecordCacheImpl method addRecord.

@Override
public VehicleLocationCacheElements addRecord(BlockInstance blockInstance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation, ScheduleDeviationSamples samples) {
    AgencyAndId vehicleId = record.getVehicleId();
    while (true) {
        VehicleLocationCacheEntry newCacheEntry = new VehicleLocationCacheEntry(blockInstance);
        VehicleLocationCacheEntry cacheEntry = _entriesByVehicleId.putIfAbsent(vehicleId, newCacheEntry);
        if (cacheEntry == null) {
            cacheEntry = newCacheEntry;
            /**
             * Since we're adding a new entry, we indicate the connection between
             * this block instance and vehicleId
             */
            ConcurrentCollectionsLibrary.addToMapValueSet(_vehicleIdsByBlockInstance, blockInstance, vehicleId);
        }
        /**
         * If the block instance of a vehicle has changed mid-stream, we close off
         * the cache entry and remove the block=>vid mapping
         */
        if (cacheEntry.isClosedBecauseBlockInstanceChanged(blockInstance)) {
            _entriesByVehicleId.remove(vehicleId);
            ConcurrentCollectionsLibrary.removeFromMapValueSet(_vehicleIdsByBlockInstance, cacheEntry.getBlockInstance(), vehicleId);
            continue;
        }
        /**
         * If the element failed to add because the entry is closed, we loop.
         * Someone closed the entry while we were in the process of requesting it
         * from the map. On the next loop, it should no longer be in the map.
         */
        if (!cacheEntry.addElement(record, scheduledBlockLocation, samples))
            continue;
        BlockInstance existingBlockInstance = cacheEntry.getBlockInstance();
        if (!blockInstance.equals(existingBlockInstance))
            ConcurrentCollectionsLibrary.removeFromMapValueSet(_vehicleIdsByBlockInstance, existingBlockInstance, vehicleId);
        return cacheEntry.getElements();
    }
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance)

Aggregations

AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 VehicleLocationCacheEntry (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry)3 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)1 VehicleLocationCacheElements (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements)1