122 lines
3.5 KiB
C++
122 lines
3.5 KiB
C++
/*
|
|
Copyright© 2021 John Sennesael
|
|
|
|
UsenetSearch is Free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
UsenetSearch is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "usenetsearch/Filter.h"
|
|
#include "usenetsearch/Serialize.h"
|
|
#include "usenetsearch/UsenetClient.h"
|
|
|
|
#include <codecvt>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <locale>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
namespace usenetsearch {
|
|
|
|
class Application;
|
|
static constexpr const std::uint64_t DatabaseVersion{1};
|
|
|
|
struct ArticleEntry
|
|
{
|
|
std::array<std::uint8_t, 16> hash;
|
|
std::uint32_t newsgroupID;
|
|
std::uint32_t articleID;
|
|
};
|
|
|
|
std::fstream& operator<<(std::fstream& out, const ArticleEntry& obj);
|
|
std::fstream& operator>>(std::fstream& in, ArticleEntry& obj);
|
|
|
|
struct DatabaseException: public UsenetSearchException
|
|
{
|
|
DatabaseException(int errorCode, const std::string& message):
|
|
UsenetSearchException(errorCode, message){}
|
|
|
|
virtual ~DatabaseException() = default;
|
|
};
|
|
|
|
class Database
|
|
{
|
|
Application& m_app;
|
|
std::filesystem::path m_databasePath;
|
|
std::uint64_t m_databaseVersion{DatabaseVersion};
|
|
std::vector<std::filesystem::path> m_lockedFiles;
|
|
std::mutex m_lockedFilesMutex;
|
|
std::uint8_t m_maxTreeDepth{5};
|
|
void CheckDbVersion(const SerializableFile& f) const;
|
|
std::filesystem::path GetNewsGroupFilePath() const;
|
|
std::filesystem::path GetTokenFilePath(
|
|
const std::string& token,
|
|
bool mkdirs=false
|
|
);
|
|
std::uint64_t GetUniqueNntpEntryId(
|
|
const std::vector<NntpListEntry>& list
|
|
) const;
|
|
bool HasToken(
|
|
const std::string& subtoken,
|
|
std::uint64_t newsgroupID,
|
|
std::uint32_t articleID
|
|
);
|
|
std::unique_ptr<std::vector<ArticleEntry>> LoadTokens(
|
|
const std::filesystem::path dbFile,
|
|
const std::string& subtoken
|
|
);
|
|
void SaveToken(
|
|
const std::string& subToken,
|
|
std::uint64_t newsgroupID,
|
|
std::uint32_t articleID
|
|
);
|
|
static void SyncLastUpdated(
|
|
NntpListEntry& a,
|
|
NntpListEntry& b
|
|
);
|
|
|
|
public:
|
|
|
|
explicit Database(Application& app);
|
|
~Database();
|
|
std::unique_ptr<NntpListEntry> FindNntpEntry(const std::string& subject);
|
|
std::uint32_t GetLastIndexedArticle(std::uint64_t newsgroupID);
|
|
std::unique_ptr<std::vector<NntpListEntry>> LoadNewsgroupList();
|
|
void MaxTreeDepth(std::uint8_t depth);
|
|
void Open(std::filesystem::path dbPath);
|
|
void ParseTokenFile(
|
|
const std::filesystem::path& dbFile,
|
|
std::function<void(const ArticleEntry& entry)> onParse
|
|
);
|
|
void SaveSearchTokens(
|
|
std::uint64_t newsgroupID,
|
|
std::uint64_t articleID,
|
|
const std::string& searchString
|
|
);
|
|
std::unique_ptr<std::vector<ArticleEntry>> Search(
|
|
const std::string& searchString
|
|
);
|
|
void SetLastIndexedArticle(
|
|
std::uint64_t newsgroupID,
|
|
std::int32_t articleID
|
|
);
|
|
void UpdateNewsgroupList(std::vector<NntpListEntry>& list);
|
|
|
|
};
|
|
|
|
} // namespace usenetsearch
|