16
16
17
17
#pragma once
18
18
19
- #include < silkworm/core/common/base.hpp>
20
- #include < silkworm/infra/common/ensure.hpp>
19
+ #include < cstdint>
20
+ #include < limits>
21
+ #include < string>
21
22
22
- #include " timestamp .hpp"
23
+ #include < silkworm/infra/common/ensure .hpp>
23
24
24
25
namespace silkworm ::datastore {
25
26
26
- // ! Scale factor to convert from-to block number values in block snapshot file names
27
- inline constexpr size_t kStepSizeForBlockSnapshots = 1'000 ;
28
-
29
- // ! Scale factor to convert from-to txn id values in temporal snapshot file names
30
- inline constexpr size_t kStepSizeForTemporalSnapshots = 1'562'500 ; // = 100M / 64
31
-
32
27
struct Step {
33
28
size_t value;
34
29
35
- explicit Step (size_t value1) : value(value1) {}
30
+ constexpr explicit Step (size_t value1) : value(value1) {}
36
31
friend bool operator ==(const Step&, const Step&) = default ;
37
32
bool operator <(const Step& other) const { return this ->value < other.value ; }
38
33
bool operator <=(const Step& other) const { return this ->value <= other.value ; }
39
34
std::string to_string () const { return std::to_string (value) + " st" ; }
40
-
41
- BlockNum to_block_num () const { return value * kStepSizeForBlockSnapshots ; }
42
- static Step from_block_num (BlockNum block_num) {
43
- return Step{static_cast <size_t >(block_num / kStepSizeForBlockSnapshots )};
44
- }
45
-
46
- TxnId to_txn_id () const { return value * kStepSizeForTemporalSnapshots ; }
47
- static Step from_txn_id (TxnId txn_id) {
48
- return Step{static_cast <size_t >(txn_id / kStepSizeForTemporalSnapshots )};
49
- }
50
35
};
51
36
37
+ inline constexpr Step kMaxStep {std::numeric_limits<size_t >::max ()};
38
+
52
39
struct StepRange {
53
40
Step start;
54
41
Step end;
@@ -61,62 +48,6 @@ struct StepRange {
61
48
bool contains_range (StepRange range) const { return (start <= range.start ) && (range.end <= end); }
62
49
size_t size () const { return end.value - start.value ; }
63
50
std::string to_string () const { return std::string (" [" ) + start.to_string () + " , " + end.to_string () + " )" ; }
64
-
65
- BlockNumRange to_block_num_range () const { return {start.to_block_num (), end.to_block_num ()}; }
66
- static StepRange from_block_num_range (BlockNumRange range) {
67
- return {Step::from_block_num (range.start ),
68
- Step::from_block_num (range.end >= kMaxBlockNum - kStepSizeForBlockSnapshots + 1 ? kMaxBlockNum
69
- : range.end + kStepSizeForBlockSnapshots - 1 )};
70
- }
71
-
72
- TxnIdRange to_txn_id_range () const { return {start.to_txn_id (), end.to_txn_id ()}; }
73
- static StepRange from_txn_id_range (TxnIdRange range) {
74
- return {Step::from_txn_id (range.start ),
75
- Step::from_txn_id (range.end >= kMaxTxnId - kStepSizeForTemporalSnapshots + 1 ? kMaxTxnId
76
- : range.end + kStepSizeForTemporalSnapshots - 1 )};
77
- }
78
- };
79
-
80
- struct StepToTimestampConverter {
81
- virtual ~StepToTimestampConverter () = default ;
82
- virtual Step step_from_timestamp (Timestamp t) const = 0;
83
- virtual Timestamp timestamp_from_step (Step s) const = 0;
84
- virtual StepRange step_range_from_timestamp_range (TimestampRange range) const = 0;
85
- virtual TimestampRange timestamp_range_from_step_range (StepRange range) const = 0;
86
- };
87
-
88
- struct StepToBlockNumConverter : public StepToTimestampConverter {
89
- ~StepToBlockNumConverter () override = default ;
90
- Step step_from_timestamp (Timestamp t) const override {
91
- return Step::from_block_num (t);
92
- }
93
- Timestamp timestamp_from_step (Step s) const override {
94
- return s.to_block_num ();
95
- }
96
- StepRange step_range_from_timestamp_range (TimestampRange range) const override {
97
- return StepRange::from_block_num_range ({range.start , range.end });
98
- }
99
- TimestampRange timestamp_range_from_step_range (StepRange range) const override {
100
- auto r = range.to_block_num_range ();
101
- return {r.start , r.end };
102
- }
103
- };
104
-
105
- struct StepToTxnIdConverter : public StepToTimestampConverter {
106
- ~StepToTxnIdConverter () override = default ;
107
- Step step_from_timestamp (Timestamp t) const override {
108
- return Step::from_txn_id (t);
109
- }
110
- Timestamp timestamp_from_step (Step s) const override {
111
- return s.to_txn_id ();
112
- }
113
- StepRange step_range_from_timestamp_range (TimestampRange range) const override {
114
- return StepRange::from_txn_id_range ({range.start , range.end });
115
- }
116
- TimestampRange timestamp_range_from_step_range (StepRange range) const override {
117
- auto r = range.to_txn_id_range ();
118
- return {r.start , r.end };
119
- }
120
51
};
121
52
122
53
} // namespace silkworm::datastore
0 commit comments