UsenetSearch/include/usenetsearch/Database.h

71 lines
2.0 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 <vector>
#include "usenetsearch/UsenetClient.h"
namespace usenetsearch {
static constexpr const std::uint64_t DatabaseVersion{1};
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::ifstream m_newsGroupFileInput;
std::ofstream m_newsGroupFileOutput;
std::filesystem::path GetArticleFilePath(const std::wstring& newsgroup);
void OpenNewsGroupFile();
public:
~Database();
std::unique_ptr<std::vector<std::wstring>> 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<std::wstring>& articleIds
);
void UpdateNewsgroupList(const std::vector<NntpListEntry>& list);
};
} // namespace usenetsearch