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;
}
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);
}
}
}
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();
}
}
Aggregations