Project
Loading...
Searching...
No Matches
MessageSet.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef FRAMEWORK_MESSAGESET_H
12#define FRAMEWORK_MESSAGESET_H
13
14#include "Framework/PartRef.h"
15#include <memory>
16#include <vector>
17#include <cassert>
18
19namespace o2
20{
21namespace framework
22{
23
33struct MessageSet {
34 struct Index {
35 Index(size_t p, size_t s) : position(p), size(s) {}
36 size_t position = 0;
37 size_t size = 0;
38 };
39 // linear storage of messages
40 std::vector<fair::mq::MessagePtr> messages;
41 // message map describes O2 messages consisting of a header message and
42 // payload message(s), index describes position in the linear storage
43 std::vector<Index> messageMap;
44 // pair map describes all messages in one sequence of header-payload pairs and
45 // where in the message index the associated header and payload can be found
46 struct PairMapping {
47 PairMapping(size_t partId, size_t payloadId) : partIndex(partId), payloadIndex(payloadId) {}
48 // O2 message where the pair is located in
49 size_t partIndex = 0;
50 // payload index within the O2 message
51 size_t payloadIndex = 0;
52 };
53 std::vector<PairMapping> pairMap;
54
57 {
58 }
59
60 template <typename F>
61 MessageSet(F getter, size_t size)
63 {
64 add(std::forward<F>(getter), size);
65 }
66
69 {
70 other.clear();
71 }
72
74 {
75 if (&other == this) {
76 return *this;
77 }
78 messages = std::move(other.messages);
79 messageMap = std::move(other.messageMap);
80 pairMap = std::move(other.pairMap);
81 other.clear();
82 return *this;
83 }
84
86 size_t size() const
87 {
88 return messageMap.size();
89 }
90
92 size_t getNumberOfPairs() const
93 {
94 return pairMap.size();
95 }
96
98 size_t getNumberOfPayloads(size_t mi) const
99 {
100 return messageMap[mi].size;
101 }
102
104 void clear()
105 {
106 messages.clear();
107 messageMap.clear();
108 pairMap.clear();
109 }
110
111 // this is more or less legacy
112 // PartRef has been earlier used to store fixed header-payload pairs
113 // reset the set and store content of the part ref
115 {
116 clear();
117 add(std::move(ref));
118 }
119
120 // this is more or less legacy
121 // PartRef has been earlier used to store fixed header-payload pairs
122 // add content of the part ref
124 {
125 pairMap.emplace_back(messageMap.size(), 0);
126 messageMap.emplace_back(messages.size(), 1);
127 messages.emplace_back(std::move(ref.header));
128 messages.emplace_back(std::move(ref.payload));
129 }
130
132 template <typename F>
133 void add(F getter, size_t size)
134 {
135 auto partid = messageMap.size();
136 messageMap.emplace_back(messages.size(), size - 1);
137 for (size_t i = 0; i < size; ++i) {
138 if (i > 0) {
139 pairMap.emplace_back(partid, i - 1);
140 }
141 messages.emplace_back(std::move(getter(i)));
142 }
143 }
144
145 fair::mq::MessagePtr& header(size_t partIndex)
146 {
147 return messages[messageMap[partIndex].position];
148 }
149
150 fair::mq::MessagePtr& payload(size_t partIndex, size_t payloadIndex = 0)
151 {
152 assert(partIndex < messageMap.size());
153 assert(messageMap[partIndex].position + payloadIndex + 1 < messages.size());
154 return messages[messageMap[partIndex].position + payloadIndex + 1];
155 }
156
157 fair::mq::MessagePtr const& header(size_t partIndex) const
158 {
159 return messages[messageMap[partIndex].position];
160 }
161
162 fair::mq::MessagePtr const& payload(size_t partIndex, size_t payloadIndex = 0) const
163 {
164 assert(partIndex < messageMap.size());
165 assert(messageMap[partIndex].position + payloadIndex + 1 < messages.size());
166 return messages[messageMap[partIndex].position + payloadIndex + 1];
167 }
168
169 fair::mq::MessagePtr const& associatedHeader(size_t pos) const
170 {
171 return messages[messageMap[pairMap[pos].partIndex].position];
172 }
173
174 fair::mq::MessagePtr const& associatedPayload(size_t pos) const
175 {
176 auto partIndex = pairMap[pos].partIndex;
177 auto payloadIndex = pairMap[pos].payloadIndex;
178 return messages[messageMap[partIndex].position + payloadIndex + 1];
179 }
180};
181
182} // namespace framework
183} // namespace o2
184#endif // FRAMEWORK_MESSAGESET_H
int32_t i
uint16_t pos
Definition RawData.h:3
GLsizeiptr size
Definition glcorearb.h:659
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.
Index(size_t p, size_t s)
Definition MessageSet.h:35
PairMapping(size_t partId, size_t payloadId)
Definition MessageSet.h:47
MessageSet(F getter, size_t size)
Definition MessageSet.h:61
void add(F getter, size_t size)
add an O2 message
Definition MessageSet.h:133
std::vector< fair::mq::MessagePtr > messages
Definition MessageSet.h:40
size_t size() const
get number of in-flight O2 messages
Definition MessageSet.h:86
fair::mq::MessagePtr & payload(size_t partIndex, size_t payloadIndex=0)
Definition MessageSet.h:150
void clear()
clear the set
Definition MessageSet.h:104
fair::mq::MessagePtr const & associatedHeader(size_t pos) const
Definition MessageSet.h:169
fair::mq::MessagePtr const & header(size_t partIndex) const
Definition MessageSet.h:157
fair::mq::MessagePtr & header(size_t partIndex)
Definition MessageSet.h:145
std::vector< Index > messageMap
Definition MessageSet.h:43
size_t getNumberOfPayloads(size_t mi) const
get number of payloads for an in-flight message
Definition MessageSet.h:98
void add(PartRef &&ref)
Definition MessageSet.h:123
void reset(PartRef &&ref)
Definition MessageSet.h:114
MessageSet & operator=(MessageSet &&other)
Definition MessageSet.h:73
size_t getNumberOfPairs() const
get number of header-payload pairs
Definition MessageSet.h:92
fair::mq::MessagePtr const & associatedPayload(size_t pos) const
Definition MessageSet.h:174
std::vector< PairMapping > pairMap
Definition MessageSet.h:53
fair::mq::MessagePtr const & payload(size_t partIndex, size_t payloadIndex=0) const
Definition MessageSet.h:162
MessageSet(MessageSet &&other)
Definition MessageSet.h:67
Reference to an inflight part.
Definition PartRef.h:24
VectorOfTObjectPtrs other