Skip to content

Commit

Permalink
rename serializer -> deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Apr 19, 2024
1 parent a952dd6 commit 01c1d2a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions silkworm/db/snapshots/body_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ namespace silkworm::snapshots {

void decode_word_into_body(ByteView word, BlockBodyForStorage& body);

struct BodySnapshotWordSerializer : public SnapshotWordSerializer {
struct BodySnapshotWordDeserializer : public SnapshotWordDeserializer {
BlockBodyForStorage value;

~BodySnapshotWordSerializer() override = default;
~BodySnapshotWordDeserializer() override = default;

void decode_word(ByteView word) override {
decode_word_into_body(word, value);
}
};

struct BodySnapshotReader : public SnapshotReader<BodySnapshotWordSerializer> {};
struct BodySnapshotReader : public SnapshotReader<BodySnapshotWordDeserializer> {};

} // namespace silkworm::snapshots
6 changes: 3 additions & 3 deletions silkworm/db/snapshots/header_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ namespace silkworm::snapshots {
void decode_word_into_header(ByteView word, BlockHeader& header);
void check_sanity_of_header_with_metadata(const BlockHeader& header, BlockNum block_from, BlockNum block_to);

struct HeaderSnapshotWordSerializer : public SnapshotWordSerializer {
struct HeaderSnapshotWordDeserializer : public SnapshotWordDeserializer {
BlockHeader value;

~HeaderSnapshotWordSerializer() override = default;
~HeaderSnapshotWordDeserializer() override = default;

void decode_word(ByteView word) override {
decode_word_into_header(word, value);
Expand All @@ -41,6 +41,6 @@ struct HeaderSnapshotWordSerializer : public SnapshotWordSerializer {
}
};

struct HeaderSnapshotReader : public SnapshotReader<HeaderSnapshotWordSerializer> {};
struct HeaderSnapshotReader : public SnapshotReader<HeaderSnapshotWordDeserializer> {};

} // namespace silkworm::snapshots
26 changes: 13 additions & 13 deletions silkworm/db/snapshots/snapshot_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ Snapshot::Iterator& Snapshot::Iterator::operator++() {
++it_;

if (has_next) {
serializer_->decode_word(*it_);
serializer_->check_sanity_with_metadata(path_.block_from(), path_.block_to());
deserializer_->decode_word(*it_);
deserializer_->check_sanity_with_metadata(path_.block_from(), path_.block_to());
} else {
serializer_.reset();
deserializer_.reset();
}
return *this;
}

bool operator==(const Snapshot::Iterator& lhs, const Snapshot::Iterator& rhs) {
return (lhs.serializer_ == rhs.serializer_) &&
(!lhs.serializer_ || (lhs.it_ == rhs.it_));
return (lhs.deserializer_ == rhs.deserializer_) &&
(!lhs.deserializer_ || (lhs.it_ == rhs.it_));
}

Snapshot::Iterator Snapshot::begin(std::shared_ptr<SnapshotWordSerializer> serializer) const {
Snapshot::Iterator Snapshot::begin(std::shared_ptr<SnapshotWordDeserializer> deserializer) const {
auto it = decoder_.begin();
if (it == decoder_.end()) {
return end();
}
serializer->decode_word(*it);
serializer->check_sanity_with_metadata(path_.block_from(), path_.block_to());
return Snapshot::Iterator{std::move(it), std::move(serializer), path()};
deserializer->decode_word(*it);
deserializer->check_sanity_with_metadata(path_.block_from(), path_.block_to());
return Snapshot::Iterator{std::move(it), std::move(deserializer), path()};
}

Snapshot::Iterator Snapshot::end() const {
Expand All @@ -82,18 +82,18 @@ seg::Decompressor::Iterator Snapshot::seek_decoder(uint64_t offset, std::optiona
return decoder_.seek(offset, hash_prefix ? ByteView{hash_prefix->bytes, 1} : ByteView{});
}

Snapshot::Iterator Snapshot::seek(uint64_t offset, std::optional<Hash> hash_prefix, std::shared_ptr<SnapshotWordSerializer> serializer) const {
Snapshot::Iterator Snapshot::seek(uint64_t offset, std::optional<Hash> hash_prefix, std::shared_ptr<SnapshotWordDeserializer> deserializer) const {
auto it = seek_decoder(offset, hash_prefix);
if (it == decoder_.end()) {
return end();
}
try {
serializer->decode_word(*it);
deserializer->decode_word(*it);
} catch (...) {
return end();
}
serializer->check_sanity_with_metadata(path_.block_from(), path_.block_to());
return Snapshot::Iterator{std::move(it), std::move(serializer), path()};
deserializer->check_sanity_with_metadata(path_.block_from(), path_.block_to());
return Snapshot::Iterator{std::move(it), std::move(deserializer), path()};
}

void Snapshot::close() {
Expand Down
30 changes: 15 additions & 15 deletions silkworm/db/snapshots/snapshot_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ class Snapshot {
public:
class Iterator {
public:
using value_type = std::shared_ptr<SnapshotWordSerializer>;
using value_type = std::shared_ptr<SnapshotWordDeserializer>;
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = const value_type*;
using reference = const value_type&;

Iterator(
seg::Decompressor::Iterator it,
std::shared_ptr<SnapshotWordSerializer> serializer,
std::shared_ptr<SnapshotWordDeserializer> deserializer,
SnapshotPath path)
: it_(std::move(it)), serializer_(std::move(serializer)), path_(std::move(path)) {}
: it_(std::move(it)), deserializer_(std::move(deserializer)), path_(std::move(path)) {}

reference operator*() const { return serializer_; }
pointer operator->() const { return &serializer_; }
reference operator*() const { return deserializer_; }
pointer operator->() const { return &deserializer_; }

Iterator operator++(int) { return std::exchange(*this, ++Iterator{*this}); }
Iterator& operator++();
Expand All @@ -68,7 +68,7 @@ class Snapshot {

private:
seg::Decompressor::Iterator it_;
std::shared_ptr<SnapshotWordSerializer> serializer_;
std::shared_ptr<SnapshotWordDeserializer> deserializer_;
SnapshotPath path_;
};

Expand Down Expand Up @@ -98,10 +98,10 @@ class Snapshot {
void reopen_segment();
void close();

Iterator begin(std::shared_ptr<SnapshotWordSerializer> serializer) const;
Iterator begin(std::shared_ptr<SnapshotWordDeserializer> deserializer) const;
Iterator end() const;

Iterator seek(uint64_t offset, std::optional<Hash> hash_prefix, std::shared_ptr<SnapshotWordSerializer> serializer) const;
Iterator seek(uint64_t offset, std::optional<Hash> hash_prefix, std::shared_ptr<SnapshotWordDeserializer> deserializer) const;

private:
seg::Decompressor::Iterator seek_decoder(uint64_t offset, std::optional<Hash> hash_prefix) const;
Expand All @@ -112,12 +112,12 @@ class Snapshot {
seg::Decompressor decoder_;
};

template <class TWordSerializer>
template <class TWordDeserializer>
class SnapshotReader {
public:
class Iterator {
public:
using value_type = decltype(TWordSerializer::value);
using value_type = decltype(TWordDeserializer::value);
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = const value_type*;
Expand All @@ -140,9 +140,9 @@ class SnapshotReader {

private:
value_type& value() const {
SnapshotWordSerializer& base_serializer = **it_;
// dynamic_cast is safe because TWordSerializer was used when creating the Iterator
auto& s = dynamic_cast<TWordSerializer&>(base_serializer);
SnapshotWordDeserializer& base_deserializer = **it_;
// dynamic_cast is safe because TWordDeserializer was used when creating the Iterator
auto& s = dynamic_cast<TWordDeserializer&>(base_deserializer);
return s.value;
}

Expand All @@ -154,15 +154,15 @@ class SnapshotReader {
SnapshotReader(const Snapshot& snapshot) : snapshot_(snapshot) {}

Iterator begin() const {
return Iterator{snapshot_.begin(std::make_shared<TWordSerializer>())};
return Iterator{snapshot_.begin(std::make_shared<TWordDeserializer>())};
}

Iterator end() const {
return Iterator{snapshot_.end()};
}

Iterator seek(uint64_t offset, std::optional<Hash> hash_prefix = std::nullopt) const {
return Iterator{snapshot_.seek(offset, hash_prefix, std::make_shared<TWordSerializer>())};
return Iterator{snapshot_.seek(offset, hash_prefix, std::make_shared<TWordDeserializer>())};
}

std::optional<typename Iterator::value_type> seek_one(uint64_t offset, std::optional<Hash> hash_prefix = std::nullopt) const {
Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/snapshots/snapshot_word_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

namespace silkworm::snapshots {

struct SnapshotWordSerializer {
virtual ~SnapshotWordSerializer() = default;
struct SnapshotWordDeserializer {
virtual ~SnapshotWordDeserializer() = default;
virtual void decode_word(ByteView word) = 0;
virtual void check_sanity_with_metadata(BlockNum /*block_from*/, BlockNum /*block_to*/) {}
};
Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/snapshots/txn_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

namespace silkworm::snapshots {

struct TransactionSnapshotReader : public SnapshotReader<TransactionSnapshotWordSerializer> {};
struct TransactionSnapshotReader : public SnapshotReader<TransactionSnapshotWordDeserializer> {};

template <class TBytes = ByteView>
struct TransactionSnapshotPayloadRlpReader : public SnapshotReader<TransactionSnapshotWordPayloadRlpSerializer<TBytes>> {};
struct TransactionSnapshotPayloadRlpReader : public SnapshotReader<TransactionSnapshotWordPayloadRlpDeserializer<TBytes>> {};

} // namespace silkworm::snapshots
8 changes: 4 additions & 4 deletions silkworm/db/snapshots/txn_snapshot_word_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ Hash tx_buffer_hash(ByteView tx_buffer, uint64_t tx_id);
//! Decode transaction from snapshot word. Format is: tx_hash_1byte + sender_address_20byte + tx_rlp_bytes
void decode_word_into_tx(ByteView word, Transaction& tx);

struct TransactionSnapshotWordSerializer : public SnapshotWordSerializer {
struct TransactionSnapshotWordDeserializer : public SnapshotWordDeserializer {
Transaction value;

~TransactionSnapshotWordSerializer() override = default;
~TransactionSnapshotWordDeserializer() override = default;

void decode_word(ByteView word) override {
decode_word_into_tx(word, value);
}
};

template <class TBytes = ByteView>
struct TransactionSnapshotWordPayloadRlpSerializer : public SnapshotWordSerializer {
struct TransactionSnapshotWordPayloadRlpDeserializer : public SnapshotWordDeserializer {
TBytes value;

~TransactionSnapshotWordPayloadRlpSerializer() override = default;
~TransactionSnapshotWordPayloadRlpDeserializer() override = default;

void decode_word(ByteView word) override {
auto data = slice_tx_data(word);
Expand Down

0 comments on commit 01c1d2a

Please sign in to comment.