Skip to content

Commit 36c5f3e

Browse files
author
jparisu
committed
Refs #12001: apply to listeners
Signed-off-by: jparisu <[email protected]>
1 parent 0514cab commit 36c5f3e

File tree

10 files changed

+356
-200
lines changed

10 files changed

+356
-200
lines changed

src/cpp/database/database.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,13 @@ std::vector<const StatisticsSample*> Database::select(
15111511
}
15121512
else
15131513
{
1514-
if (source_entity->kind != kinds.first)
1515-
{
1516-
continue;
1517-
}
1514+
// There are no possibility with the current kinds to arrive to this branch
1515+
assert(source_entity->kind == kinds.first);
1516+
// If in the future new kinds are added that could use this branch, use this code
1517+
// if (source_entity->kind != kinds.first)
1518+
// {
1519+
// continue;
1520+
// }
15181521
}
15191522

15201523
if (!target_entity)
@@ -1523,10 +1526,13 @@ std::vector<const StatisticsSample*> Database::select(
15231526
}
15241527
else
15251528
{
1526-
if (target_entity->kind != kinds.second)
1527-
{
1528-
continue;
1529-
}
1529+
// There are no possibility with the current kinds to arrive to this branch
1530+
assert(target_entity->kind == kinds.second);
1531+
// If in the future new kinds are added that could use this branch, use this code
1532+
// if (target_entity->kind != kinds.second)
1533+
// {
1534+
// continue;
1535+
// }
15301536
}
15311537
}
15321538
catch (const std::exception& e)
@@ -2026,7 +2032,7 @@ EntityKind Database::get_entity_kind(
20262032
const std::vector<std::shared_ptr<const Entity>> Database::get_entities(
20272033
EntityKind entity_kind,
20282034
const EntityId& entity_id,
2029-
EntityKind source_entity_kind /* = EntityKind::INVALID */) const
2035+
const EntityKind source_entity_kind /* = EntityKind::INVALID */) const
20302036
{
20312037
std::shared_ptr<const Entity> origin;
20322038

@@ -2061,7 +2067,7 @@ const std::vector<std::shared_ptr<const Entity>> Database::get_entities(
20612067
std::vector<EntityId> Database::get_entity_ids(
20622068
EntityKind entity_kind,
20632069
const EntityId& entity_id,
2064-
EntityKind source_entity_kind /* = EntityKind::INVALID */) const
2070+
const EntityKind source_entity_kind /* = EntityKind::INVALID */) const
20652071
{
20662072
std::vector<EntityId> entitiesIds;
20672073
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: 17 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
{
@@ -134,7 +136,6 @@ void StatisticsParticipantListener::process_endpoint_discovery(
134136
auto endpoint = create_endpoint(endpoint_guid, info, participant, topic);
135137

136138
/* Start processing the locator info */
137-
138139
// Routine to process one locator from the locator list of the endpoint
139140
auto process_locators = [&](const Locator_t& dds_locator)
140141
{
@@ -319,7 +320,9 @@ void StatisticsParticipantListener::on_participant_discovery(
319320
database::EntityDiscoveryInfo entity_discovery_info;
320321
entity_discovery_info.domain_id = domain_id_;
321322
entity_discovery_info.entity = std::const_pointer_cast<database::DomainParticipant>(
322-
std::static_pointer_cast<const database::DomainParticipant>(database_->get_entity(participant_id)));
323+
std::static_pointer_cast<const database::DomainParticipant>(database_->get_entity(
324+
participant_id,
325+
EntityKind::PARTICIPANT)));
323326

324327
switch (info.status)
325328
{
@@ -352,7 +355,9 @@ void StatisticsParticipantListener::on_participant_discovery(
352355
// This may throw if the domain does not exist
353356
// The database MUST contain the domain, or something went wrong upstream
354357
std::shared_ptr<database::Domain> domain = std::const_pointer_cast<database::Domain>(
355-
std::static_pointer_cast<const database::Domain>(database_->get_entity(domain_id_)));
358+
std::static_pointer_cast<const database::Domain>(database_->get_entity(
359+
domain_id_,
360+
EntityKind::DOMAIN)));
356361

357362
std::string name = info.info.m_participantName.to_string();
358363

@@ -418,7 +423,9 @@ void StatisticsParticipantListener::on_subscriber_discovery(
418423
database::EntityDiscoveryInfo entity_discovery_info;
419424
entity_discovery_info.domain_id = domain_id_;
420425
entity_discovery_info.entity = std::const_pointer_cast<database::DataReader>(
421-
std::static_pointer_cast<const database::DataReader>(database_->get_entity(datareader_id)));
426+
std::static_pointer_cast<const database::DataReader>(database_->get_entity(
427+
datareader_id,
428+
EntityKind::DATAREADER)));
422429

423430
switch (info.status)
424431
{
@@ -488,7 +495,9 @@ void StatisticsParticipantListener::on_publisher_discovery(
488495
database::EntityDiscoveryInfo entity_discovery_info;
489496
entity_discovery_info.domain_id = domain_id_;
490497
entity_discovery_info.entity = std::const_pointer_cast<database::DataWriter>(
491-
std::static_pointer_cast<const database::DataWriter>(database_->get_entity(datawriter_id)));
498+
std::static_pointer_cast<const database::DataWriter>(database_->get_entity(
499+
datawriter_id,
500+
EntityKind::DATAWRITER)));
492501

493502
switch (info.status)
494503
{

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)