COOPENOMICS  v1
Кооперативная Экономика
cleanup.hpp
См. документацию.
1#pragma once
2
3#include <eosio/eosio.hpp>
4#include <eosio/../../capi/eosio/db.h> // C API узла; тот же путь, что у CDT в instant_finality.hpp
5#include <limits>
6#include <map>
7#include <type_traits>
8
27namespace Cleanup {
28
29 static constexpr uint32_t BUDGET = 100;
30
40 inline bool row_exists(eosio::name code, uint64_t scope, eosio::name table, uint64_t primary_key) {
41 return db_find_i64(code.value, scope, table.value, primary_key) >= 0;
42 }
43
51 template <typename Index>
52 inline bool next_index_value(eosio::name code, uint64_t scope, uint64_t from, uint64_t &value) {
53 uint64_t secondary = from;
54 uint64_t primary = 0;
55 const int32_t itr = db_idx64_lowerbound(code.value, scope, Index::name(), &secondary, &primary);
56 if (itr < 0) return false;
57 value = secondary;
58 return true;
59 }
60
62 struct budget {
63 uint32_t left = BUDGET;
64 uint32_t erased = 0;
65
66 bool exhausted() const { return left == 0; }
67 void take() {
68 --left;
69 ++erased;
70 }
71 };
72
79 template <typename Index, typename Pred>
80 inline void erase_where(Index &index, budget &b, Pred pred) {
81 for (auto it = index.begin(); it != index.end() && !b.exhausted();) {
82 if (pred(*it)) {
83 it = index.erase(it);
84 b.take();
85 } else {
86 ++it;
87 }
88 }
89 }
90
92 template <typename Table>
93 inline void erase_all(Table &table, budget &b) {
94 erase_where(table, b, [](const auto &) { return true; });
95 }
96
98 inline void report(eosio::name contract, const budget &b) {
99 eosio::print("cleanup ", contract, ": удалено строк ", b.erased,
100 b.exhausted() ? ", бюджет исчерпан — продолжит следующий вызов" : "");
101 }
102
103} // namespace Cleanup
contract
Definition: eosio.msig_tests.cpp:977
Очистка отработавших записей — общее для действия cleanup всех контрактов.
Definition: cleanup.hpp:27
void erase_where(Index &index, budget &b, Pred pred)
Удаляет строки индекса или таблицы, для которых выполнено условие.
Definition: cleanup.hpp:80
void erase_all(Table &table, budget &b)
Удаляет все строки таблицы, которую код больше не читает и не пишет.
Definition: cleanup.hpp:93
static constexpr uint32_t BUDGET
Строк на один вызов cleanup
Definition: cleanup.hpp:29
void report(eosio::name contract, const budget &b)
Итог вызова в консоль транзакции — его видит лог плейбука.
Definition: cleanup.hpp:98
bool row_exists(eosio::name code, uint64_t scope, eosio::name table, uint64_t primary_key)
Есть ли в таблице строка с таким первичным ключом — без разбора строки.
Definition: cleanup.hpp:40
bool next_index_value(eosio::name code, uint64_t scope, uint64_t from, uint64_t &value)
Следующее значение вторичного индекса uint64 не меньше from — без разбора строк.
Definition: cleanup.hpp:52
Остаток строк, которые текущий вызов ещё может удалить.
Definition: cleanup.hpp:62
bool exhausted() const
Definition: cleanup.hpp:66
void take()
Definition: cleanup.hpp:67
uint32_t erased
Definition: cleanup.hpp:64
uint32_t left
Definition: cleanup.hpp:63