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 <memory>
6#include <optional>
7#include <string>
8#include <unordered_set>
9#include <vector>
10
11#include <sqlite3.h>
12
13#include "sqlite/wal.hpp"
14
15#include "events_store.hpp"
16#include "events_archive.hpp"
17#include "events_feed.hpp"
18#include "events_shard.hpp"
19
20namespace dcn::events
21{
22 class SQLiteHotStore final : public IHotEventStore, public IArchiveManager
23 {
24 public:
26 const std::filesystem::path & hot_db_path,
27 const std::filesystem::path & archive_root,
28 const std::int64_t outbox_retention_ms,
29 const int default_chain_id,
30 std::string default_chain_namespace = "eth");
31
32 ~SQLiteHotStore() override;
33
34 // ---- hot DB / serialized by runtime strand / synchronous ----
35
36 std::optional<std::int64_t> loadNextFromBlock(const int chain_id) override;
37 std::optional<std::uint64_t> loadNextLocalSeq(const int chain_id);
38
39 bool saveNextLocalSeq(const int chain_id, const std::uint64_t next_seq, const std::int64_t now_ms);
40
41 std::vector<std::int64_t> loadReorgWindowBlocks(
42 const int chain_id,
43 const std::int64_t from_block,
44 const std::int64_t to_block) const;
45
46 bool ingestBatch(
47 const int chain_id,
48 const std::vector<RawChainLog> & raw_events,
49 const std::vector<DecodedEvent> & decoded_events,
50 const std::vector<ChainBlockInfo> & block_infos,
51 const std::int64_t next_from_block,
52 const std::int64_t now_ms,
53 const std::optional<std::uint64_t> next_local_seq = std::nullopt) override;
54
55 bool ingestBatch(
56 const int chain_id,
57 const std::vector<DecodedEvent> & events,
58 const std::vector<ChainBlockInfo> & block_infos,
59 const std::int64_t next_from_block,
60 const std::int64_t now_ms);
61
62 bool applyFinality(
63 const int chain_id,
64 const FinalityHeights & heights,
65 const std::int64_t now_ms,
66 const std::size_t reorg_window_blocks) override;
67
68 std::size_t projectBatch(const std::size_t limit, const std::int64_t now_ms) override;
69
70 bool runArchiveCycle(
71 const int chain_id,
72 const std::size_t hot_window_days,
73 const std::int64_t now_ms) override;
74
75 bool runCycle(const int chain_id, const std::size_t hot_window_days, const std::int64_t now_ms) override;
76
78
79 // ---- read side / synchronous ----
80
81 FeedPage getFeedPage(const FeedQuery & query) const;
82 StreamPage getStreamPage(const StreamQuery & query) const;
83
84 std::int64_t minAvailableStreamSeq() const;
85
86 private:
87 bool _initializeHotSchema();
88 bool _initializeArchiveSchema(sqlite3 * archive_db) const;
89
90 bool _exportMonth(const int chain_id, const std::string& month_token, const std::int64_t now_ms);
91
92 std::vector<std::filesystem::path> _candidateArchivePaths(const std::optional<CursorKey> & before_key) const;
93
94 void _appendFeedRowsFromDatabase(
95 sqlite3 * db,
96 const char * table_name,
97 const FeedQuery & query,
98 const std::optional<CursorKey> & before_key,
99 const std::size_t limit,
100 std::vector<FeedItem> & out_items,
101 std::unordered_set<std::string> & seen_feed_ids) const;
102
103 private:
104 std::filesystem::path _hot_db_path;
105 std::filesystem::path _archive_root;
106 std::int64_t _outbox_retention_ms = 0;
107
108 sqlite3 * _write_db = nullptr;
109 sqlite3 * _read_db = nullptr;
110 std::unique_ptr<IEventShardRouter> _shard_router;
111
112 int _default_chain_id = 1;
113 std::string _default_chain_namespace = "eth";
114 };
115}
Definition events_archive.hpp:9
Definition events_store.hpp:41
Definition sqlite_hot_store.hpp:23
std::optional< std::int64_t > loadNextFromBlock(const int chain_id) override
Definition sqlite_hot_store.cpp:451
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:496
std::optional< std::uint64_t > loadNextLocalSeq(const int chain_id)
Definition sqlite_hot_store.cpp:464
FeedPage getFeedPage(const FeedQuery &query) const
Definition sqlite_hot_store.cpp:1993
bool runCycle(const int chain_id, const std::size_t hot_window_days, const std::int64_t now_ms) override
Definition sqlite_hot_store.cpp:1988
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:1164
StreamPage getStreamPage(const StreamQuery &query) const
Definition sqlite_hot_store.cpp:2087
std::int64_t minAvailableStreamSeq() const
Definition sqlite_hot_store.cpp:2188
storage::sqlite::WalCheckpointStats checkpointWal(storage::sqlite::WalCheckpointMode mode)
Definition sqlite_hot_store.cpp:398
~SQLiteHotStore() override
Definition sqlite_hot_store.cpp:384
std::size_t projectBatch(const std::size_t limit, const std::int64_t now_ms) override
Definition sqlite_hot_store.cpp:1348
bool runArchiveCycle(const int chain_id, const std::size_t hot_window_days, const std::int64_t now_ms) override
Definition sqlite_hot_store.cpp:1783
bool saveNextLocalSeq(const int chain_id, const std::uint64_t next_seq, const std::int64_t now_ms)
Definition sqlite_hot_store.cpp:477
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:552
Definition decoded_event.hpp:11
WalCheckpointMode
Definition wal.hpp:9
Definition events_feed.hpp:57
Definition events_feed.hpp:49
Definition events_ingest.hpp:12
Definition events_feed.hpp:81
Definition events_feed.hpp:64