Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion rocks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(SetupRocksDB)
userver_module(
rocks
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
LINK_LIBRARIES RocksDB::rocksdb
LINK_LIBRARIES_PRIVATE RocksDB::rocksdb
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
DEPENDS core
)
72 changes: 0 additions & 72 deletions rocks/include/userver/storages/rocks/client.hpp

This file was deleted.

15 changes: 15 additions & 0 deletions rocks/include/userver/storages/rocks/column_family.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace rocksdb {
class ColumnFamilyHandle;
} // namespace rocksdb

USERVER_NAMESPACE_BEGIN

namespace storages::rocks {

using ColumnFamilyHandle = rocksdb::ColumnFamilyHandle*;

} // namespace storages::rocks

USERVER_NAMESPACE_END
45 changes: 20 additions & 25 deletions rocks/include/userver/storages/rocks/component.hpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
#pragma once

/// @file userver/storages/rocks/component.hpp
/// @brief @copybrief rocks::Rocks
/// @brief @copybrief components::Rocks

#include <string_view>
#include <userver/storages/rocks/db_fwd.hpp>
#include <userver/components/component_base.hpp>
#include <userver/components/component_config.hpp>
#include <userver/components/component_context.hpp>
#include <userver/engine/task/task_processor_fwd.hpp>
#include <userver/storages/rocks/client_fwd.hpp>

USERVER_NAMESPACE_BEGIN

