RocksDB implementations should be transactional #24
This commit is contained in:
parent
7d5d52afe3
commit
48ed06a255
@ -64,8 +64,16 @@ private:
|
|||||||
std::function<rocksdb::Status(rocksdb::Transaction *txn)> action)
|
std::function<rocksdb::Status(rocksdb::Transaction *txn)> action)
|
||||||
-> api_error;
|
-> api_error;
|
||||||
|
|
||||||
|
[[nodiscard]] auto remove_api_path(const std::string &api_path,
|
||||||
|
const std::string &source_path,
|
||||||
|
rocksdb::Transaction *txn)
|
||||||
|
-> rocksdb::Status;
|
||||||
|
|
||||||
[[nodiscard]] auto update_item_meta(const std::string &api_path,
|
[[nodiscard]] auto update_item_meta(const std::string &api_path,
|
||||||
json json_data) -> api_error;
|
json json_data,
|
||||||
|
rocksdb::Transaction *base_txn = nullptr,
|
||||||
|
rocksdb::Status *status = nullptr)
|
||||||
|
-> api_error;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void clear() override;
|
void clear() override;
|
||||||
|
@ -322,6 +322,18 @@ void rdb_meta_db::remove_api_path(const std::string &api_path) {
|
|||||||
res = perform_action(function_name,
|
res = perform_action(function_name,
|
||||||
[this, &api_path, &source_path](
|
[this, &api_path, &source_path](
|
||||||
rocksdb::Transaction *txn) -> rocksdb::Status {
|
rocksdb::Transaction *txn) -> rocksdb::Status {
|
||||||
|
return remove_api_path(api_path, source_path, txn);
|
||||||
|
});
|
||||||
|
if (res != api_error::success) {
|
||||||
|
utils::error::raise_api_path_error(function_name, api_path, res,
|
||||||
|
"failed to remove api path");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto rdb_meta_db::remove_api_path(const std::string &api_path,
|
||||||
|
const std::string &source_path,
|
||||||
|
rocksdb::Transaction *txn)
|
||||||
|
-> rocksdb::Status {
|
||||||
auto txn_res = txn->Delete(pinned_family_, api_path);
|
auto txn_res = txn->Delete(pinned_family_, api_path);
|
||||||
if (not txn_res.ok()) {
|
if (not txn_res.ok()) {
|
||||||
return txn_res;
|
return txn_res;
|
||||||
@ -340,11 +352,6 @@ void rdb_meta_db::remove_api_path(const std::string &api_path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return txn->Delete(default_family_, api_path);
|
return txn->Delete(default_family_, api_path);
|
||||||
});
|
|
||||||
if (res != api_error::success) {
|
|
||||||
utils::error::raise_api_path_error(function_name, api_path, res,
|
|
||||||
"failed to remove api path");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rdb_meta_db::remove_item_meta(const std::string &api_path,
|
auto rdb_meta_db::remove_item_meta(const std::string &api_path,
|
||||||
@ -368,14 +375,27 @@ auto rdb_meta_db::remove_item_meta(const std::string &api_path,
|
|||||||
auto rdb_meta_db::rename_item_meta(const std::string &from_api_path,
|
auto rdb_meta_db::rename_item_meta(const std::string &from_api_path,
|
||||||
const std::string &to_api_path)
|
const std::string &to_api_path)
|
||||||
-> api_error {
|
-> api_error {
|
||||||
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
json json_data;
|
json json_data;
|
||||||
auto res = get_item_meta_json(from_api_path, json_data);
|
auto res = get_item_meta_json(from_api_path, json_data);
|
||||||
if (res != api_error::success) {
|
if (res != api_error::success) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_api_path(from_api_path);
|
return perform_action(
|
||||||
return update_item_meta(to_api_path, json_data);
|
function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status {
|
||||||
|
auto txn_res = remove_api_path(
|
||||||
|
from_api_path, json_data[META_SOURCE].get<std::string>(), txn);
|
||||||
|
if (not txn_res.ok()) {
|
||||||
|
return txn_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
rocksdb::Status status;
|
||||||
|
[[maybe_unused]] auto api_res =
|
||||||
|
update_item_meta(to_api_path, json_data, txn, &status);
|
||||||
|
return status;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rdb_meta_db::set_item_meta(const std::string &api_path,
|
auto rdb_meta_db::set_item_meta(const std::string &api_path,
|
||||||
@ -423,8 +443,9 @@ auto rdb_meta_db::set_item_meta(const std::string &api_path,
|
|||||||
return update_item_meta(api_path, json_data);
|
return update_item_meta(api_path, json_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data)
|
auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data,
|
||||||
-> api_error {
|
rocksdb::Transaction *base_txn,
|
||||||
|
rocksdb::Status *status) -> api_error {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -469,35 +490,69 @@ auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data)
|
|||||||
json_data.erase(META_PINNED);
|
json_data.erase(META_PINNED);
|
||||||
json_data.erase(META_SIZE);
|
json_data.erase(META_SIZE);
|
||||||
|
|
||||||
return perform_action(
|
const auto do_transaction =
|
||||||
function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status {
|
[&](rocksdb::Transaction *txn) -> rocksdb::Status {
|
||||||
if (should_del_source) {
|
if (should_del_source) {
|
||||||
auto res = txn->Delete(source_family_, orig_source_path);
|
auto res = txn->Delete(source_family_, orig_source_path);
|
||||||
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
if (not res.ok()) {
|
if (not res.ok()) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto res = txn->Put(pinned_family_, api_path,
|
auto res =
|
||||||
utils::string::from_bool(pinned));
|
txn->Put(pinned_family_, api_path, utils::string::from_bool(pinned));
|
||||||
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
if (not res.ok()) {
|
if (not res.ok()) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = txn->Put(size_family_, api_path, std::to_string(size));
|
res = txn->Put(size_family_, api_path, std::to_string(size));
|
||||||
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
if (not res.ok()) {
|
if (not res.ok()) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (not source_path.empty()) {
|
if (not source_path.empty()) {
|
||||||
res = txn->Put(source_family_, source_path, api_path);
|
res = txn->Put(source_family_, source_path, api_path);
|
||||||
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
if (not res.ok()) {
|
if (not res.ok()) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return txn->Put(default_family_, api_path, json_data.dump());
|
res = txn->Put(default_family_, api_path, json_data.dump());
|
||||||
});
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (base_txn == nullptr) {
|
||||||
|
return perform_action(function_name, do_transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto res = do_transaction(base_txn);
|
||||||
|
if (status != nullptr) {
|
||||||
|
*status = res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.ok()) {
|
||||||
|
return api_error::success;
|
||||||
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
utils::error::raise_api_path_error(function_name, api_path, e,
|
utils::error::raise_api_path_error(function_name, api_path, e,
|
||||||
"failed to update item meta");
|
"failed to update item meta");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user