cli MVP
This commit is contained in:
parent
367c19386c
commit
1ea50c50bc
62 changed files with 9982 additions and 608 deletions
|
|
@ -1,210 +1,117 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module Nostro:Account_impl;
|
||||
import :Account;
|
||||
import :File;
|
||||
import std;
|
||||
import Crafter.DB;
|
||||
|
||||
using namespace Nostro;
|
||||
using namespace Crafter;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// Transaction is the on-disk schema of the tables below. These fail when it changes, as a reminder to bump
|
||||
// Transaction::version; update them together with it.
|
||||
static_assert(DB::Layout<Transaction>::rowSize == 120);
|
||||
static_assert(DB::Layout<Transaction>::fingerprint == 0x1ceea246dfcdf4f0);
|
||||
// Version 1, which existing tables migrate from. It must never change.
|
||||
static_assert(DB::Layout<Transaction::Previous>::rowSize == 88);
|
||||
static_assert(DB::Layout<Transaction::Previous>::fingerprint == 0x6b478e471876761f);
|
||||
|
||||
namespace {
|
||||
// An account name is the file name of its table: one with a '/' in it would be a path.
|
||||
std::string FileName(std::string_view account) {
|
||||
std::string name(account);
|
||||
std::ranges::replace(name, '/', '_');
|
||||
return std::format("{}.cdb", name);
|
||||
}
|
||||
|
||||
// Each table is opened on first use and kept open for the rest of the process, or until its connector is
|
||||
// forgotten: an open maps the whole file, and a second writable Table on the same file would throw DB::LockedError.
|
||||
std::map<fs::path, DB::Table<Transaction>>& Tables() {
|
||||
static std::map<fs::path, DB::Table<Transaction>> tables;
|
||||
return tables;
|
||||
}
|
||||
|
||||
DB::Table<Transaction>& GetTable(std::string_view connector, std::string_view account) {
|
||||
const fs::path path = cachePath / "transactions" / connector / FileName(account);
|
||||
return Tables().try_emplace(path, path).first->second;
|
||||
}
|
||||
|
||||
// ASCII case folding: enough for names and descriptions, IBANs are upper case anyway.
|
||||
char Lower(char c) {
|
||||
return c >= 'A' && c <= 'Z' ? static_cast<char>(c + ('a' - 'A')) : c;
|
||||
}
|
||||
|
||||
// `search` is lower case already.
|
||||
bool Contains(std::string_view text, std::string_view search) {
|
||||
return !std::ranges::search(text, search, {}, Lower).empty();
|
||||
}
|
||||
}
|
||||
|
||||
std::chrono::system_clock::time_point Account::GetLastTransactionTime(std::string_view connector) const {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path) || fs::file_size(path) == 0) {
|
||||
const DB::Table<Transaction>& table = GetTable(connector, name);
|
||||
if(table.Empty()) {
|
||||
return std::chrono::system_clock::time_point::min();
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
file.seekg(-static_cast<std::streamoff>(sizeof(std::int64_t)), std::ios::end);
|
||||
std::int64_t lastTime;
|
||||
file.read(reinterpret_cast<char*>(&lastTime), sizeof(lastTime));
|
||||
return std::chrono::system_clock::time_point(std::chrono::microseconds(lastTime));
|
||||
return table.Back()[&Transaction::timePoint];
|
||||
}
|
||||
|
||||
void Account::SaveTransactions(std::string_view connector) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
CreatePrivateFile(path);
|
||||
std::ofstream file(path, std::ios::in | std::ios::out | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
void Account::SaveTransactions(std::string_view connector, std::vector<Transaction> transactions) {
|
||||
DB::Table<Transaction>& table = GetTable(connector, name);
|
||||
const std::chrono::system_clock::time_point last = GetLastTransactionTime(connector);
|
||||
|
||||
std::uint32_t pendingIndex = transactions.size();
|
||||
for(std::uint32_t i = 0; i < transactions.size(); i++) {
|
||||
if(transactions[i].timePoint > last) {
|
||||
pendingIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
file.seekp(0, std::ios::end);
|
||||
for(std::uint32_t i = pendingIndex; i < transactions.size(); i++) {
|
||||
WriteString(file, transactions[i].name);
|
||||
WriteString(file, transactions[i].bic);
|
||||
WriteString(file, transactions[i].iban);
|
||||
WriteString(file, transactions[i].description);
|
||||
file.write(reinterpret_cast<char*>(&transactions[i].amount), sizeof(transactions[i].amount));
|
||||
file.write(reinterpret_cast<char*>(&transactions[i].currency), sizeof(Currency));
|
||||
std::int64_t time = std::chrono::duration_cast<std::chrono::microseconds>(transactions[i].timePoint.time_since_epoch()).count();
|
||||
file.write(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
}
|
||||
}
|
||||
|
||||
Transaction& Account::LoadTransaction(std::ifstream& file) {
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
ReadString(file, tr.iban);
|
||||
ReadString(file, tr.description);
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.currency));
|
||||
std::int64_t time;
|
||||
file.read(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
tr.timePoint = std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::microseconds(time)));
|
||||
return tr;
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::fstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
LoadTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
std::ifstream Account::LoadUntilFrom(std::string_view connector, std::chrono::system_clock::time_point from) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
in.seekg(sizeof(tr.amount)+sizeof(tr.Currency), std::ios::cur);
|
||||
std::int64_t timeI;
|
||||
file.read(reinterpret_cast<char*>(&timeI), sizeof(tr.timeI));
|
||||
std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::from_time_t(timeI);
|
||||
if(timePoint > from) {
|
||||
in.seekg(-seeked-sizeof(tr.amount)-sizeof(tr.Currency)-sizeof(tr.timeI), std::ios::cur);
|
||||
LoadTransaction();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from, std::chrono::system_clock::time_point to) {
|
||||
std::ifstream file = LoadUntilFrom(connector, from);
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
Transaction& tr = LoadTransaction();
|
||||
if(tr.timePoint > to) {
|
||||
transactions.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from) {
|
||||
std::ifstream file = LoadUntilFrom(connector, from);
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
LoadTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Account::LoadTransactionsByProperty(std::string_view connector, std::string_view property) {
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
seeked += SeekString();
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
if(description.size() == size) {
|
||||
std::string readDescription;
|
||||
readDescription.resize(size);
|
||||
file.read(readDescription.data(), size);
|
||||
if(readDescription == description) {
|
||||
in.seekg(-seeked-readDescription.size()-sizeof(size), std::ios::cur);
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
ReadString(file, tr.iban);
|
||||
SeekString();
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&tr.time), sizeof(tr.time));
|
||||
// The ids stored at the last timestamp: a fetch that includes that timestamp again brings them back. A row there
|
||||
// without an id (stored by version 1) cannot be matched, so then nothing at that timestamp is taken as new.
|
||||
std::vector<std::string> known;
|
||||
bool unmatchable = false;
|
||||
if(!table.Empty()) {
|
||||
for(DB::RowId id = table.LowerBound<&Transaction::timePoint>(last); id < table.SlotCount(); id++) {
|
||||
const DB::Row<Transaction> row = table[id];
|
||||
if(!row.Erased()) {
|
||||
const std::string_view stored = row[&Transaction::id];
|
||||
unmatchable = unmatchable || stored.empty();
|
||||
known.emplace_back(stored);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The table is ordered by timePoint and refuses a row older than its last one. A row at the last timestamp is
|
||||
// new when its id is.
|
||||
std::ranges::stable_sort(transactions, {}, &Transaction::timePoint);
|
||||
std::erase_if(transactions, [last, unmatchable, &known](const Transaction& transaction) {
|
||||
return transaction.timePoint < last || (transaction.timePoint == last && (unmatchable || transaction.id.empty() || std::ranges::contains(known, transaction.id)));
|
||||
});
|
||||
|
||||
table.Append(std::span<const Transaction>(transactions));
|
||||
table.Flush();
|
||||
}
|
||||
|
||||
void Account::LoadTransactionsByIban(std::string_view connector, std::string_view iban) {
|
||||
transactions.clear();
|
||||
void Account::ForgetTransactions(std::string_view connector) {
|
||||
const fs::path directory = cachePath / "transactions" / connector;
|
||||
std::erase_if(Tables(), [&directory](const std::pair<const fs::path, DB::Table<Transaction>>& table) {
|
||||
return std::mismatch(directory.begin(), directory.end(), table.first.begin(), table.first.end()).first == directory.end();
|
||||
});
|
||||
fs::remove_all(directory);
|
||||
}
|
||||
|
||||
const fs::path path = cachePath / "transactions" / connector / (name + ".bin");
|
||||
if(!fs::exists(path)) {
|
||||
return;
|
||||
}
|
||||
std::vector<Transaction> Account::FindTransactions(std::string_view connector, const TransactionQuery& query) const {
|
||||
const DB::Table<Transaction>& table = GetTable(connector, name);
|
||||
const std::string search = query.search | std::views::transform(Lower) | std::ranges::to<std::string>();
|
||||
|
||||
std::ifstream file(path, std::ios::in | std::ios::binary);
|
||||
if(!file) {
|
||||
throw std::runtime_error(std::format("cannot open {}", path.string()));
|
||||
}
|
||||
|
||||
while(file.peek() != std::fstream::traits_type::eof()) {
|
||||
std::uint16_t seeked = SeekString();
|
||||
seeked += SeekString();
|
||||
std::uint16_t size;
|
||||
file.read(reinterpret_cast<char*>(&size), sizeof(size));
|
||||
if(iban.size() == size) {
|
||||
std::string readIban;
|
||||
readIban.resize(size);
|
||||
file.read(readIban.data(), size);
|
||||
if(readIban == iban) {
|
||||
in.seekg(-seeked-readIban.size()-sizeof(size), std::ios::cur);
|
||||
Transaction& tr = transactions.emplace_back();
|
||||
ReadString(file, tr.name);
|
||||
ReadString(file, tr.bic);
|
||||
SeekString();
|
||||
ReadString(file, tr.description);
|
||||
file.read(reinterpret_cast<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&tr.time), sizeof(tr.time));
|
||||
}
|
||||
// Walk the time range backwards from the newest row and stop at the limit, so a search that matches often
|
||||
// returns early; only a rare or failed one reads the whole range.
|
||||
const DB::RowId first = table.LowerBound<&Transaction::timePoint>(query.from);
|
||||
std::vector<Transaction> rows;
|
||||
for(DB::RowId id = table.UpperBound<&Transaction::timePoint>(query.to); id > first && rows.size() < query.limit; id--) {
|
||||
const DB::Row<Transaction> row = table[id - 1];
|
||||
if(row.Erased() || (query.amount && row[&Transaction::amount] != *query.amount)) {
|
||||
continue;
|
||||
}
|
||||
if(search.empty() || Contains(row[&Transaction::name], search) || Contains(row[&Transaction::description], search) || Contains(row[&Transaction::iban], search)) {
|
||||
rows.push_back(row.Materialize());
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue