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

datastore: fix debug_getModifiedAccountsBy regression #2771

Merged
merged 1 commit into from
Mar 6, 2025
Merged
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 silkworm/db/datastore/common/ranges/caching_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CachingView : public std::ranges::view_interface<CachingView<TRange>> {

reference operator*() const {
if (!cached_value_) {
cached_value_ = *it_;
cached_value_.emplace(std::move(*it_));
}
return *cached_value_;
}
Expand Down
7 changes: 5 additions & 2 deletions silkworm/db/datastore/common/ranges/merge_many_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <silkworm/core/common/assert.hpp>

#include "merge_compare_func.hpp"
#include "vector_from_range.hpp"

namespace silkworm::views {

Expand Down Expand Up @@ -57,9 +58,10 @@ class MergeManyView : public std::ranges::view_interface<MergeManyView<Range, Ra
Iterator(
Ranges& ranges,
const Comp* comp, Proj proj)
: comp_{comp},
: ranges_{vector_from_range(ranges)},
comp_{comp},
proj_{std::move(proj)} {
for (auto&& range : ranges) {
for (Range& range : ranges_) {
iterators_.emplace_back(std::ranges::begin(range));
sentinels_.emplace_back(std::ranges::end(range));
}
Expand Down Expand Up @@ -154,6 +156,7 @@ class MergeManyView : public std::ranges::view_interface<MergeManyView<Range, Ra
};
}

std::vector<Range> ranges_;
std::vector<RangeIterator> iterators_;
std::vector<RangeSentinel> sentinels_;
const Comp* comp_{nullptr};
Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/datastore/common/ranges/vector_from_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace silkworm {

template <std::ranges::input_range Range, typename Value = std::iter_value_t<std::ranges::iterator_t<Range>>>
std::vector<Value> vector_from_range(Range range) {
std::vector<Value> vector_from_range(Range&& range) {
std::vector<Value> results;
for (auto&& value : range) {
results.emplace_back(std::move(value));
Expand All @@ -33,7 +33,7 @@ std::vector<Value> vector_from_range(Range range) {
}

template <std::ranges::input_range Range, typename Value = std::iter_value_t<std::ranges::iterator_t<Range>>>
std::vector<Value> vector_from_range_copy(Range range) {
std::vector<Value> vector_from_range_copy(Range&& range) {
std::vector<Value> results;
std::ranges::copy(range, std::back_inserter(results));
return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct HistoryRangeInPeriodSegmentQuery {

auto ii_reader = entity_.inverted_index.kv_segment_reader<RawDecoder<Bytes>>();

return ii_reader |
return silkworm::ranges::owning_view(std::move(ii_reader)) |
std::views::transform(std::move(lookup_kv_pair_func)) |
silkworm::views::caching |
std::views::filter([](const std::optional<ResultItem>& result_opt) { return result_opt.has_value(); }) |
Expand Down
36 changes: 18 additions & 18 deletions silkworm/db/datastore/snapshots/segment/kv_segment_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,29 @@ class KVSegmentReader {
using KeyDecoderType = TKeyDecoder;
using ValueDecoderType = TValueDecoder;

explicit KVSegmentReader(const KVSegmentFileReader& reader) : reader_(reader) {}
explicit KVSegmentReader(const KVSegmentFileReader& reader) : reader_{&reader} {}

Iterator begin() const {
return Iterator{reader_.begin(std::make_shared<TKeyDecoder>(), std::make_shared<TValueDecoder>())};
return Iterator{reader_->begin(std::make_shared<TKeyDecoder>(), std::make_shared<TValueDecoder>())};
}

Iterator end() const {
return Iterator{reader_.end()};
return Iterator{reader_->end()};
}

Iterator seek(uint64_t offset) const {
return Iterator{reader_.seek(offset, std::nullopt, std::make_shared<TKeyDecoder>(), std::make_shared<TValueDecoder>())};
return Iterator{reader_->seek(offset, std::nullopt, std::make_shared<TKeyDecoder>(), std::make_shared<TValueDecoder>())};
}

std::optional<typename Iterator::value_type_owned> seek_one(uint64_t offset) const {
auto it = seek(offset);
return (it != end()) ? std::optional{it.move_value()} : std::nullopt;
}

const SnapshotPath& path() const { return reader_.path(); }
const SnapshotPath& path() const { return reader_->path(); }

private:
const KVSegmentFileReader& reader_;
const KVSegmentFileReader* reader_;
};

template <DecoderConcept TKeyDecoder>
Expand Down Expand Up @@ -248,29 +248,29 @@ class KVSegmentKeysReader {

using KeyDecoderType = TKeyDecoder;

explicit KVSegmentKeysReader(const KVSegmentFileReader& reader) : reader_(reader) {}
explicit KVSegmentKeysReader(const KVSegmentFileReader& reader) : reader_{&reader} {}

Iterator begin() const {
return Iterator{reader_.begin(std::make_shared<TKeyDecoder>(), {})};
return Iterator{reader_->begin(std::make_shared<TKeyDecoder>(), {})};
}

Iterator end() const {
return Iterator{reader_.end()};
return Iterator{reader_->end()};
}

Iterator seek(uint64_t offset) const {
return Iterator{reader_.seek(offset, std::nullopt, std::make_shared<TKeyDecoder>(), {})};
return Iterator{reader_->seek(offset, std::nullopt, std::make_shared<TKeyDecoder>(), {})};
}

std::optional<typename Iterator::value_type> seek_one(uint64_t offset) const {
auto it = seek(offset);
return (it != end()) ? std::optional{std::move(*it)} : std::nullopt;
}

const SnapshotPath& path() const { return reader_.path(); }
const SnapshotPath& path() const { return reader_->path(); }

private:
const KVSegmentFileReader& reader_;
const KVSegmentFileReader* reader_;
};

template <DecoderConcept TValueDecoder>
Expand Down Expand Up @@ -322,29 +322,29 @@ class KVSegmentValuesReader {

using ValueDecoderType = TValueDecoder;

explicit KVSegmentValuesReader(const KVSegmentFileReader& reader) : reader_(reader) {}
explicit KVSegmentValuesReader(const KVSegmentFileReader& reader) : reader_{&reader} {}

Iterator begin() const {
return Iterator{reader_.begin({}, std::make_shared<TValueDecoder>())};
return Iterator{reader_->begin({}, std::make_shared<TValueDecoder>())};
}

Iterator end() const {
return Iterator{reader_.end()};
return Iterator{reader_->end()};
}

Iterator seek(uint64_t offset) const {
return Iterator{reader_.seek(offset, std::nullopt, {}, std::make_shared<TValueDecoder>())};
return Iterator{reader_->seek(offset, std::nullopt, {}, std::make_shared<TValueDecoder>())};
}

std::optional<typename Iterator::value_type> seek_one(uint64_t offset) const {
auto it = seek(offset);
return (it != end()) ? std::optional{std::move(*it)} : std::nullopt;
}

const SnapshotPath& path() const { return reader_.path(); }
const SnapshotPath& path() const { return reader_->path(); }

private:
const KVSegmentFileReader& reader_;
const KVSegmentFileReader* reader_;
};

template <class TKVSegmentReader>
Expand Down
12 changes: 12 additions & 0 deletions silkworm/db/datastore/snapshots/segment/kv_segment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
limitations under the License.
*/

#include <concepts>
#include <ranges>

#include <catch2/catch_test_macros.hpp>

#include <silkworm/core/common/bytes_to_string.hpp>
Expand Down Expand Up @@ -45,6 +48,15 @@ struct CharCodec : public Codec {
}
};

static_assert(std::ranges::input_range<KVSegmentReader<StringCodec, CharCodec>>);
static_assert(std::movable<KVSegmentReader<StringCodec, CharCodec>>);

static_assert(std::ranges::input_range<KVSegmentKeysReader<StringCodec>>);
static_assert(std::movable<KVSegmentKeysReader<StringCodec>>);

static_assert(std::ranges::input_range<KVSegmentValuesReader<CharCodec>>);
static_assert(std::movable<KVSegmentValuesReader<CharCodec>>);

TEST_CASE("KVSegmentFile") {
using namespace datastore;
TemporaryDirectory tmp_dir;
Expand Down
12 changes: 6 additions & 6 deletions silkworm/db/datastore/snapshots/segment/segment_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,18 @@ class SegmentReader {

using DecoderType = TDecoder;

explicit SegmentReader(const SegmentFileReader& reader) : reader_(reader) {}
explicit SegmentReader(const SegmentFileReader& reader) : reader_{&reader} {}

Iterator begin() const {
return Iterator{reader_.begin(std::make_shared<TDecoder>())};
return Iterator{reader_->begin(std::make_shared<TDecoder>())};
}

Iterator end() const {
return Iterator{reader_.end()};
return Iterator{reader_->end()};
}

Iterator seek(uint64_t offset, std::optional<ByteView> check_prefix = std::nullopt) const {
return Iterator{reader_.seek(offset, check_prefix, std::make_shared<TDecoder>())};
return Iterator{reader_->seek(offset, check_prefix, std::make_shared<TDecoder>())};
}

std::optional<typename Iterator::value_type> seek_one(uint64_t offset, std::optional<ByteView> check_prefix = std::nullopt) const {
Expand All @@ -194,10 +194,10 @@ class SegmentReader {
return iterator_read_into_vector(std::move(it), count);
}

const SnapshotPath& path() const { return reader_.path(); }
const SnapshotPath& path() const { return reader_->path(); }

private:
const SegmentFileReader& reader_;
const SegmentFileReader* reader_;
};

template <class TSegmentReader>
Expand Down
6 changes: 6 additions & 0 deletions silkworm/db/datastore/snapshots/segment/segment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
limitations under the License.
*/

#include <concepts>
#include <ranges>

#include <catch2/catch_test_macros.hpp>

#include <silkworm/infra/common/directories.hpp>
Expand All @@ -24,6 +27,9 @@

namespace silkworm::snapshots::segment {

static_assert(std::ranges::input_range<SegmentReader<StringCodec>>);
static_assert(std::movable<SegmentReader<StringCodec>>);

TEST_CASE("SegmentFile") {
using namespace datastore;
TemporaryDirectory tmp_dir;
Expand Down
Loading