ChimeraTK-DeviceAccess-DoocsBackend  01.09.02
EventIdMapper.cc
Go to the documentation of this file.
1 #include "EventIdMapper.h"
2 
3 #include <iostream>
4 
5 constexpr size_t EventIdMapper::maxSizeEventIdMap;
6 
7 ChimeraTK::VersionNumber EventIdMapper::getVersionForEventId(const doocs::EventId& eventId) {
8  // event id will be 0 e.g. if value still comes from the config file (even values changed through the panel will have
9  // some macro pulse number attached). See spec B.1.1.1.
10  if(eventId.to_int() == 0) {
11  return ChimeraTK::VersionNumber{nullptr};
12  }
13 
14  // acquire lock
15  std::lock_guard<decltype(_mapMutex)> lock{_mapMutex};
16 
17  // check if entry already exists
18  if(_eventIdToVersionMap.find(eventId) != _eventIdToVersionMap.end()) {
19  return _eventIdToVersionMap[eventId];
20  }
21 
22  // special case: map is empty right now, we need to make the very first entry.
23  if(_eventIdToVersionMap.size() == 0) {
24  return _eventIdToVersionMap[eventId];
25  }
26 
27  // check if eventId too old (cf. B.1.1.4.2)
28  if(eventId < _eventIdToVersionMap.begin()->first) {
29  return ChimeraTK::VersionNumber{nullptr};
30  }
31 
32  // determine how many elements to insert (according to B.1.1.3.1 we must not leave any gaps)
33  auto lastEventId = _eventIdToVersionMap.rbegin()->first;
34  size_t nElementsToInsert = std::min((size_t)(eventId - lastEventId), maxSizeEventIdMap);
35 
36  // delete old entries to keep map size below maximum (cf. B.1.1.4.1)
37  size_t nElementsToKeep = maxSizeEventIdMap - nElementsToInsert;
38  while(_eventIdToVersionMap.size() > nElementsToKeep) {
39  _eventIdToVersionMap.erase(_eventIdToVersionMap.begin());
40  }
41 
42  // insert new entries
43  for(doocs::EventId eid = eventId - (nElementsToInsert - 1); eid <= eventId; eid = eid + 1) {
44  _eventIdToVersionMap[eid] = ChimeraTK::VersionNumber();
45  }
46  assert(_eventIdToVersionMap.find(eventId) != _eventIdToVersionMap.end());
47 
48  // return the requested entry
49  return _eventIdToVersionMap[eventId];
50 }
EventIdMapper::getVersionForEventId
ChimeraTK::VersionNumber getVersionForEventId(const doocs::EventId &eventId)
Definition: EventIdMapper.cc:7
EventIdMapper.h