Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
Loading...
Searching...
No Matches
sqlite_hot_store.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <filesystem>
5#include <optional>
6#include <string>
7#include <vector>
8
9#include <sqlite3.h>
10
11#include "sqlite/wal.hpp"
12
13#include "event_projector.hpp"
14#include "events_store.hpp"
15
16namespace dcn::events
17{
18 class SQLiteHotStore final : public IHotEventStore
19 {
20 public:
22 const std::filesystem::path & hot_db_path,
23 const int default_chain_id);
24
25 ~SQLiteHotStore() override;
26
27 // ---- hot DB / serialized by runtime strand / synchronous ----
28
29 std::optional<std::int64_t> loadNextFromBlock(const int chain_id) override;
30 std::optional<std::uint64_t> loadNextLocalSeq(const int chain_id);
31
32 bool saveNextLocalSeq(const int chain_id, const std::uint64_t next_seq, const std::int64_t now_ms);
33
34 std::vector<std::int64_t> loadReorgWindowBlocks(
35 const int chain_id,
36 const std::int64_t from_block,
37 const std::int64_t to_block) const;
38
39 bool ingestBatch(
40 const int chain_id,
41 const std::vector<RawChainLog> & raw_events,
42 const std::vector<DecodedEvent> & decoded_events,
43 const std::vector<ChainBlockInfo> & block_infos,
44 const std::int64_t next_from_block,
45 const std::int64_t now_ms,
46 const std::optional<std::uint64_t> next_local_seq = std::nullopt) override;
47
48 bool ingestBatch(
49 const int chain_id,
50 const std::vector<DecodedEvent> & events,
51 const std::vector<ChainBlockInfo> & block_infos,
52 const std::int64_t next_from_block,
53 const std::int64_t now_ms);
54
55 bool applyFinality(
56 const int chain_id,
57 const FinalityHeights & heights,
58 const std::int64_t now_ms,
59 const std::size_t reorg_window_blocks) override;
60
61 // ---- min-cursor pruning / write-strand only ----
62
63 std::size_t pruneConsumedRaw(
64 std::int64_t watermark,
65 std::int64_t finalized_floor_block,
66 std::size_t batch_limit);
67
68 std::int64_t loadHeadBlock(int chain_id);
69
71
72 // ---- dead letters / write-strand only ----
73 // projector_bit is one of the *_DEAD_LETTER_BIT constants. A marked row is
74 // exempt from pruneConsumedRaw until every marking projector clears its bit.
75 // markDeadLetter returns false when the row does not exist or the write failed,
76 // in which case the caller must keep its cursor parked instead of skipping.
77
78 bool markDeadLetter(int projector_bit, int chain_id, const std::string & block_hash, std::int64_t log_index);
79 bool clearDeadLetter(int projector_bit, int chain_id, const std::string & block_hash, std::int64_t log_index);
80
81 // ---- read side / synchronous ----
82
83 std::vector<ChangeRecord> readChangesSince(std::int64_t after_change_seq, std::size_t limit) const;
84 std::vector<ChangeRecord> readDeadLetters(int projector_bit, std::size_t limit) const;
85
86 private:
87 bool _initializeHotSchema();
88 std::int64_t _nextChangeSeq();
89
90 private:
91 std::filesystem::path _hot_db_path;
92
93 sqlite3 * _write_db = nullptr;
94 sqlite3 * _read_db = nullptr;
95
96 int _default_chain_id = 1;
97 };
98}
Definition events_store.hpp:41
bool clearDeadLetter(int projector_bit, int chain_id, const std::string &block_hash, std::int64_t log_index)
Definition sqlite_hot_store.cpp:1585
std::optional< std::int64_t > loadNextFromBlock(const int chain_id) override
Definition sqlite_hot_store.cpp:270
std::vector< std::int64_t > loadReorgWindowBlocks(const int chain_id, const std::int64_t from_block, const std::int64_t to_block) const
Definition sqlite_hot_store.cpp:315
std::optional< std::uint64_t > loadNextLocalSeq(const int chain_id)
Definition sqlite_hot_store.cpp:283
bool applyFinality(const int chain_id, const FinalityHeights &heights, const std::int64_t now_ms, const std::size_t reorg_window_blocks) override
Definition sqlite_hot_store.cpp:956
storage::sqlite::WalCheckpointStats checkpointWal(storage::sqlite::WalCheckpointMode mode)
Definition sqlite_hot_store.cpp:217
SQLiteHotStore(const std::filesystem::path &hot_db_path, const int default_chain_id)
Definition sqlite_hot_store.cpp:120
~SQLiteHotStore() override
Definition sqlite_hot_store.cpp:203
std::vector< ChangeRecord > readChangesSince(std::int64_t after_change_seq, std::size_t limit) const
Definition sqlite_hot_store.cpp:1484
bool saveNextLocalSeq(const int chain_id, const std::uint64_t next_seq, const std::int64_t now_ms)
Definition sqlite_hot_store.cpp:296
bool markDeadLetter(int projector_bit, int chain_id, const std::string &block_hash, std::int64_t log_index)
Definition sqlite_hot_store.cpp:1550
bool ingestBatch(const int chain_id, const std::vector< RawChainLog > &raw_events, const std::vector< DecodedEvent > &decoded_events, const std::vector< ChainBlockInfo > &block_infos, const std::int64_t next_from_block, const std::int64_t now_ms, const std::optional< std::uint64_t > next_local_seq=std::nullopt) override
Definition sqlite_hot_store.cpp:371
std::vector< ChangeRecord > readDeadLetters(int projector_bit, std::size_t limit) const
Definition sqlite_hot_store.cpp:1516
std::int64_t loadHeadBlock(int chain_id)
Definition sqlite_hot_store.cpp:1311
std::size_t pruneConsumedRaw(std::int64_t watermark, std::int64_t finalized_floor_block, std::size_t batch_limit)
Definition sqlite_hot_store.cpp:1188
Definition decoded_event.hpp:11
WalCheckpointMode
Definition wal.hpp:9
Definition events_ingest.hpp:12