Kagome
Polkadot Runtime Engine in C++17
offchain_local_storage.cpp
Go to the documentation of this file.
1 
7 
10 
11 namespace kagome::offchain {
12 
13  namespace {
14  common::Buffer internalKey(const common::BufferView &key) {
15  const auto &prefix = storage::kOffchainWorkerStoragePrefix;
16  return common::Buffer()
17  .reserve(prefix.size() + key.size())
18  .put(prefix)
19  .put(key);
20  }
21  } // namespace
22 
24  std::shared_ptr<storage::BufferStorage> storage)
25  : storage_(std::move(storage)),
26  log_(log::createLogger("OffchainLocalStorage", "offchain")) {
27  BOOST_ASSERT(storage_);
28  }
29 
30  outcome::result<void> OffchainLocalStorageImpl::set(
31  const common::BufferView &key, common::Buffer value) {
32  // TODO(xDimon):
33  // Need to implemented as soon as it will implemented in Substrate.
34  // Specification in not enough to implement it now.
35  // issue: https://github.com/soramitsu/kagome/issues/997
36  throw std::invalid_argument("Off-chain local storage is unavailable yet");
37 
38  auto iKey = internalKey(key);
39  std::lock_guard lg(mutex_);
40  return storage_->put(iKey, std::move(value));
41  }
42 
43  outcome::result<void> OffchainLocalStorageImpl::clear(
44  const common::BufferView &key) {
45  // TODO(xDimon):
46  // Need to implemented as soon as it will implemented in Substrate.
47  // Specification in not enough to implement it now.
48  // issue: https://github.com/soramitsu/kagome/issues/997
49  throw std::invalid_argument("Off-chain local storage is unavailable yet");
50 
51  std::lock_guard lg(mutex_);
52  return storage_->remove(key);
53  }
54 
56  const common::BufferView &key,
57  const std::optional<common::BufferView> &expected,
58  common::Buffer value) {
59  // TODO(xDimon):
60  // Need to implemented as soon as it will implemented in Substrate.
61  // Specification in not enough to implement it now.
62  // issue: https://github.com/soramitsu/kagome/issues/997
63  throw std::invalid_argument("Off-chain local storage is unavailable yet");
64 
65  auto iKey = internalKey(key);
66  std::lock_guard lg(mutex_);
67  OUTCOME_TRY(get_opt, storage_->tryLoad(iKey));
68 
69  std::optional<common::BufferView> existing;
70  if (get_opt.has_value()) {
71  existing = get_opt.value();
72  }
73 
74  if (existing == expected) {
75  OUTCOME_TRY(storage_->put(iKey, std::move(value)));
76  return true;
77  }
78  return false;
79  }
80 
81  outcome::result<common::Buffer> OffchainLocalStorageImpl::get(
82  const common::BufferView &key) {
83  // TODO(xDimon):
84  // Need to implemented as soon as it will implemented in Substrate.
85  // Specification in not enough to implement it now.
86  // issue: https://github.com/soramitsu/kagome/issues/997
87  throw std::invalid_argument("Off-chain local storage is unavailable yet");
88 
89  auto iKey = internalKey(key);
90  return storage_->load(iKey);
91  }
92 } // namespace kagome::offchain
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< common::Buffer > get(const common::BufferView &key) override
Gets a value from the local storage.
common::BufferView BufferView
STL namespace.
const common::Buffer kOffchainWorkerStoragePrefix
std::shared_ptr< storage::BufferStorage > storage_
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
OffchainLocalStorageImpl(std::shared_ptr< storage::BufferStorage > storage)
outcome::result< void > clear(const common::BufferView &key) override
Remove a value from the local storage.
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
outcome::result< void > set(const common::BufferView &key, common::Buffer value) override
Sets a value in the storage.
outcome::result< bool > compare_and_set(const common::BufferView &key, const std::optional< common::BufferView > &expected, common::Buffer value) override
Sets a new value in the local storage if the condition matches the current value. ...