Make the `UpdatableNameMapper` keep a `shared_ptr`.

Same as `Manager`, we want to be sure that `Library` actually exists
when we use it.
This commit is contained in:
Matthieu Gautier 2023-09-25 16:13:52 +02:00
parent 139b561253
commit efcbf6ef1e
3 changed files with 6 additions and 6 deletions

View File

@ -59,7 +59,7 @@ class HumanReadableNameMapper : public NameMapper {
class UpdatableNameMapper : public NameMapper { class UpdatableNameMapper : public NameMapper {
typedef std::shared_ptr<NameMapper> NameMapperHandle; typedef std::shared_ptr<NameMapper> NameMapperHandle;
public: public:
UpdatableNameMapper(Library& library, bool withAlias); UpdatableNameMapper(std::shared_ptr<Library> library, bool withAlias);
virtual std::string getNameForId(const std::string& id) const; virtual std::string getNameForId(const std::string& id) const;
virtual std::string getIdForName(const std::string& name) const; virtual std::string getIdForName(const std::string& name) const;
@ -71,7 +71,7 @@ class UpdatableNameMapper : public NameMapper {
private: private:
mutable std::mutex mutex; mutable std::mutex mutex;
Library& library; std::shared_ptr<Library> library;
NameMapperHandle nameMapper; NameMapperHandle nameMapper;
const bool withAlias; const bool withAlias;
}; };

View File

@ -63,7 +63,7 @@ std::string HumanReadableNameMapper::getIdForName(const std::string& name) const
// UpdatableNameMapper // UpdatableNameMapper
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
UpdatableNameMapper::UpdatableNameMapper(Library& lib, bool withAlias) UpdatableNameMapper::UpdatableNameMapper(std::shared_ptr<Library> lib, bool withAlias)
: library(lib) : library(lib)
, withAlias(withAlias) , withAlias(withAlias)
{ {
@ -72,7 +72,7 @@ UpdatableNameMapper::UpdatableNameMapper(Library& lib, bool withAlias)
void UpdatableNameMapper::update() void UpdatableNameMapper::update()
{ {
const auto newNameMapper = new HumanReadableNameMapper(library, withAlias); const auto newNameMapper = new HumanReadableNameMapper(*library, withAlias);
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
nameMapper.reset(newNameMapper); nameMapper.reset(newNameMapper);
} }

View File

@ -110,7 +110,7 @@ TEST_F(NameMapperTest, HumanReadableNameMapperWithAliases)
TEST_F(NameMapperTest, UpdatableNameMapperWithoutAliases) TEST_F(NameMapperTest, UpdatableNameMapperWithoutAliases)
{ {
CapturedStderr stderror; CapturedStderr stderror;
kiwix::UpdatableNameMapper nm(*lib, false); kiwix::UpdatableNameMapper nm(lib, false);
EXPECT_EQ("", std::string(stderror)); EXPECT_EQ("", std::string(stderror));
checkUnaliasedEntriesInNameMapper(nm); checkUnaliasedEntriesInNameMapper(nm);
@ -126,7 +126,7 @@ TEST_F(NameMapperTest, UpdatableNameMapperWithoutAliases)
TEST_F(NameMapperTest, UpdatableNameMapperWithAliases) TEST_F(NameMapperTest, UpdatableNameMapperWithAliases)
{ {
CapturedStderr stderror; CapturedStderr stderror;
kiwix::UpdatableNameMapper nm(*lib, true); kiwix::UpdatableNameMapper nm(lib, true);
EXPECT_EQ( EXPECT_EQ(
"Path collision: /data/zero_four_2021-10.zim and" "Path collision: /data/zero_four_2021-10.zim and"
" /data/zero_four_2021-11.zim can't share the same URL path 'zero_four'." " /data/zero_four_2021-11.zim can't share the same URL path 'zero_four'."