VeloGraphX
High-performance dynamic graph analytics in C++20
Loading...
Searching...
No Matches
async_partition_loader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <filesystem>
5#include <future>
6#include <utility>
7#include <vector>
8
13
14namespace velographx {
15
17 public:
19 : cache_(resident_bytes) {}
20
21 std::future<std::vector<std::uint8_t>> prefetch(
22 std::uint64_t partition_id,
23 std::filesystem::path path) {
24 return std::async(std::launch::async,
25 [this, partition_id, path = std::move(path)]() mutable {
26 if (auto* cached = cache_.get(static_cast<PartitionId>(partition_id))) return *cached;
27 advise_prefetch(path);
28 auto payload = PartitionFile::read_mmap_or_fallback(path, partition_id);
29 const auto bytes = payload.size();
30 cache_.put(static_cast<PartitionId>(partition_id), payload, bytes);
31 return payload;
32 });
33 }
34
35 std::vector<std::uint8_t> load(std::uint64_t partition_id,
36 const std::filesystem::path& path) {
37 if (auto* cached = cache_.get(static_cast<PartitionId>(partition_id))) return *cached;
38 advise_prefetch(path);
39 auto payload = PartitionFile::read_mmap_or_fallback(path, partition_id);
40 const auto bytes = payload.size();
41 cache_.put(static_cast<PartitionId>(partition_id), payload, bytes);
42 return payload;
43 }
44
45 [[nodiscard]] const PartitionCacheStats& stats() const noexcept { return cache_.stats(); }
46 [[nodiscard]] std::size_t resident_bytes() const noexcept { return cache_.resident_bytes(); }
47 [[nodiscard]] FilePrefetchResult last_prefetch_result() const noexcept { return last_prefetch_; }
48 [[nodiscard]] IoUringPrefetchResult last_io_uring_result() const noexcept { return last_io_uring_; }
49
50 [[nodiscard]] static constexpr bool native_prefetch_supported() noexcept {
52 }
53 [[nodiscard]] static constexpr bool io_uring_prefetch_compiled() noexcept {
55 }
56
57 private:
58 void advise_prefetch(const std::filesystem::path& path) {
59 last_io_uring_ = IoUringPrefetchAdvisor::prefetch(path);
60 if (!last_io_uring_.succeeded) last_prefetch_ = FilePrefetchAdvisor::advise_will_need(path);
61 else last_prefetch_ = {};
62 }
63
64 PartitionCache<> cache_;
65 FilePrefetchResult last_prefetch_{};
66 IoUringPrefetchResult last_io_uring_{};
67};
68
69} // namespace velographx
const PartitionCacheStats & stats() const noexcept
IoUringPrefetchResult last_io_uring_result() const noexcept
FilePrefetchResult last_prefetch_result() const noexcept
AsyncPartitionLoader(std::size_t resident_bytes)
std::size_t resident_bytes() const noexcept
static constexpr bool native_prefetch_supported() noexcept
static constexpr bool io_uring_prefetch_compiled() noexcept
std::vector< std::uint8_t > load(std::uint64_t partition_id, const std::filesystem::path &path)
std::future< std::vector< std::uint8_t > > prefetch(std::uint64_t partition_id, std::filesystem::path path)
static FilePrefetchResult advise_will_need(const std::filesystem::path &path) noexcept
static constexpr bool supported() noexcept
static constexpr bool compiled() noexcept
static IoUringPrefetchResult prefetch(const std::filesystem::path &path, std::size_t max_bytes=1U<< 20U) noexcept
const PartitionCacheStats & stats() const noexcept
std::size_t resident_bytes() const noexcept
const Payload * get(PartitionId id)
void put(PartitionId id, Payload payload, std::size_t bytes)
static std::vector< std::uint8_t > read_mmap_or_fallback(const std::filesystem::path &path, std::uint64_t expected_partition_id, bool *used_mmap=nullptr)
std::uint32_t PartitionId