UsenetSearch/include/usenetsearch/Database.h

118 lines
3.2 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 <codecvt>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <locale>
#include <memory>
#include <mutex>
#include <vector>
#include "usenetsearch/Serialize.h"
#include "usenetsearch/UsenetClient.h"
namespace usenetsearch {
static constexpr const std::uint64_t DatabaseVersion{1};
struct ArticleEntry
{
std::vector<std::uint64_t> articleIDs;
std::string searchString;
size_t Size() const;
};
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
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> m_conv;
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};
SerializableFile m_newsGroupFileIO;
bool GetArticleEntry(
const std::string& subToken,
const std::string& searchString,
ArticleEntry& entry,
size_t& startPosition,
size_t& endPosition,
size_t& count);
std::filesystem::path GetArticleFilePath(
const std::wstring& newsgroup,
bool mkdirs=false
);
std::filesystem::path GetTokenFilePath(
const std::string& token,
bool mkdirs=false
);
bool HasToken(
const std::string& subToken,
const std::string& subject,
std::uint64_t articleID
);
void LockFile(std::filesystem::path file);
void UnlockFile(std::filesystem::path file);
void OpenNewsGroupFile();
void SaveToken(
const std::string& subToken,
const std::string& subject,
std::uint64_t articleID
);
public:
~Database();
std::unique_ptr<std::vector<NntpHeader>> LoadArticleList(
const std::wstring& newsgroup
);
std::unique_ptr<std::vector<NntpListEntry>> LoadNewsgroupList();
void Open(std::filesystem::path dbPath);
void UpdateArticleList(
const std::wstring& newsgroup,
const std::vector<NntpHeader>& headers
);
void UpdateNewsgroupList(const std::vector<NntpListEntry>& list);
void SaveSearchTokens(
std::uint64_t articleID,
const std::string& searchString
);
};
} // namespace usenetsearch