Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor storage interface #1435

Merged
merged 20 commits into from
Dec 6, 2022
74 changes: 74 additions & 0 deletions core/common/buffer_or_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_COMMON_BUFFER_OR_VIEW_HPP
#define KAGOME_COMMON_BUFFER_OR_VIEW_HPP

#include <variant>

#include "common/buffer.hpp"

namespace kagome::common {
class BufferOrView {
turuslan marked this conversation as resolved.
Show resolved Hide resolved
public:
BufferOrView() = default;

BufferOrView(const BufferView &view) : variant{view} {}

BufferOrView(const std::vector<uint8_t> &vector) = delete;
BufferOrView(std::vector<uint8_t> &&vector)
: variant{Buffer{std::move(vector)}} {}

BufferOrView(const BufferOrView &) = delete;
BufferOrView(BufferOrView &&) = default;

BufferOrView &operator=(const BufferOrView &) = delete;
BufferOrView &operator=(BufferOrView &&) = default;

bool owned() const {
return variant.index() == 1;
}

BufferView view() const {
if (!owned()) {
return std::get<BufferView>(variant);
}
return BufferView{std::get<Buffer>(variant)};
}

operator BufferView() const {
return view();
}

size_t size() const {
return view().size();
}

// get mutable buffer reference, copy once if view
Buffer &mut() {
if (!owned()) {
auto view = std::get<BufferView>(variant);
variant.emplace<Buffer>(view);
}
return std::get<Buffer>(variant);
}

// move buffer away, copy once if view
Buffer into() {
auto buffer = std::move(mut());
variant.emplace<BufferView>();
return buffer;
}

private:
std::variant<BufferView, Buffer> variant;
xDimon marked this conversation as resolved.
Show resolved Hide resolved
};
} // namespace kagome::common

template <>
struct fmt::formatter<kagome::common::BufferOrView>
: fmt::formatter<kagome::common::BufferView> {};

#endif // KAGOME_COMMON_BUFFER_OR_VIEW_HPP
9 changes: 9 additions & 0 deletions core/storage/buffer_map_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@
*/

#include "common/buffer.hpp"
#include "common/buffer_or_view.hpp"
#include "storage/face/batch_writeable.hpp"
#include "storage/face/generic_maps.hpp"
#include "storage/face/write_batch.hpp"

namespace kagome::storage::face {
template <>
struct OwnedOrView<common::Buffer> {
using type = common::BufferOrView;
};
} // namespace kagome::storage::face

namespace kagome::storage {

using Buffer = common::SLBuffer<std::numeric_limits<size_t>::max()>;
using BufferView = common::BufferView;
using BufferConstRef = common::BufferConstRef;
using common::BufferOrView;

using BufferBatch = face::WriteBatch<BufferView, Buffer>;

Expand Down
17 changes: 17 additions & 0 deletions core/storage/face/owned_or_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP
#define KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP

namespace kagome::storage::face {
template <typename T>
struct OwnedOrView;

template <typename T>
using OwnedOrViewOf = typename OwnedOrView<T>::type;
} // namespace kagome::storage::face

#endif // KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP