mirror of
https://github.com/kiwix/libkiwix.git
synced 2025-12-23 22:47:57 -05:00
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:
@@ -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;
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user