SObjectizer  5.8
Loading...
Searching...
No Matches
subscr_storage_adaptive.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \since
7 * v.5.5.3
8 *
9 * \file
10 * \brief An adaptive storage for agent's subscriptions information.
11 */
12
13#include <so_5/impl/subscription_storage_iface.hpp>
14
15#include <algorithm>
16#include <vector>
17
18namespace so_5
19{
20
21namespace impl
22{
23
24/*!
25 * \since
26 * v.5.5.3
27 *
28 * \brief An adaptive storage for agent's subscriptions information.
29 */
31{
32
33/*!
34 * \since
35 * v.5.5.3
36 *
37 * \brief An adaptive storage for agent's subscriptions information.
38 *
39 * Uses two actual storages: one for small amount of subscriptions,
40 * another for the big amount.
41 *
42 * Controls the size of the current storage. If size of the small storage
43 * exceeded threshold then switches from small to the big one. If size of the
44 * big storage drops below the threshold then switches to the small storage.
45 */
47 {
48 public :
50 std::size_t threshold,
51 subscription_storage_unique_ptr_t small_storage,
52 subscription_storage_unique_ptr_t large_storage );
53
54 virtual void
56 const mbox_t & mbox_ref,
57 const std::type_index & type_index,
58 abstract_message_sink_t & message_sink,
59 const state_t & target_state,
60 const event_handler_method_t & method,
61 thread_safety_t thread_safety,
62 event_handler_kind_t handler_kind ) override;
63
64 virtual void
66 const mbox_t & mbox,
67 const std::type_index & msg_type,
68 const state_t & target_state ) noexcept override;
69
70 void
72 const mbox_t & mbox,
73 const std::type_index & msg_type ) noexcept override;
74
75 void
76 drop_all_subscriptions() noexcept override;
77
80 mbox_id_t mbox_id,
81 const std::type_index & msg_type,
82 const state_t & current_state ) const noexcept override;
83
84 void
85 debug_dump( std::ostream & to ) const override;
86
87 void
88 drop_content() noexcept override;
89
91 query_content() const override;
92
93 void
95 subscription_storage_common::subscr_info_vector_t && info ) override;
96
97 std::size_t
98 query_subscriptions_count() const override;
99
100 private :
102
105
107
108 void
110 };
111
113 std::size_t threshold,
114 subscription_storage_unique_ptr_t small_storage,
115 subscription_storage_unique_ptr_t large_storage )
119 {
120 m_current_storage = m_small_storage.get();
121 }
122
123void
125 const mbox_t & mbox,
126 const std::type_index & msg_type,
127 abstract_message_sink_t & message_sink,
128 const state_t & target_state,
129 const event_handler_method_t & method,
130 thread_safety_t thread_safety,
131 event_handler_kind_t handler_kind )
132 {
133 if( m_current_storage == m_small_storage.get() &&
134 m_threshold <= m_current_storage->query_subscriptions_count() )
135 {
136 // Storage must be switched to the large one.
137 // Exceptions are going out.
138 // It means that exception during switching
139 // to the large storage will prohibit subscription.
140 m_large_storage->setup_content(
141 m_small_storage->query_content() );
142
143 m_small_storage->drop_content();
144
145 m_current_storage = m_large_storage.get();
146 }
147
148 m_current_storage->create_event_subscription(
149 mbox,
150 msg_type,
151 message_sink,
152 target_state,
153 method,
154 thread_safety,
155 handler_kind );
156 }
157
158void
160 const mbox_t & mbox,
161 const std::type_index & msg_type,
162 const state_t & target_state ) noexcept
163 {
164 m_current_storage->drop_subscription( mbox, msg_type, target_state );
165
167 }
168
169void
171 const mbox_t & mbox,
172 const std::type_index & msg_type ) noexcept
173 {
174 m_current_storage->drop_subscription_for_all_states( mbox, msg_type );
175
177 }
178
179void
181 {
183 m_current_storage = m_small_storage.get();
184 }
185
188 mbox_id_t mbox_id,
189 const std::type_index & msg_type,
190 const state_t & current_state ) const noexcept
191 {
192 return m_current_storage->find_handler(
193 mbox_id,
194 msg_type,
195 current_state );
196 }
197
198void
199storage_t::debug_dump( std::ostream & to ) const
200 {
201 m_current_storage->debug_dump( to );
202 }
203
204void
206 {
208 m_current_storage = m_small_storage.get();
209 }
210
213 {
214 return m_current_storage->query_content();
215 }
216
217void
219 subscription_storage_common::subscr_info_vector_t && info )
220 {
221 auto s = info.size() <= m_threshold ?
222 m_small_storage.get() : m_large_storage.get();
223
224 s->setup_content( std::move( info ) );
225
226 m_current_storage = s;
227 }
228
229std::size_t
231 {
232 return m_current_storage->query_subscriptions_count();
233 }
234
235void
237 {
238 if( m_current_storage == m_large_storage.get() &&
239 m_threshold >= m_large_storage->query_subscriptions_count() )
240 {
241 // All exceptions are ignored because
242 // query_content, setup_content and drop_content
243 // must provide strong exception guarantee.
244 try
245 {
246 m_small_storage->setup_content(
247 m_large_storage->query_content() );
248
249 m_large_storage->drop_content();
250
251 m_current_storage = m_small_storage.get();
252 }
253 catch( ... )
254 {}
255 }
256 }
257
258} /* namespace adaptive_subscr_storage */
259
260} /* namespace impl */
261
265 {
266 return [threshold]() {
267 return impl::subscription_storage_unique_ptr_t(
268 new impl::adaptive_subscr_storage::storage_t(
269 threshold,
270 vector_based_subscription_storage_factory( threshold )(),
271 map_based_subscription_storage_factory()() ) );
272 };
273 }
274
280 {
281 return [=]() {
282 return impl::subscription_storage_unique_ptr_t(
283 new impl::adaptive_subscr_storage::storage_t(
284 threshold,
285 small_storage_factory(),
286 large_storage_factory() ) );
287 };
288 }
289
292 {
293 return adaptive_subscription_storage_factory( 8 );
294 }
295
296} /* namespace so_5 */
Interface for message sink.
An adaptive storage for agent's subscriptions information.
virtual void drop_subscription(const mbox_t &mbox, const std::type_index &msg_type, const state_t &target_state) noexcept override
void setup_content(subscription_storage_common::subscr_info_vector_t &&info) override
Setup content from information from another storage object.
const event_handler_data_t * find_handler(mbox_id_t mbox_id, const std::type_index &msg_type, const state_t &current_state) const noexcept override
void drop_all_subscriptions() noexcept override
Drop all subscriptions.
void drop_content() noexcept override
Drop all content.
std::size_t query_subscriptions_count() const override
Count of subscriptions in the storage.
storage_t(std::size_t threshold, subscription_storage_unique_ptr_t small_storage, subscription_storage_unique_ptr_t large_storage)
virtual void create_event_subscription(const mbox_t &mbox_ref, const std::type_index &type_index, abstract_message_sink_t &message_sink, const state_t &target_state, const event_handler_method_t &method, thread_safety_t thread_safety, event_handler_kind_t handler_kind) override
void drop_subscription_for_all_states(const mbox_t &mbox, const std::type_index &msg_type) noexcept override
subscription_storage_common::subscr_info_vector_t query_content() const override
An interface of subscription storage.
virtual void drop_content() noexcept=0
Drop all content.
virtual void drop_all_subscriptions() noexcept=0
Drop all subscriptions.
std::enable_if< details::is_agent_method_pointer< details::method_arity::nullary, Method_Pointer >::value, state_t & >::type on_enter(Method_Pointer pfn)
Set on enter handler.
Definition agent.hpp:4042
#define SO_5_FUNC
Definition declspec.hpp:48
An adaptive storage for agent's subscriptions information.
Details of SObjectizer run-time implementations.
Definition agent.cpp:905
Private part of message limit implementation.
Definition agent.cpp:33
SO_5_FUNC subscription_storage_factory_t default_subscription_storage_factory()
Factory for default subscription storage object.
thread_safety_t
Thread safety indicator.
Definition types.hpp:50
SO_5_FUNC subscription_storage_factory_t adaptive_subscription_storage_factory(std::size_t threshold, const subscription_storage_factory_t &small_storage_factory, const subscription_storage_factory_t &large_storage_factory)
Factory for adaptive subscription storage.
SO_5_FUNC subscription_storage_factory_t adaptive_subscription_storage_factory(std::size_t threshold)
Factory for adaptive subscription storage.
event_handler_kind_t
Kind of an event handler.
Definition types.hpp:154
Information about event_handler and its properties.