stage/publish/publisher.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <memory> | ||
| 7 | #include <span> | ||
| 8 | #include <string_view> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "chrono_batch_render/device/operation.h" | ||
| 13 | #include "chrono_batch_render/device/stream.h" | ||
| 14 | #include "chrono_batch_render/stage/publish/change_set.h" | ||
| 15 | #include "chrono_batch_render/stage/publish/header.h" | ||
| 16 | #include "chrono_batch_render/stage/publish/table.h" | ||
| 17 | #include "chrono_batch_render/stage/schema/schema.h" | ||
| 18 | #include "chrono_batch_render/stage/structure/structure.h" | ||
| 19 | #include "chrono_batch_render/stage/types.h" | ||
| 20 | #include "chrono_batch_render/util/error.h" | ||
| 21 | |||
| 22 | namespace chrono::batch_render { | ||
| 23 | |||
| 24 | 1167 | struct PinnedTables { | |
| 25 | Ref<device::Buffer> buffer; | ||
| 26 | std::vector<std::byte> header; | ||
| 27 | std::vector<Ref<const RefCounted>> held; | ||
| 28 | uint64_t content = 0; | ||
| 29 | std::vector<std::pair<std::string_view, uint64_t>> versions; | ||
| 30 | |||
| 31 | 44 | static const PinnedTables& From(const Snapshot& snapshot) { | |
| 32 | 390 | return *static_cast<const PinnedTables*>(snapshot.pinned.get()); | |
| 33 | } | ||
| 34 | |||
| 35 | 22 | template <class T> const T& Header() const { | |
| 36 | 22 | if (header.size() != sizeof(T)) { | |
| 37 | ✗ | throw Error("A snapshot header holds {} bytes, not {}", header.size(), sizeof(T)); | |
| 38 | } | ||
| 39 | 22 | return *reinterpret_cast<const T*>(header.data()); | |
| 40 | } | ||
| 41 | }; | ||
| 42 | |||
| 43 | class Publisher { | ||
| 44 | public: | ||
| 45 | 446 | explicit Publisher(Structure& structure, SnapshotHeader* header = nullptr) | |
| 46 | 446 | : m_structure(structure), m_header(header) {} | |
| 47 | |||
| 48 | 8242 | Ordinal Published() const { | |
| 49 | 8242 | return m_sealed.empty() ? 0 : m_sealed.back().ordinal; | |
| 50 | } | ||
| 51 | |||
| 52 | static constexpr size_t kSealedHistory = 4096; | ||
| 53 | |||
| 54 | 12289 | size_t SealedHistory() const { | |
| 55 | 12289 | return m_sealed.size(); | |
| 56 | } | ||
| 57 | |||
| 58 | bool Pending() const; | ||
| 59 | void Finish(); | ||
| 60 | |||
| 61 | 2 | uint32_t Outstanding() const { | |
| 62 | 2 | return m_outstanding; | |
| 63 | } | ||
| 64 | |||
| 65 | void Add(Table& table); | ||
| 66 | device::Operation Publish(Ordinal ordinal, Stream producer = Stream::None()); | ||
| 67 | device::Operation Refresh(std::span<Table* const> tables); | ||
| 68 | ChangeSet Changes(Ordinal since, std::span<const ColumnRef> columns = {}); | ||
| 69 | |||
| 70 | Snapshot Acquire(Stream consumer = Stream::None()); | ||
| 71 | void Release(const Snapshot& snapshot, Stream consumer = Stream::None()); | ||
| 72 | |||
| 73 | private: | ||
| 74 | struct Sealed { | ||
| 75 | Ordinal ordinal = 0; | ||
| 76 | Ordinal stamp = 0; | ||
| 77 | }; | ||
| 78 | |||
| 79 | 24084 | struct Scheduled { | |
| 80 | Table* table = nullptr; | ||
| 81 | Ordinal stamp = 0; | ||
| 82 | uint64_t writes = 0; | ||
| 83 | uint64_t epoch = 0; | ||
| 84 | bool ran = false; | ||
| 85 | uint64_t logged = 0; | ||
| 86 | std::vector<Ref<device::Buffer>> changed; | ||
| 87 | }; | ||
| 88 | |||
| 89 | Ordinal SealedStamp(Ordinal ordinal) const; | ||
| 90 | Ordinal SealedOrdinal(Ordinal stamp, Ordinal sealing) const; | ||
| 91 | ChangeSet Build(Ordinal since, Ordinal until, Ordinal sealing, | ||
| 92 | std::span<const ColumnRef> columns, std::span<const ColumnRef> rows, | ||
| 93 | std::vector<Ref<device::Buffer>>* reused = nullptr); | ||
| 94 | bool RunTables(Ordinal stamp, Ordinal ordinal, std::span<Table* const> only = {}); | ||
| 95 | void Trim(Ordinal stamp); | ||
| 96 | void Seal(); | ||
| 97 | |||
| 98 | Structure& m_structure; | ||
| 99 | SnapshotHeader* m_header = nullptr; | ||
| 100 | std::vector<Sealed> m_sealed; | ||
| 101 | std::vector<Scheduled> m_scheduled; | ||
| 102 | uint64_t m_writes = 0; | ||
| 103 | uint64_t m_epoch = 0; | ||
| 104 | uint32_t m_outstanding = 0; | ||
| 105 | TableVersions m_versions; | ||
| 106 | uint64_t m_tableRuns = 0; | ||
| 107 | std::shared_ptr<const PinnedTables> m_pinned; | ||
| 108 | }; | ||
| 109 | |||
| 110 | } | ||
| 111 |