-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89996a6
commit 927719b
Showing
19 changed files
with
551 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
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 BigEndianU64Encoder::encode() { | ||
data.resize(sizeof(uint64_t), 0); | ||
endian::store_big_u64(data.data(), value); | ||
return to_slice(data); | ||
} | ||
|
||
} // namespace silkworm::datastore::kvdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
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 BigEndianU64Encoder : public Encoder { | ||
uint64_t value{0}; | ||
Bytes data; | ||
|
||
~BigEndianU64Encoder() override = default; | ||
Slice encode() override; | ||
}; | ||
|
||
static_assert(EncoderConcept<BigEndianU64Encoder>); | ||
|
||
} // namespace silkworm::datastore::kvdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
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 data) = 0; | ||
}; | ||
|
||
template <class TDecoder> | ||
concept DecoderConcept = | ||
std::derived_from<TDecoder, Decoder> && | ||
requires(TDecoder decoder) { decoder.value; }; | ||
|
||
} // namespace silkworm::datastore::kvdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
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" | ||
#include "raw_codec.hpp" | ||
|
||
namespace silkworm::datastore::kvdb { | ||
|
||
template <EncoderConcept TEncoder, EncoderConcept TTimestampEncoder> | ||
class KVTSKeyEncoder : public Encoder { | ||
public: | ||
struct { | ||
TEncoder key; | ||
TTimestampEncoder timestamp; | ||
} value; | ||
|
||
explicit KVTSKeyEncoder(bool has_large_values) | ||
: has_large_values_{has_large_values} {} | ||
~KVTSKeyEncoder() override = default; | ||
|
||
Slice encode() override { | ||
data_.clear(); | ||
if (has_large_values_) { | ||
// encode as key + timestamp | ||
data_.append(from_slice(value.key.encode())); | ||
data_.append(from_slice(value.timestamp.encode())); | ||
return to_slice(data_); | ||
} else { | ||
return value.key.encode(); | ||
} | ||
} | ||
|
||
private: | ||
bool has_large_values_; | ||
Bytes data_; | ||
}; | ||
|
||
template <EncoderConcept TEncoder, EncoderConcept TTimestampEncoder> | ||
class KVTSValueEncoder : public Encoder { | ||
public: | ||
struct { | ||
TEncoder value; | ||
TTimestampEncoder timestamp; | ||
} value; | ||
|
||
explicit KVTSValueEncoder(bool has_large_values) | ||
: has_large_values_{has_large_values} {} | ||
~KVTSValueEncoder() override = default; | ||
|
||
Slice encode() override { | ||
data_.clear(); | ||
if (has_large_values_) { | ||
return value.value.encode(); | ||
} else { | ||
// encode as timestamp + value | ||
data_.append(from_slice(value.timestamp.encode())); | ||
data_.append(from_slice(value.value.encode())); | ||
return to_slice(data_); | ||
} | ||
} | ||
|
||
private: | ||
bool has_large_values_; | ||
Bytes data_; | ||
}; | ||
|
||
static_assert(EncoderConcept<KVTSKeyEncoder<RawEncoder<Bytes>, RawEncoder<Bytes>>>); | ||
static_assert(EncoderConcept<KVTSValueEncoder<RawEncoder<Bytes>, RawEncoder<Bytes>>>); | ||
|
||
} // namespace silkworm::datastore::kvdb |
Oops, something went wrong.