This commit is contained in:
parent
14cbde2586
commit
412b807d3f
@ -44,13 +44,14 @@ meta_db::meta_db(const app_config &cfg) {
|
||||
"data TEXT, "
|
||||
"directory INTEGER, "
|
||||
"pinned INTEGER, "
|
||||
"size INTEGER, "
|
||||
"source_path TEXT"
|
||||
");"},
|
||||
},
|
||||
};
|
||||
|
||||
db_ = utils::db::sqlite::create_db(
|
||||
utils::path::combine(cfg.get_data_directory(), {"meta.db"}),
|
||||
utils::path::combine(cfg.get_data_directory(), {"provider_meta.db"}),
|
||||
sql_create_tables);
|
||||
}
|
||||
|
||||
@ -114,9 +115,10 @@ auto meta_db::get_item_meta(const std::string &api_path, api_meta_map &meta)
|
||||
row->get_column("directory").get_value<std::int64_t>() == 1);
|
||||
meta[META_PINNED] = utils::string::from_bool(
|
||||
row->get_column("pinned").get_value<std::int64_t>() == 1);
|
||||
meta[META_SIZE] = std::to_string(static_cast<std::uint64_t>(
|
||||
row->get_column("size").get_value<std::int64_t>()));
|
||||
meta[META_SOURCE] =
|
||||
row->get_column("source_path").get_value<std::string>();
|
||||
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
@ -156,6 +158,9 @@ auto meta_db::get_item_meta(const std::string &api_path, const std::string &key,
|
||||
: key == META_DIRECTORY
|
||||
? utils::string::from_bool(
|
||||
row->get_column("directory").get_value<std::int64_t>() == 1)
|
||||
: key == META_SIZE
|
||||
? std::to_string(static_cast<std::uint64_t>(
|
||||
row->get_column("size").get_value<std::int64_t>()))
|
||||
: json::parse(
|
||||
row->get_column("data").get_value<std::string>())[key]
|
||||
.get<std::string>();
|
||||
@ -221,38 +226,23 @@ auto meta_db::get_total_item_count() const -> std::uint64_t {
|
||||
auto meta_db::get_total_size() const -> std::uint64_t {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
std::uint64_t ret{};
|
||||
|
||||
try {
|
||||
auto result = utils::db::sqlite::db_select{*db_, table_name}
|
||||
.column("api_path")
|
||||
.column("SUM(size) as total_size")
|
||||
.where("directory")
|
||||
.equals(0)
|
||||
.go();
|
||||
|
||||
while (result.has_row()) {
|
||||
std::optional<utils::db::sqlite::db_result::row> row;
|
||||
if (not result.get_row(row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (not row.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto api_path = row->get_column("api_path").get_value<std::string>();
|
||||
std::string size_str;
|
||||
if (get_item_meta(api_path, META_SIZE, size_str) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ret += utils::string::to_uint64(size_str);
|
||||
std::optional<utils::db::sqlite::db_result::row> row;
|
||||
if (result.get_row(row) && row.has_value()) {
|
||||
return static_cast<std::uint64_t>(
|
||||
row->get_column("total_size").get_value<std::int64_t>());
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::raise_error(function_name, e, "failed to get total size");
|
||||
}
|
||||
|
||||
return ret;
|
||||
return 0U;
|
||||
}
|
||||
|
||||
void meta_db::remove_api_path(const std::string &api_path) {
|
||||
@ -317,10 +307,13 @@ auto meta_db::update_item_meta(const std::string &api_path, api_meta_map meta)
|
||||
|
||||
auto directory = utils::string::to_bool(meta[META_DIRECTORY]);
|
||||
auto pinned = utils::string::to_bool(meta[META_PINNED]);
|
||||
auto size =
|
||||
directory ? std::uint64_t(0U) : utils::string::to_uint64(meta[META_SIZE]);
|
||||
auto source_path = meta[META_SOURCE];
|
||||
|
||||
meta.erase(META_DIRECTORY);
|
||||
meta.erase(META_PINNED);
|
||||
meta.erase(META_SIZE);
|
||||
meta.erase(META_SOURCE);
|
||||
|
||||
auto result = utils::db::sqlite::db_insert{*db_, table_name}
|
||||
@ -329,6 +322,7 @@ auto meta_db::update_item_meta(const std::string &api_path, api_meta_map meta)
|
||||
.column_value("data", nlohmann::json(meta).dump())
|
||||
.column_value("directory", directory ? 1 : 0)
|
||||
.column_value("pinned", pinned ? 1 : 0)
|
||||
.column_value("size", static_cast<std::int64_t>(size))
|
||||
.column_value("source_path", source_path)
|
||||
.go();
|
||||
if (not result.ok()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user