diff --git a/repertory/librepertory/src/providers/meta_db.cpp b/repertory/librepertory/src/providers/meta_db.cpp index b2aa1489..0458cfcb 100644 --- a/repertory/librepertory/src/providers/meta_db.cpp +++ b/repertory/librepertory/src/providers/meta_db.cpp @@ -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() == 1); meta[META_PINNED] = utils::string::from_bool( row->get_column("pinned").get_value() == 1); + meta[META_SIZE] = std::to_string(static_cast( + row->get_column("size").get_value())); meta[META_SOURCE] = row->get_column("source_path").get_value(); - 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() == 1) + : key == META_SIZE + ? std::to_string(static_cast( + row->get_column("size").get_value())) : json::parse( row->get_column("data").get_value())[key] .get(); @@ -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 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 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 row; + if (result.get_row(row) && row.has_value()) { + return static_cast( + row->get_column("total_size").get_value()); } } 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(size)) .column_value("source_path", source_path) .go(); if (not result.ok()) {