Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rocks/include/userver/storages/rocks/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ class Client final {
*/
void CheckStatus(rocksdb::Status status, std::string_view method_name);

Client MakeSnapshot(const std::string& checkpoint_path);

private:
std::unique_ptr<rocksdb::DB> db_;
engine::TaskProcessor& blocking_task_processor_;
};

} // namespace storages::rocks

USERVER_NAMESPACE_END
22 changes: 22 additions & 0 deletions rocks/src/storages/rocks/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <fmt/format.h>

#include <rocksdb/utilities/checkpoint.h>

#include <userver/storages/rocks/exception.hpp>
#include <userver/utils/async.hpp>

Expand Down Expand Up @@ -56,6 +58,26 @@ void Client::CheckStatus(rocksdb::Status status, std::string_view method_name) {
method_name, status.ToString());
}
}

Client Client::MakeSnapshot(const std::string& checkpoint_path) {
rocksdb::Checkpoint* checkpoint;
rocksdb::Status status = rocksdb::Checkpoint::Create(db_.get(), &checkpoint);

CheckStatus(status, "Create Checkpoint");

try {
status = checkpoint->CreateCheckpoint(checkpoint_path);

CheckStatus(status, "Bind Checkpoint to the path");

delete checkpoint;
} catch (...) {
delete checkpoint;
}

return Client(checkpoint_path, blocking_task_processor_);
}

} // namespace storages::rocks

USERVER_NAMESPACE_END
33 changes: 33 additions & 0 deletions rocks/src/storages/rocks/client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@ UTEST(Rocks, CheckCRUD) {
client.Delete(key);
res = client.Get(key);
EXPECT_EQ("", res);

}

UTEST(RocksSnapshot, CheckCRUD) {
storages::rocks::Client client{"/tmp/rocksdb_simple_snapshot",
engine::current_task::GetTaskProcessor()};
std::string key = "key";
std::string value = "value";

client.Put(key, value);
std::string res = client.Get(key);
EXPECT_EQ(value, res);

storages::rocks::Client snapshot(client.MakeSnapshot("/tmp/snapshot"));
res = snapshot.Get(key);
EXPECT_EQ(value, res);


std::string new_value = "new value";
snapshot.Put(key, new_value);
res = snapshot.Get(key);
EXPECT_EQ(new_value, res);

res = client.Get(key);
EXPECT_EQ(value, res);


snapshot.Delete(key);
res = snapshot.Get(key);
EXPECT_EQ("", res);

res = client.Get(key);
EXPECT_EQ(value, res);
}

} // namespace
Expand Down