Skip to content

Commit

Permalink
DomainPutQuery, DomainGetLatestQuery, DomainDeleteQuery, LocalState u…
Browse files Browse the repository at this point in the history
…pdate
  • Loading branch information
battlmonstr committed Dec 25, 2024
1 parent 89996a6 commit 62946f2
Show file tree
Hide file tree
Showing 23 changed files with 755 additions and 44 deletions.
34 changes: 34 additions & 0 deletions silkworm/db/datastore/kvdb/big_endian_codec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2024 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "big_endian_codec.hpp"

#include <silkworm/core/common/endian.hpp>

namespace silkworm::datastore::kvdb {

Slice BigEndianU64Codec::encode() {
data.resize(sizeof(uint64_t), 0);
endian::store_big_u64(data.data(), value);
return to_slice(data);
}

void BigEndianU64Codec::decode(Slice slice) {
SILKWORM_ASSERT(slice.size() >= sizeof(uint64_t));
value = endian::load_big_u64(static_cast<uint8_t*>(slice.data()));
}

} // namespace silkworm::datastore::kvdb
35 changes: 35 additions & 0 deletions silkworm/db/datastore/kvdb/big_endian_codec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2024 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "codec.hpp"

namespace silkworm::datastore::kvdb {

struct BigEndianU64Codec : public Codec {
uint64_t value{0};
Bytes data;

~BigEndianU64Codec() override = default;
Slice encode() override;
void decode(Slice slice) override;
};

static_assert(EncoderConcept<BigEndianU64Codec>);
static_assert(DecoderConcept<BigEndianU64Codec>);

} // namespace silkworm::datastore::kvdb
47 changes: 47 additions & 0 deletions silkworm/db/datastore/kvdb/codec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2024 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "mdbx.hpp"

namespace silkworm::datastore::kvdb {

struct Encoder {
virtual ~Encoder() = default;
virtual Slice encode() = 0;
};

template <class TEncoder>
concept EncoderConcept =
std::derived_from<TEncoder, Encoder> &&
requires(TEncoder encoder) { encoder.value; };

struct Decoder {
virtual ~Decoder() = default;
virtual void decode(Slice slice) = 0;
};

template <class TDecoder>
concept DecoderConcept =
std::derived_from<TDecoder, Decoder> &&
requires(TDecoder decoder) { decoder.value; };

struct Codec : public Encoder, public Decoder {
~Codec() override = default;
};

} // namespace silkworm::datastore::kvdb
145 changes: 145 additions & 0 deletions silkworm/db/datastore/kvdb/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

#include <optional>

#include "../common/step.hpp"
#include "big_endian_codec.hpp"
#include "codec.hpp"
#include "history.hpp"
#include "kvts_codec.hpp"
#include "mdbx.hpp"

namespace silkworm::datastore::kvdb {
Expand All @@ -29,4 +33,145 @@ struct Domain {
std::optional<History> history;
};

struct InvertedStepCodec : public Codec {
Step value{0};
BigEndianU64Codec codec;
static constexpr size_t kEncodedSize = sizeof(decltype(BigEndianU64Codec::value));

~InvertedStepCodec() override = default;

Slice encode() override {
codec.value = ~value.value;
return codec.encode();
}

void decode(Slice slice) override {
codec.decode(slice);
value = Step(~codec.value);
}
};

static_assert(EncoderConcept<InvertedStepCodec>);
static_assert(DecoderConcept<InvertedStepCodec>);

template <EncoderConcept TEncoder>
using DomainKeyEncoder = KVTSKeyEncoder<TEncoder, InvertedStepCodec>;

template <EncoderConcept TEncoder>
using DomainValueEncoder = KVTSValueEncoder<TEncoder, InvertedStepCodec>;

template <DecoderConcept TDecoder>
using DomainKeyDecoder = KVTSKeyDecoder<TDecoder, InvertedStepCodec, InvertedStepCodec::kEncodedSize>;

template <DecoderConcept TDecoder>
using DomainValueDecoder = KVTSValueDecoder<TDecoder, InvertedStepCodec, InvertedStepCodec::kEncodedSize>;

template <EncoderConcept TKeyEncoder, EncoderConcept TValueEncoder>
struct DomainPutLatestQuery {
RWTxn& tx;
Domain entity;

using TKey = decltype(TKeyEncoder::value);
using TValue = decltype(TValueEncoder::value);

void exec(const TKey& key, const TValue& value, Step step) {
DomainKeyEncoder<TKeyEncoder> key_encoder{entity.has_large_values};
key_encoder.value.key.value = key;
key_encoder.value.timestamp.value = step;

DomainValueEncoder<TValueEncoder> value_encoder{entity.has_large_values};
value_encoder.value.value.value = value;
value_encoder.value.timestamp.value = step;

tx.rw_cursor(entity.values_table)->insert(key_encoder.encode(), value_encoder.encode());
}
};

template <EncoderConcept TKeyEncoder, DecoderConcept TValueDecoder>
struct DomainGetLatestQuery {
RWTxn& tx;
Domain entity;

using TKey = decltype(TKeyEncoder::value);
using TValue = decltype(TValueDecoder::value);

struct Result {
TValue value;
Step step;
};

std::optional<Result> exec(const TKey& key) {
DomainKeyEncoder<TKeyEncoder> key_encoder{/* has_large_values = */ false};
key_encoder.value.key.value = key;
Slice key_slice = key_encoder.encode();

auto result = tx.ro_cursor(entity.values_table)->lower_bound(key_slice, false);
if (!result) return std::nullopt;

DomainKeyDecoder<RawDecoder<ByteView>> key_decoder{entity.has_large_values};
key_decoder.decode(result.key);
if (key_decoder.value.key.value != from_slice(key_slice)) return std::nullopt;

DomainValueDecoder<RawDecoder<ByteView>> empty_value_decoder{entity.has_large_values};
empty_value_decoder.decode(result.value);
if (empty_value_decoder.value.value.value.empty()) return std::nullopt;

DomainValueDecoder<TValueDecoder> value_decoder{entity.has_large_values};
value_decoder.decode(result.value);

Step step = Step(key_decoder.value.timestamp.value.value | value_decoder.value.timestamp.value.value);

return Result{std::move(value_decoder.value.value.value), step};
}
};

template <EncoderConcept TKeyEncoder, EncoderConcept TValueEncoder>
struct DomainPutQuery {
RWTxn& tx;
Domain entity;

using TKey = decltype(TKeyEncoder::value);
using TValue = decltype(TValueEncoder::value);

void exec(
const TKey& key,
const TValue& value,
Timestamp timestamp,
const std::optional<TValue>& prev_value,
Step prev_step) {
DomainPutLatestQuery<TKeyEncoder, TValueEncoder> value_query{tx, entity};
value_query.exec(key, value, prev_step);

if (entity.history) {
if (prev_value) {
HistoryPutQuery<TKeyEncoder, TValueEncoder> history_query{tx, *entity.history};
history_query.exec(key, *prev_value, timestamp);
} else {
HistoryDeleteQuery<TKeyEncoder> history_query{tx, *entity.history};
history_query.exec(key, timestamp);
}
}
}
};

template <EncoderConcept TKeyEncoder, EncoderConcept TValueEncoder>
struct DomainDeleteQuery {
RWTxn& tx;
Domain entity;

using TKey = decltype(TKeyEncoder::value);
using TValue = decltype(TValueEncoder::value);

void exec(
const TKey& key,
Timestamp timestamp,
const std::optional<TValue>& prev_value,
Step prev_step) {
if (prev_value) {
DomainPutQuery<TKeyEncoder, RawEncoder<ByteView>> query{tx, entity};
query.exec(key, ByteView{}, timestamp, prev_value, prev_step);
}
}
};

} // namespace silkworm::datastore::kvdb
44 changes: 44 additions & 0 deletions silkworm/db/datastore/kvdb/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "inverted_index.hpp"
#include "kvts_codec.hpp"
#include "mdbx.hpp"

