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

snapshots: SnapshotPath.step_range #2419

Merged
merged 16 commits into from
Oct 15, 2024
Merged
Changes from 1 commit
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
Next Next commit
define steps
battlmonstr committed Oct 14, 2024
commit 0392457844546791b98847b5682bf078c3436a23
2 changes: 2 additions & 0 deletions silkworm/db/datastore/snapshots/snapshot_path.cpp
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ namespace silkworm::snapshots {

namespace fs = std::filesystem;

static constexpr size_t kFileNameBlockScaleFactor = kStepSizeForBlockSnapshots;

std::optional<SnapshotPath> SnapshotPath::parse(fs::path path) {
const std::string filename_no_ext = path.stem().string();

7 changes: 1 addition & 6 deletions silkworm/db/datastore/snapshots/snapshot_path.hpp
Original file line number Diff line number Diff line change
@@ -29,15 +29,10 @@
#include <silkworm/core/types/block.hpp>

#include "snapshot_type.hpp"
#include "step.hpp"

namespace silkworm::snapshots {

//! The scale factor to convert the block numbers to/from the values in snapshot file names
inline constexpr int kFileNameBlockScaleFactor{1'000};

//! The minimum segment size measured as number of blocks included in each segment
inline constexpr uint64_t kMinimumSegmentSize{kFileNameBlockScaleFactor};

inline constexpr const char* kSegmentExtension{".seg"};
inline constexpr const char* kIdxExtension{".idx"};
inline constexpr const char* kTmpExtension{".tmp"};
72 changes: 72 additions & 0 deletions silkworm/db/datastore/snapshots/step.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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 <silkworm/core/common/base.hpp>

namespace silkworm::snapshots {

//! Scale factor to convert from-to block number values in block snapshot file names
inline constexpr size_t kStepSizeForBlockSnapshots = 1'000;

//! Scale factor to convert from-to txn id values in temporal snapshot file names
inline constexpr size_t kStepSizeForTemporalSnapshots = 1'562'500; // = 100M / 64

struct Step {
size_t value;

explicit Step(size_t value1) : value(value1) {}
friend bool operator==(const Step&, const Step&) = default;
bool operator<(const Step& other) const { return this->value < other.value; }
bool operator<=(const Step& other) const { return this->value <= other.value; }
std::string to_string() const { return std::to_string(value) + "st"; }

BlockNum to_block_num() const { return value * kStepSizeForBlockSnapshots; }
static Step from_block_num(BlockNum block_num) {
return Step{block_num / kStepSizeForBlockSnapshots};
}

uint64_t to_txn_id() const { return value * kStepSizeForTemporalSnapshots; }
static Step from_txn_id(uint64_t txn_id) {
return Step{txn_id / kStepSizeForTemporalSnapshots};
}
};

struct StepRange {
Step start;
Step end;

StepRange(Step start1, Step end1) : start(start1), end(end1) {}
friend bool operator==(const StepRange&, const StepRange&) = default;
bool contains(Step x) const { return (start <= x) && (x < end); }
size_t size() const { return end.value - start.value; }
std::string to_string() const { return std::string("[") + start.to_string() + ", " + end.to_string() + ")"; }

BlockNumRange to_block_num_range() const { return {start.to_block_num(), end.to_block_num()}; }
static StepRange from_block_num_range(BlockNumRange range) {
return {Step::from_block_num(range.start), Step::from_block_num(range.end + kStepSizeForBlockSnapshots - 1)};
}

// TODO: fix range type to txn range
BlockNumRange to_txn_id_range() const { return {start.to_txn_id(), end.to_txn_id()}; }
// TODO: fix range type to txn range
static StepRange from_txn_id_range(BlockNumRange range) {
return {Step::from_txn_id(range.start), Step::from_txn_id(range.end + kStepSizeForTemporalSnapshots - 1)};
}
};

} // namespace silkworm::snapshots