namespace storages::rocks {
namespace components {

// clang-format off

/// @ingroup userver_components
///
/// @brief RocksDB client component.
/// ## Static options:
/// Name | Description | Default value
/// ---------------------------------- | ------------------------------------------------ | ---------------
/// task-processor | name of the task processor to run the blocking file operations | -
/// db-path | path to database file | -

// clang-format on

class Component : public components::ComponentBase {
/**
* @brief Component for configuring and managing RocksDB.
*/
class Rocks final : public components::ComponentBase {
public:
Component(const components::ComponentConfig&, const components::ComponentContext&);

~Component() = default;
static constexpr std::string_view kName = "rocks";
static yaml_config::Schema GetStaticConfigSchema();

storages::rocks::ClientPtr MakeClient();
/**
* @brief Constructor of the Rocks class.
*/
Rocks(const components::ComponentConfig&, const components::ComponentContext&);

static yaml_config::Schema GetStaticConfigSchema();
/**
* @brief Return a pointer to the database instance.
*/
[[nodiscard]] const storages::rocks::DbPtr& GetDb() const;

private:
storages::rocks::ClientPtr client_ptr_;
storages::rocks::DbPtr db_ptr_;
};

} // namespace storages::rocks
} // namespace components

USERVER_NAMESPACE_END
59 changes: 59 additions & 0 deletions rocks/include/userver/storages/rocks/db.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

/// @file userver/storages/rocks/db.hpp
/// @brief @copybrief storages::rocks::Db

#include <memory>
#include <string>
#include <optional>
#include <string_view>
#include <userver/storages/rocks/snapshot.hpp>
#include <userver/storages/rocks/column_family.hpp>
#include <userver/engine/task/task_processor_fwd.hpp>

USERVER_NAMESPACE_BEGIN

namespace storages::rocks::detail {
class DbImpl;
} // namespace storages::rocks::detail

namespace storages::rocks {

class WriteBatch;

struct DbOptions final {
std::optional<std::string_view> compression;
std::optional<int> compression_level;
std::optional<std::string_view> bottommost_compression;
std::optional<int> bottommost_compression_level;
std::optional<bool> use_direct_reads;
std::optional<bool> use_direct_io_for_flush_and_compaction;
};

class Db final {
public:
Db(const std::string& db_path, int max_background_jobs, const std::vector<std::string>& column_families,
const DbOptions& db_options, engine::TaskProcessor& task_processor);

[[nodiscard]] ColumnFamilyHandle GetColumnFamily(const std::string& name) const;

void Put(std::string_view key, std::string_view value);
void Put(ColumnFamilyHandle column_family, std::string_view key, std::string_view value);

void Delete(std::string_view key);
void Delete(ColumnFamilyHandle column_family, std::string_view key);

void Write(WriteBatch& write_batch);

[[nodiscard]] std::optional<std::string> Get(std::string_view key) const;
[[nodiscard]] std::optional<std::string> Get(ColumnFamilyHandle column_family, std::string_view key) const;

[[nodiscard]] Snapshot GetSnapshot() const;

private:
std::shared_ptr<detail::DbImpl> db_impl_;
};

} // namespace storages::rocks

USERVER_NAMESPACE_END
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
USERVER_NAMESPACE_BEGIN

namespace storages::rocks {

class Client;
using ClientPtr = std::shared_ptr<Client>;

class Db;
using DbPtr = std::shared_ptr<Db>;
} // namespace storages::rocks

USERVER_NAMESPACE_END
67 changes: 67 additions & 0 deletions rocks/include/userver/storages/rocks/detail/db_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

/// @file userver/storages/rocks/detail/db_impl.hpp
/// @brief @copybrief storages::rocks::detail::DbImpl

#include <memory>
#include <string>
#include <optional>
#include <string_view>
#include <unordered_map>
#include <userver/storages/rocks/column_family.hpp>
#include <userver/engine/task/task_processor_fwd.hpp>

namespace rocksdb {
class Options;
class WriteOptions;
class WriteBatch;
class ReadOptions;
class Iterator;
class Snapshot;
class DB;
class ColumnFamilyHandle;
} // namespace rocksdb

USERVER_NAMESPACE_BEGIN

namespace storages::rocks::detail {

class DbImpl final {
public:
DbImpl(const rocksdb::Options& options, const std::string& db, const std::vector<std::string>& column_families,
engine::TaskProcessor& task_processor);
~DbImpl();

[[nodiscard]] rocksdb::ColumnFamilyHandle* GetColumnFamily(const std::string& name) const;

void Put(const rocksdb::WriteOptions& options, std::string_view key, std::string_view value);
void Put(const rocksdb::WriteOptions& options, rocksdb::ColumnFamilyHandle* column_family, std::string_view key,
std::string_view value);

void Delete(const rocksdb::WriteOptions& options, std::string_view key);
void Delete(const rocksdb::WriteOptions& options, rocksdb::ColumnFamilyHandle* column_family, std::string_view key);

void Write(const rocksdb::WriteOptions& options, rocksdb::WriteBatch& write_batch);

[[nodiscard]] std::optional<std::string> Get(const rocksdb::ReadOptions& options, std::string_view key) const;
[[nodiscard]] std::optional<std::string> Get(const rocksdb::ReadOptions& options,
rocksdb::ColumnFamilyHandle* column_family, std::string_view key) const;

[[nodiscard]] std::unique_ptr<rocksdb::Iterator> NewIterator(const rocksdb::ReadOptions& options) const;
[[nodiscard]] std::unique_ptr<rocksdb::Iterator> NewIterator(const rocksdb::ReadOptions& options,
rocksdb::ColumnFamilyHandle* column_family) const;

[[nodiscard]] const rocksdb::Snapshot* GetSnapshot() const;
void ReleaseSnapshot(const rocksdb::Snapshot* snapshot) const;

[[nodiscard]] engine::TaskProcessor& GetTaskProcessor();

private:
std::unique_ptr<rocksdb::DB> db_;
engine::TaskProcessor& task_processor_;
std::unordered_map<std::string, rocksdb::ColumnFamilyHandle*> column_family_handles_;
};

} // namespace storages::rocks::detail

USERVER_NAMESPACE_END
49 changes: 49 additions & 0 deletions rocks/include/userver/storages/rocks/detail/iterator_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

/// @file userver/storages/rocks/detail/iterator_impl.hpp
/// @brief @copybrief storages::rocks::detail::IteratorImpl

#include <memory>
#include <string>
#include <string_view>

namespace rocksdb {
class Iterator;
class Snapshot;
} // namespace rocksdb

USERVER_NAMESPACE_BEGIN

namespace storages::rocks::detail {

class DbImpl;

class IteratorImpl {
public:
IteratorImpl(const std::shared_ptr<DbImpl>& db, const std::shared_ptr<const rocksdb::Snapshot>& snapshot,
std::unique_ptr<rocksdb::Iterator> iterator) noexcept;
virtual ~IteratorImpl();

IteratorImpl(IteratorImpl&&) noexcept;
IteratorImpl& operator=(IteratorImpl&&) noexcept;

[[nodiscard]] bool Valid() const noexcept;
void SeekToFirst();
void SeekToLast();
void Seek(std::string_view slice);
void SeekForPrev(std::string_view slice);
void Next();
void Prev();

[[nodiscard]] std::string Key() const;
[[nodiscard]] std::string Value() const;

private:
std::shared_ptr<DbImpl> db_impl_;
std::shared_ptr<const rocksdb::Snapshot> snapshot_;
std::unique_ptr<rocksdb::Iterator> iterator_;
};

} // namespace storages::rocks::detail

USERVER_NAMESPACE_END
44 changes: 44 additions & 0 deletions rocks/include/userver/storages/rocks/detail/snapshot_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

/// @file userver/storages/rocks/detail/snapshot_impl.hpp
/// @brief @copybrief storages::rocks::detail::SnapshotImpl

#include <memory>
#include <string>
#include <optional>
#include <string_view>
#include <userver/storages/rocks/column_family.hpp>
#include <userver/storages/rocks/detail/iterator_impl.hpp>

namespace rocksdb {
class Snapshot;
class ReadOptions;
class ColumnFamilyHandle;
} // namespace rocksdb

USERVER_NAMESPACE_BEGIN

namespace storages::rocks::detail {

class DbImpl;

class SnapshotImpl final {
public:
SnapshotImpl(const std::shared_ptr<DbImpl>& db, const rocksdb::Snapshot* snapshot);

[[nodiscard]] std::optional<std::string> Get(rocksdb::ReadOptions& options, std::string_view key) const;
[[nodiscard]] std::optional<std::string> Get(rocksdb::ReadOptions& options,
rocksdb::ColumnFamilyHandle* column_family, std::string_view key) const;

[[nodiscard]] IteratorImpl NewIterator(rocksdb::ReadOptions& options) const;
[[nodiscard]] IteratorImpl NewIterator(rocksdb::ReadOptions& options,
rocksdb::ColumnFamilyHandle* column_family) const;

private:
std::shared_ptr<detail::DbImpl> db_impl_;
std::shared_ptr<const rocksdb::Snapshot> snapshot_;
};

} // namespace storages::rocks::detail

USERVER_NAMESPACE_END
Loading