-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb_size_api_plugin.cpp
More file actions
61 lines (46 loc) · 1.88 KB
/
db_size_api_plugin.cpp
File metadata and controls
61 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <fc/variant.hpp>
#include <fc/io/json.hpp>
#include <eosio/db_size_api_plugin/db_size_api_plugin.hpp>
#include <eosio/http_plugin/http_plugin.hpp>
namespace eosio {
static appbase::abstract_plugin& _db_size_api_plugin = app().register_plugin<db_size_api_plugin>();
using namespace eosio;
#define CALL_WITH_400(api_name, api_handle, call_name, INVOKE, http_response_code) \
{std::string("/v1/" #api_name "/" #call_name), \
[api_handle](string, string body, url_response_callback cb) mutable { \
try { \
body = parse_params<std::string, http_params_types::no_params_required>(body); \
INVOKE \
cb(http_response_code, fc::variant(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
}}
#define INVOKE_R_V(api_handle, call_name) \
auto result = api_handle->call_name();
void db_size_api_plugin::plugin_startup() {
app().get_plugin<http_plugin>().add_api({
CALL_WITH_400(db_size, this, get, INVOKE_R_V(this, get), 200),
CALL_WITH_400(db_size, this, get_reversible, INVOKE_R_V(this, get_reversible), 200),
});
}
db_size_stats db_size_api_plugin::get_db_stats(const chainbase::database& db) {
db_size_stats ret;
ret.free_bytes = db.get_segment_manager()->get_free_memory();
ret.size = db.get_segment_manager()->get_size();
ret.used_bytes = ret.size - ret.free_bytes;
chainbase::database::database_index_row_count_multiset indices = db.row_count_per_index();
for(const auto& i : indices)
ret.indices.emplace_back(db_size_index_count{i.second, i.first});
return ret;
}
db_size_stats db_size_api_plugin::get() {
return get_db_stats(app().get_plugin<chain_plugin>().chain().db());
}
db_size_stats db_size_api_plugin::get_reversible() {
// no longer have a reversible db
return {};
}
#undef INVOKE_R_V
#undef CALL
}