namespace silkworm::datastore::kvdb {
Expand All @@ -27,4 +28,47 @@ struct History {
InvertedIndex inverted_index;
};

template <EncoderConcept TEncoder>
using HistoryKeyEncoder = KVTSKeyEncoder<TEncoder, TimestampEncoder>;

template <EncoderConcept TEncoder>
using HistoryValueEncoder = KVTSValueEncoder<TEncoder, TimestampEncoder>;

template <EncoderConcept TKeyEncoder, EncoderConcept TValueEncoder>
struct HistoryPutQuery {
RWTxn& tx;
History entity;

using TKey = decltype(TKeyEncoder::value);
using TValue = decltype(TValueEncoder::value);

void exec(const TKey& key, const TValue& value, Timestamp timestamp) {
HistoryKeyEncoder<TKeyEncoder> key_encoder{entity.has_large_values};
key_encoder.value.key.value = key;
key_encoder.value.timestamp.value = timestamp;

HistoryValueEncoder<TValueEncoder> value_encoder{entity.has_large_values};
value_encoder.value.value.value = value;
value_encoder.value.timestamp.value = timestamp;

tx.rw_cursor(entity.values_table)->insert(key_encoder.encode(), value_encoder.encode());

InvertedIndexPutQuery<TKeyEncoder> inverted_index_query{tx, entity.inverted_index};
inverted_index_query.exec(key, timestamp, false);
}
};

template <EncoderConcept TKeyEncoder>
struct HistoryDeleteQuery {
RWTxn& tx;
History entity;

using TKey = decltype(TKeyEncoder::value);

void exec(const TKey& key, Timestamp timestamp) {
HistoryPutQuery<TKeyEncoder, RawEncoder<ByteView>> query{tx, entity};
query.exec(key, ByteView{}, timestamp);
}
};

} // namespace silkworm::datastore::kvdb
33 changes: 33 additions & 0 deletions silkworm/db/datastore/kvdb/inverted_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#pragma once

#include "../common/timestamp.hpp"
#include "big_endian_codec.hpp"
#include "codec.hpp"
#include "mdbx.hpp"

namespace silkworm::datastore::kvdb {
Expand All @@ -25,4 +28,34 @@ struct InvertedIndex {
const MapConfig& index_table;
};

using TimestampEncoder = BigEndianU64Codec;

template <EncoderConcept TKeyEncoder>
struct InvertedIndexPutQuery {
RWTxn& tx;
InvertedIndex entity;

using TKey = decltype(TKeyEncoder::value);

void exec(const TKey& key, const Timestamp timestamp, bool with_index_update) {
return exec<TimestampEncoder>(key, timestamp, with_index_update);
}

template <EncoderConcept TTimestampEncoder, typename TTimestamp = decltype(TTimestampEncoder::value)>
void exec(const TKey& key, const TTimestamp& timestamp, bool with_index_update) {
TKeyEncoder key_encoder;
key_encoder.value = key;
Slice key_slice = key_encoder.encode();

TTimestampEncoder ts_encoder;
ts_encoder.value = timestamp;
Slice ts_slice = ts_encoder.encode();

tx.rw_cursor(entity.keys_table)->insert(ts_slice, key_slice);
if (with_index_update) {
tx.rw_cursor(entity.index_table)->insert(key_slice, ts_slice);
}
}
};

} // namespace silkworm::datastore::kvdb
Loading

0 comments on commit 62946f2

Please sign in to comment.