-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBucketIndexUtils.cpp
More file actions
104 lines (90 loc) · 3.46 KB
/
BucketIndexUtils.cpp
File metadata and controls
104 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2025 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "bucket/BucketIndexUtils.h"
#include "bucket/BucketManager.h"
#include "bucket/DiskIndex.h"
#include "bucket/HotArchiveBucket.h"
#include "bucket/HotArchiveBucketIndex.h"
#include "bucket/LiveBucket.h"
#include "bucket/LiveBucketIndex.h"
#include "main/Config.h"
#include "util/Fs.h"
#include <fmt/format.h>
namespace stellar
{
std::streamoff getPageSizeFromConfig(Config const& cfg)
{
if (cfg.BUCKETLIST_DB_INDEX_PAGE_SIZE_EXPONENT == 0)
{
return 0;
}
return 1UL << cfg.BUCKETLIST_DB_INDEX_PAGE_SIZE_EXPONENT;
}
template <class BucketT>
std::unique_ptr<typename BucketT::IndexT const>
createIndex(BucketManager& bm, std::filesystem::path const& filename,
Hash const& hash, asio::io_context& ctx, SHA256* hasher)
{
BUCKET_TYPE_ASSERT(BucketT);
ZoneScoped;
releaseAssertOrThrow(!filename.empty());
try
{
return std::unique_ptr<typename BucketT::IndexT const>(
new typename BucketT::IndexT(bm, filename, hash, ctx, hasher));
}
// BucketIndex throws if BucketManager shuts down before index finishes,
// so return empty index instead of partial index
catch (std::runtime_error&)
{
return {};
}
}
template <class BucketT>
std::unique_ptr<typename BucketT::IndexT const>
loadIndex(BucketManager const& bm, std::filesystem::path const& filename,
std::size_t fileSize)
{
std::ifstream in(filename, std::ios::binary);
if (!in)
{
throw std::runtime_error(fmt::format(
FMT_STRING("Error opening file {}"), filename.string()));
}
std::streamoff pageSize;
uint32_t version;
cereal::BinaryInputArchive ar(in);
DiskIndex<BucketT>::preLoad(ar, version, pageSize);
// Page size based on current settings. These may have changed since the
// on-disk index was serialized.
auto expectedPageSize =
BucketT::IndexT::getPageSize(bm.getConfig(), fileSize);
// Make sure on-disk index was built with correct version and config
// parameters before deserializing whole file
if (version != BucketT::IndexT::BUCKET_INDEX_VERSION ||
pageSize != expectedPageSize)
{
return {};
}
return std::unique_ptr<typename BucketT::IndexT const>(
new typename BucketT::IndexT(bm, ar, pageSize));
}
template std::unique_ptr<typename LiveBucket::IndexT const>
createIndex<LiveBucket>(BucketManager& bm,
std::filesystem::path const& filename, Hash const& hash,
asio::io_context& ctx, SHA256* hasher);
template std::unique_ptr<typename HotArchiveBucket::IndexT const>
createIndex<HotArchiveBucket>(BucketManager& bm,
std::filesystem::path const& filename,
Hash const& hash, asio::io_context& ctx,
SHA256* hasher);
template std::unique_ptr<typename LiveBucket::IndexT const>
loadIndex<LiveBucket>(BucketManager const& bm,
std::filesystem::path const& filename,
std::size_t fileSize);
template std::unique_ptr<typename HotArchiveBucket::IndexT const>
loadIndex<HotArchiveBucket>(BucketManager const& bm,
std::filesystem::path const& filename,
std::size_t fileSize);
}