Skip to content

Commit dec3d65

Browse files
author
jparisu
committed
Refs #12001: apply to listeners
Signed-off-by: jparisu <[email protected]>
1 parent 4b807bf commit dec3d65

File tree

10 files changed

+360
-200
lines changed

10 files changed

+360
-200
lines changed

src/cpp/database/database.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,13 @@ std::vector<const StatisticsSample*> Database::select(
15021502
}
15031503
else
15041504
{
1505-
if (source_entity->kind != kinds.first)
1506-
{
1507-
continue;
1508-
}
1505+
// There are no possibility with the current kinds to arrive to this branch
1506+
assert(source_entity->kind == kinds.first);
1507+
// If in the future new kinds are added that could use this branch, use this code
1508+
// if (source_entity->kind != kinds.first)
1509+
// {
1510+
// continue;
1511+
// }
15091512
}
15101513

15111514
if (!target_entity)
@@ -1514,10 +1517,13 @@ std::vector<const StatisticsSample*> Database::select(
15141517
}
15151518
else
15161519
{
1517-
if (target_entity->kind != kinds.second)
1518-
{
1519-
continue;
1520-
}
1520+
// There are no possibility with the current kinds to arrive to this branch
1521+
assert(target_entity->kind == kinds.second);
1522+
// If in the future new kinds are added that could use this branch, use this code
1523+
// if (target_entity->kind != kinds.second)
1524+
// {
1525+
// continue;
1526+
// }
15211527
}
15221528
}
15231529
catch (const std::exception& e)
@@ -2017,7 +2023,7 @@ EntityKind Database::get_entity_kind(
20172023
const std::vector<std::shared_ptr<const Entity>> Database::get_entities(
20182024
EntityKind entity_kind,
20192025
const EntityId& entity_id,
2020-
EntityKind source_entity_kind /* = EntityKind::INVALID */) const
2026+
const EntityKind source_entity_kind /* = EntityKind::INVALID */) const
20212027
{
20222028
std::shared_ptr<const Entity> origin;
20232029

@@ -2052,7 +2058,7 @@ const std::vector<std::shared_ptr<const Entity>> Database::get_entities(
20522058
std::vector<EntityId> Database::get_entity_ids(
20532059
EntityKind entity_kind,
20542060
const EntityId& entity_id,
2055-
EntityKind source_entity_kind /* = EntityKind::INVALID */) const
2061+
const EntityKind source_entity_kind /* = EntityKind::INVALID */) const
20562062
{
20572063
std::vector<EntityId> entitiesIds;
20582064
for (const auto& entity : get_entities(entity_kind, entity_id, source_entity_kind))

src/cpp/database/database.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class Database
241241
const std::vector<std::shared_ptr<const Entity>> get_entities(
242242
EntityKind entity_kind,
243243
const EntityId& entity_id,
244-
EntityKind source_entity_kind = EntityKind::INVALID) const;
244+
const EntityKind source_entity_kind = EntityKind::INVALID) const;
245245

246246
/**
247247
* Get all EntityIds of a given EntityKind related to another entity.
@@ -261,7 +261,7 @@ class Database
261261
std::vector<EntityId> get_entity_ids(
262262
EntityKind entity_type,
263263
const EntityId& entity_id,
264-
EntityKind source_entity_kind = EntityKind::INVALID) const;
264+
const EntityKind source_entity_kind = EntityKind::INVALID) const;
265265

266266
/**
267267
* @brief Generate an EntityId that is unique for the database.

src/cpp/database/database_queue.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ void DatabaseDataQueue::process_sample()
699699
{
700700
// Host name reported by Fast DDS are considered unique
701701
std::shared_ptr<const Host> const_host = std::dynamic_pointer_cast<const Host>(database_->get_entity(
702-
hosts.front().second));
702+
hosts.front().second, EntityKind::HOST));
703703
host = std::const_pointer_cast<Host>(const_host);
704704
}
705705

@@ -709,7 +709,7 @@ void DatabaseDataQueue::process_sample()
709709
for (const auto& it : users)
710710
{
711711
std::shared_ptr<const User> const_user =
712-
std::dynamic_pointer_cast<const User>(database_->get_entity(it.second));
712+
std::dynamic_pointer_cast<const User>(database_->get_entity(it.second, EntityKind::USER));
713713

714714
// The user name is unique within the host
715715
if (const_user->host == host)
@@ -731,7 +731,9 @@ void DatabaseDataQueue::process_sample()
731731
for (const auto& it : processes)
732732
{
733733
std::shared_ptr<const Process> const_process =
734-
std::dynamic_pointer_cast<const Process>(database_->get_entity(it.second));
734+
std::dynamic_pointer_cast<const Process>(database_->get_entity(
735+
it.second,
736+
EntityKind::PROCESS));
735737

736738
// There is only one process with the same name for a given user
737739
if (const_process->user == user)

src/cpp/subscriber/StatisticsParticipantListener.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void StatisticsParticipantListener::process_endpoint_discovery(
6666
// This may throw if the domain does not exist
6767
// The database MUST contain the domain, or something went wrong upstream
6868
std::shared_ptr<database::Domain> domain = std::const_pointer_cast<database::Domain>(
69-
std::static_pointer_cast<const database::Domain>(database_->get_entity(domain_id_)));
69+
std::static_pointer_cast<const database::Domain>(database_->get_entity(domain_id_, EntityKind::DOMAIN)));
7070

7171
// Get the participant from the database
7272
GUID_t endpoint_guid = info.info.guid();
@@ -86,7 +86,7 @@ void StatisticsParticipantListener::process_endpoint_discovery(
8686
std::shared_ptr<database::DomainParticipant> participant =
8787
std::const_pointer_cast<database::DomainParticipant>(
8888
std::static_pointer_cast<const database::DomainParticipant>(database_->get_entity(
89-
participant_id.second)));
89+
participant_id.second, EntityKind::PARTICIPANT)));
9090

9191
assert(participant_id.first == domain_id_);
9292

@@ -100,7 +100,9 @@ void StatisticsParticipantListener::process_endpoint_discovery(
100100
if (topic_id.first == domain_id_)
101101
{
102102
topic = std::const_pointer_cast<database::Topic>(
103-
std::static_pointer_cast<const database::Topic>(database_->get_entity(topic_id.second)));
103+
std::static_pointer_cast<const database::Topic>(database_->get_entity(
104+
topic_id.second,
105+
EntityKind::TOPIC)));
104106

105107
if (topic->data_type == info.info.typeName().to_string())
106108
{
@@ -139,7 +141,10 @@ void StatisticsParticipantListener::process_endpoint_discovery(
139141
// This will be used only if there is no physical info.
140142
// Even if the host info is not available, the participant can only have one locator
141143
// with a given address/port combination, so we don't want to add duplicates
142-
auto participant_locators = database_->get_entities(EntityKind::LOCATOR, participant_id.second);
144+
auto participant_locators = database_->get_entities(
145+
EntityKind::LOCATOR,
146+
participant_id.second,
147+
EntityKind::PARTICIPANT);
143148

144149
// Routine to process one locator from the locator list of the endpoint
145150
auto process_locators = [&](const Locator_t& dds_locator)
@@ -328,7 +333,9 @@ void StatisticsParticipantListener::on_participant_discovery(
328333
database::EntityDiscoveryInfo entity_discovery_info;
329334
entity_discovery_info.domain_id = domain_id_;
330335
entity_discovery_info.entity = std::const_pointer_cast<database::DomainParticipant>(
331-
std::static_pointer_cast<const database::DomainParticipant>(database_->get_entity(participant_id)));
336+
std::static_pointer_cast<const database::DomainParticipant>(database_->get_entity(
337+
participant_id,
338+
EntityKind::PARTICIPANT)));
332339

333340
switch (info.status)
334341
{
@@ -361,7 +368,9 @@ void StatisticsParticipantListener::on_participant_discovery(
361368
// This may throw if the domain does not exist
362369
// The database MUST contain the domain, or something went wrong upstream
363370
std::shared_ptr<database::Domain> domain = std::const_pointer_cast<database::Domain>(
364-
std::static_pointer_cast<const database::Domain>(database_->get_entity(domain_id_)));
371+
std::static_pointer_cast<const database::Domain>(database_->get_entity(
372+
domain_id_,
373+
EntityKind::DOMAIN)));
365374

366375
std::string name = info.info.m_participantName.to_string();
367376

@@ -427,7 +436,9 @@ void StatisticsParticipantListener::on_subscriber_discovery(
427436
database::EntityDiscoveryInfo entity_discovery_info;
428437
entity_discovery_info.domain_id = domain_id_;
429438
entity_discovery_info.entity = std::const_pointer_cast<database::DataReader>(
430-
std::static_pointer_cast<const database::DataReader>(database_->get_entity(datareader_id)));
439+
std::static_pointer_cast<const database::DataReader>(database_->get_entity(
440+
datareader_id,
441+
EntityKind::DATAREADER)));
431442

432443
switch (info.status)
433444
{
@@ -497,7 +508,9 @@ void StatisticsParticipantListener::on_publisher_discovery(
497508
database::EntityDiscoveryInfo entity_discovery_info;
498509
entity_discovery_info.domain_id = domain_id_;
499510
entity_discovery_info.entity = std::const_pointer_cast<database::DataWriter>(
500-
std::static_pointer_cast<const database::DataWriter>(database_->get_entity(datawriter_id)));
511+
std::static_pointer_cast<const database::DataWriter>(database_->get_entity(
512+
datawriter_id,
513+
EntityKind::DATAWRITER)));
501514

502515
switch (info.status)
503516
{

test/mock/database/database/database/database.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,28 @@ class Database
8383
MOCK_CONST_METHOD1(get_entity, const std::shared_ptr<const Entity>(
8484
const EntityId& entity_id));
8585

86+
MOCK_CONST_METHOD2(get_entity, const std::shared_ptr<const Entity>(
87+
const EntityId& entity_id,
88+
const EntityKind entity_kind));
89+
8690
MOCK_CONST_METHOD2(get_entities, const std::vector<std::shared_ptr<const Entity>>(
8791
EntityKind entity_kind,
8892
const EntityId& entity_id));
8993

94+
MOCK_CONST_METHOD3(get_entities, const std::vector<std::shared_ptr<const Entity>>(
95+
EntityKind entity_kind,
96+
const EntityId& entity_id,
97+
const EntityKind source_entity_kind));
98+
9099
MOCK_CONST_METHOD2(get_entity_ids, std::vector<EntityId>(
91100
EntityKind entity_kind,
92101
const EntityId& entity_id));
93102

103+
MOCK_CONST_METHOD3(get_entity_ids, std::vector<EntityId>(
104+
EntityKind entity_kind,
105+
const EntityId& entity_id,
106+
const EntityKind source_entity_kind));
107+
94108
MOCK_CONST_METHOD2(get_entity_by_guid, std::pair<EntityId, EntityId>(
95109
EntityKind entity_kind,
96110
const std::string& guid));

test/unittest/Database/CMakeLists.txt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,19 @@ set(DATABASE_TEST_LIST
300300
get_entity_datareader
301301
get_entity_datawriter
302302
get_entity_locator
303+
get_entity_host_efficient_search
304+
get_entity_process_efficient_search
305+
get_entity_user_efficient_search
306+
get_entity_domain_efficient_search
307+
get_entity_topic_efficient_search
308+
get_entity_participant_efficient_search
309+
get_entity_datareader_efficient_search
310+
get_entity_datawriter_efficient_search
311+
get_entity_locator_efficient_search
312+
303313
get_entity_no_existing
314+
get_entity_no_correct_kind
315+
304316
# get_entities_by_name
305317
get_entities_by_name_host
306318
get_entities_by_name_host_wrong_name
@@ -473,7 +485,7 @@ endforeach()
473485
add_executable(database_load_tests DatabaseLoadTests.cpp ${LIBRARY_SOURCES})
474486

475487
if(MSVC)
476-
target_compile_definitions(database_load_tests PRIVATE
488+
target_compile_definitions(database_load_tests PRIVATE
477489
_CRT_DECLARE_NONSTDC_NAMES=0 FASTDDS_STATISTICS_BACKEND_SOURCE)
478490
endif(MSVC)
479491

@@ -525,7 +537,7 @@ endforeach()
525537
add_executable(database_load_insert_tests DatabaseLoadInsertTests.cpp ${LIBRARY_SOURCES})
526538

527539
if(MSVC)
528-
target_compile_definitions(database_load_insert_tests PRIVATE
540+
target_compile_definitions(database_load_insert_tests PRIVATE
529541
_CRT_DECLARE_NONSTDC_NAMES=0 FASTDDS_STATISTICS_BACKEND_SOURCE)
530542
endif(MSVC)
531543

@@ -565,7 +577,7 @@ endforeach()
565577
add_executable(database_status_tests DatabaseStatusTests.cpp ${LIBRARY_SOURCES})
566578

567579
if(MSVC)
568-
target_compile_definitions(database_status_tests PRIVATE
580+
target_compile_definitions(database_status_tests PRIVATE
569581
_CRT_DECLARE_NONSTDC_NAMES=0 FASTDDS_STATISTICS_BACKEND_SOURCE)
570582
endif(MSVC)
571583

@@ -615,7 +627,7 @@ endforeach()
615627
add_executable(database_erase_tests DatabaseEraseTests.cpp ${LIBRARY_SOURCES})
616628

617629
if(MSVC)
618-
target_compile_definitions(database_erase_tests PRIVATE
630+
target_compile_definitions(database_erase_tests PRIVATE
619631
_CRT_DECLARE_NONSTDC_NAMES=0 FASTDDS_STATISTICS_BACKEND_SOURCE)
620632
endif(MSVC)
621633

test/unittest/Database/DatabaseTests.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,9 +2432,114 @@ TEST_F(database_tests, get_entity_locator)
24322432
ASSERT_EQ(local_writer_locator.get(), writer_locator.get());
24332433
}
24342434

2435+
TEST_F(database_tests, get_entity_host_efficient_search)
2436+
{
2437+
auto local_host = db.get_entity(host_id, EntityKind::HOST);
2438+
ASSERT_EQ(local_host.get(), host.get());
2439+
}
2440+
2441+
TEST_F(database_tests, get_entity_process_efficient_search)
2442+
{
2443+
auto local_process = db.get_entity(process_id, EntityKind::PROCESS);
2444+
ASSERT_EQ(local_process.get(), process.get());
2445+
}
2446+
2447+
TEST_F(database_tests, get_entity_user_efficient_search)
2448+
{
2449+
auto local_user = db.get_entity(user_id, EntityKind::USER);
2450+
ASSERT_EQ(local_user.get(), user.get());
2451+
}
2452+
2453+
TEST_F(database_tests, get_entity_domain_efficient_search)
2454+
{
2455+
auto local_domain = db.get_entity(domain_id, EntityKind::DOMAIN);
2456+
ASSERT_EQ(local_domain.get(), domain.get());
2457+
}
2458+
2459+
TEST_F(database_tests, get_entity_topic_efficient_search)
2460+
{
2461+
auto local_topic = db.get_entity(topic_id, EntityKind::TOPIC);
2462+
ASSERT_EQ(local_topic.get(), topic.get());
2463+
}
2464+
2465+
TEST_F(database_tests, get_entity_participant_efficient_search)
2466+
{
2467+
auto local_participant = db.get_entity(participant_id, EntityKind::PARTICIPANT);
2468+
ASSERT_EQ(local_participant.get(), participant.get());
2469+
}
2470+
2471+
TEST_F(database_tests, get_entity_datareader_efficient_search)
2472+
{
2473+
auto local_reader = db.get_entity(reader_id, EntityKind::DATAREADER);
2474+
ASSERT_EQ(local_reader.get(), reader.get());
2475+
}
2476+
2477+
TEST_F(database_tests, get_entity_datawriter_efficient_search)
2478+
{
2479+
auto local_writer = db.get_entity(writer_id, EntityKind::DATAWRITER);
2480+
ASSERT_EQ(local_writer.get(), writer.get());
2481+
}
2482+
2483+
TEST_F(database_tests, get_entity_locator_efficient_search)
2484+
{
2485+
auto local_reader_locator = db.get_entity(reader_locator->id, EntityKind::LOCATOR);
2486+
ASSERT_EQ(local_reader_locator.get(), reader_locator.get());
2487+
auto local_writer_locator = db.get_entity(writer_locator->id, EntityKind::LOCATOR);
2488+
ASSERT_EQ(local_writer_locator.get(), writer_locator.get());
2489+
}
2490+
24352491
TEST_F(database_tests, get_entity_no_existing)
24362492
{
24372493
ASSERT_THROW(db.get_entity(EntityId()), BadParameter);
2494+
// With specific kind
2495+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::HOST), BadParameter);
2496+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::USER), BadParameter);
2497+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::PROCESS), BadParameter);
2498+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::DOMAIN), BadParameter);
2499+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::TOPIC), BadParameter);
2500+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::PARTICIPANT), BadParameter);
2501+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::DATAREADER), BadParameter);
2502+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::DATAWRITER), BadParameter);
2503+
ASSERT_THROW(db.get_entity(EntityId(), EntityKind::LOCATOR), BadParameter);
2504+
}
2505+
2506+
TEST_F(database_tests, get_entity_no_correct_kind)
2507+
{
2508+
std::map<EntityId, EntityKind> correct_kinds = {
2509+
{host_id, EntityKind::HOST},
2510+
{process_id, EntityKind::PROCESS},
2511+
{user_id, EntityKind::USER},
2512+
{domain_id, EntityKind::DOMAIN},
2513+
{topic_id, EntityKind::TOPIC},
2514+
{participant_id, EntityKind::PARTICIPANT},
2515+
{reader_id, EntityKind::DATAREADER},
2516+
{writer_id, EntityKind::DATAWRITER},
2517+
{reader_locator->id, EntityKind::LOCATOR},
2518+
{writer_locator->id, EntityKind::LOCATOR}
2519+
};
2520+
2521+
std::vector<EntityKind> all_kinds = {
2522+
EntityKind::HOST,
2523+
EntityKind::PROCESS,
2524+
EntityKind::USER,
2525+
EntityKind::DOMAIN,
2526+
EntityKind::TOPIC,
2527+
EntityKind::PARTICIPANT,
2528+
EntityKind::DATAREADER,
2529+
EntityKind::DATAWRITER,
2530+
EntityKind::LOCATOR
2531+
};
2532+
2533+
for (auto correct_kind : correct_kinds)
2534+
{
2535+
for (auto kind : all_kinds)
2536+
{
2537+
if (kind != correct_kind.second)
2538+
{
2539+
ASSERT_THROW(db.get_entity(correct_kind.first, kind), BadParameter);
2540+
}
2541+
}
2542+
}
24382543
}
24392544

24402545
TEST_F(database_tests, get_entities_by_name_host)

0 commit comments

Comments
 (0)