Nostro/implementations/Nostro-Account.cpp

117 lines
5.4 KiB
C++
Raw Normal View History

2026-11-01 18:35:23 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2026-10-31 20:50:57 +01:00
module Nostro:Account_impl;
import :Account;
import :File;
import std;
2026-11-01 18:35:23 +01:00
import Crafter.DB;
2026-10-31 20:50:57 +01:00
using namespace Nostro;
2026-11-01 18:35:23 +01:00
using namespace Crafter;
2026-10-31 20:50:57 +01:00
namespace fs = std::filesystem;
2026-11-01 18:35:23 +01:00
// 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);
2026-10-31 20:50:57 +01:00
2026-11-01 18:35:23 +01:00
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);
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
// 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;
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
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;
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
// 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;
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
// `search` is lower case already.
bool Contains(std::string_view text, std::string_view search) {
return !std::ranges::search(text, search, {}, Lower).empty();
2026-10-31 20:50:57 +01:00
}
}
2026-11-01 18:35:23 +01:00
std::chrono::system_clock::time_point Account::GetLastTransactionTime(std::string_view connector) const {
const DB::Table<Transaction>& table = GetTable(connector, name);
if(table.Empty()) {
return std::chrono::system_clock::time_point::min();
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
return table.Back()[&Transaction::timePoint];
}
2026-10-31 20:50:57 +01:00
2026-11-01 18:35:23 +01:00
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);
2026-10-31 20:50:57 +01:00
2026-11-01 18:35:23 +01:00
// 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);
}
2026-10-31 20:50:57 +01:00
}
}
2026-11-01 18:35:23 +01:00
// 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)));
});
2026-10-31 20:50:57 +01:00
2026-11-01 18:35:23 +01:00
table.Append(std::span<const Transaction>(transactions));
table.Flush();
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
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);
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
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>();
// 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;
2026-10-31 20:50:57 +01:00
}
2026-11-01 18:35:23 +01:00
if(search.empty() || Contains(row[&Transaction::name], search) || Contains(row[&Transaction::description], search) || Contains(row[&Transaction::iban], search)) {
rows.push_back(row.Materialize());
2026-10-31 20:50:57 +01:00
}
}
2026-11-01 18:35:23 +01:00
return rows;
}