SpellingsDB is created in a cache directory

The path parameter of the SpellingsDB constructor has been changed to
denote the path of the cache directory where spellings databases for
different ZIM archive should be stored. The filename of the spellings
database is generated from the ZIM archive UUID and the current version
of the spellings database implementation.
This commit is contained in:
Veloman Yunkan
2025-10-06 16:24:48 +04:00
parent 39672f0532
commit 5c8aa240ad
3 changed files with 20 additions and 6 deletions

View File

@@ -41,7 +41,7 @@ namespace kiwix
class SpellingsDB
{
public: // functions
SpellingsDB(const zim::Archive& archive, std::filesystem::path path);
SpellingsDB(const zim::Archive& archive, std::filesystem::path cacheDirPath);
~SpellingsDB();
SpellingsDB(const SpellingsDB& ) = delete;

View File

@@ -20,6 +20,7 @@
#include "spelling_correction.h"
#include "zim/archive.h"
#include <sstream>
#include <stdexcept>
#include <xapian.h>
@@ -53,8 +54,21 @@ void createXapianDB(std::string path, const zim::Archive& archive)
std::filesystem::remove_all(tmpDbPath);
}
std::unique_ptr<Xapian::Database> openOrCreateXapianDB(std::string path, const zim::Archive& archive)
std::string spellingsDBPathForZIMArchive(std::filesystem::path cacheDirPath, const zim::Archive& a)
{
// The version of spellings DB must be updated each time an important change
// to the implementation is made that renders using the previous version
// impossible or undesirable.
const char SPELLINGS_DB_VERSION[] = "0.1";
std::ostringstream filename;
filename << a.getUuid() << ".spellingsdb.v" << SPELLINGS_DB_VERSION;
return (cacheDirPath / filename.str()).string();
}
std::unique_ptr<Xapian::Database> openOrCreateXapianDB(std::filesystem::path cacheDirPath, const zim::Archive& archive)
{
const auto path = spellingsDBPathForZIMArchive(cacheDirPath, archive);
try
{
return std::make_unique<Xapian::Database>(path);
@@ -68,8 +82,8 @@ std::unique_ptr<Xapian::Database> openOrCreateXapianDB(std::string path, const z
} // unnamed namespace
SpellingsDB::SpellingsDB(const zim::Archive& archive, std::filesystem::path path)
: impl_(openOrCreateXapianDB(path.string(), archive))
SpellingsDB::SpellingsDB(const zim::Archive& archive, std::filesystem::path cacheDirPath)
: impl_(openOrCreateXapianDB(cacheDirPath, archive))
{
}

View File

@@ -192,14 +192,14 @@ TEST_F(SpellingCorrectionTest, SpellingsDBCannotBeCreatedInAReadOnlyDirectory)
makeTmpDirReadOnly();
EXPECT_THROW(
const kiwix::SpellingsDB spellingsDB(*archive, tmpDirPath / "spellings.db"),
const kiwix::SpellingsDB spellingsDB(*archive, tmpDirPath),
Xapian::DatabaseCreateError
);
}
TEST_F(SpellingCorrectionTest, allInOne)
{
const auto spellingsDbPath = tmpDirPath / "spellings.db";
const auto spellingsDbPath = tmpDirPath;
{
const kiwix::SpellingsDB spellingsDB(*archive, spellingsDbPath);