Renamed NameMapperProxy -> UpdatableNameMapper

This commit is contained in:
Veloman Yunkan 2021-11-27 21:47:24 +04:00
parent 3aeeeeee76
commit 298247ca9b
3 changed files with 16 additions and 16 deletions

View File

@ -56,10 +56,10 @@ class HumanReadableNameMapper : public NameMapper {
virtual std::string getIdForName(const std::string& name) const; virtual std::string getIdForName(const std::string& name) const;
}; };
class NameMapperProxy : public NameMapper { class UpdatableNameMapper : public NameMapper {
typedef std::shared_ptr<NameMapper> NameMapperHandle; typedef std::shared_ptr<NameMapper> NameMapperHandle;
public: public:
NameMapperProxy(Library& library, bool withAlias); UpdatableNameMapper(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;

View File

@ -60,44 +60,44 @@ std::string HumanReadableNameMapper::getIdForName(const std::string& name) const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// NameMapperProxy // UpdatableNameMapper
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
NameMapperProxy::NameMapperProxy(Library& lib, bool withAlias) UpdatableNameMapper::UpdatableNameMapper(Library& lib, bool withAlias)
: library(lib) : library(lib)
, withAlias(withAlias) , withAlias(withAlias)
{ {
update(); update();
} }
void NameMapperProxy::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);
} }
NameMapperProxy::NameMapperHandle UpdatableNameMapper::NameMapperHandle
NameMapperProxy::currentNameMapper() const UpdatableNameMapper::currentNameMapper() const
{ {
// Return a copy of the handle to the current NameMapper object. It will // Return a copy of the handle to the current NameMapper object. It will
// ensure that the object survives any call to NameMapperProxy::update() // ensure that the object survives any call to UpdatableNameMapper::update()
// made before the completion of any pending operation on that object. // made before the completion of any pending operation on that object.
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
return nameMapper; return nameMapper;
} }
std::string NameMapperProxy::getNameForId(const std::string& id) const std::string UpdatableNameMapper::getNameForId(const std::string& id) const
{ {
// Ensure that the current nameMapper object survives a concurrent call // Ensure that the current nameMapper object survives a concurrent call
// to NameMapperProxy::update() // to UpdatableNameMapper::update()
return currentNameMapper()->getNameForId(id); return currentNameMapper()->getNameForId(id);
} }
std::string NameMapperProxy::getIdForName(const std::string& name) const std::string UpdatableNameMapper::getIdForName(const std::string& name) const
{ {
// Ensure that the current nameMapper object survives a concurrent call // Ensure that the current nameMapper object survives a concurrent call
// to NameMapperProxy::update() // to UpdatableNameMapper::update()
return currentNameMapper()->getIdForName(name); return currentNameMapper()->getIdForName(name);
} }

View File

@ -103,10 +103,10 @@ TEST_F(NameMapperTest, HumanReadableNameMapperWithAliases)
EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four")); EXPECT_EQ("04-2021-10", nm.getIdForName("zero_four"));
} }
TEST_F(NameMapperTest, NameMapperProxyWithoutAliases) TEST_F(NameMapperTest, UpdatableNameMapperWithoutAliases)
{ {
CapturedStderr stderror; CapturedStderr stderror;
kiwix::NameMapperProxy nm(lib, false); kiwix::UpdatableNameMapper nm(lib, false);
EXPECT_EQ("", std::string(stderror)); EXPECT_EQ("", std::string(stderror));
checkUnaliasedEntriesInNameMapper(nm); checkUnaliasedEntriesInNameMapper(nm);
@ -119,10 +119,10 @@ TEST_F(NameMapperTest, NameMapperProxyWithoutAliases)
EXPECT_THROW(nm.getIdForName("zero_four"), std::out_of_range); EXPECT_THROW(nm.getIdForName("zero_four"), std::out_of_range);
} }
TEST_F(NameMapperTest, NameMapperProxyWithAliases) TEST_F(NameMapperTest, UpdatableNameMapperWithAliases)
{ {
CapturedStderr stderror; CapturedStderr stderror;
kiwix::NameMapperProxy 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'."