// 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::rowSize == 120); static_assert(DB::Layout::fingerprint == 0x1ceea246dfcdf4f0); // Version 1, which existing tables migrate from. It must never change. static_assert(DB::Layout::rowSize == 88); static_assert(DB::Layout::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>& Tables() { static std::map> tables; return tables; } DB::Table& 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(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 DB::Table& table = GetTable(connector, name); if(table.Empty()) { return std::chrono::system_clock::time_point::min(); } return table.Back()[&Transaction::timePoint]; } void Account::SaveTransactions(std::string_view connector, std::vector transactions) { DB::Table& table = GetTable(connector, name); const std::chrono::system_clock::time_point last = GetLastTransactionTime(connector); // 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 known; bool unmatchable = false; if(!table.Empty()) { for(DB::RowId id = table.LowerBound<&Transaction::timePoint>(last); id < table.SlotCount(); id++) { const DB::Row 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(transactions)); table.Flush(); } void Account::ForgetTransactions(std::string_view connector) { const fs::path directory = cachePath / "transactions" / connector; std::erase_if(Tables(), [&directory](const std::pair>& table) { return std::mismatch(directory.begin(), directory.end(), table.first.begin(), table.first.end()).first == directory.end(); }); fs::remove_all(directory); } std::vector Account::FindTransactions(std::string_view connector, const TransactionQuery& query) const { const DB::Table& table = GetTable(connector, name); const std::string search = query.search | std::views::transform(Lower) | std::ranges::to(); // 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 rows; for(DB::RowId id = table.UpperBound<&Transaction::timePoint>(query.to); id > first && rows.size() < query.limit; id--) { const DB::Row 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; }