xrpld
Loading...
Searching...
No Matches
State.cpp
1#include <xrpl/server/State.h>
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/config/BasicConfig.h>
5#include <xrpl/protocol/Protocol.h>
6#include <xrpl/rdb/SociDB.h>
7
8#include <boost/optional/optional.hpp> // IWYU pragma: keep
9
10#include <soci/boost-optional.h> // IWYU pragma: keep
11#include <soci/into.h>
12#include <soci/session.h>
13#include <soci/use.h>
14
15#include <cstdint>
16#include <stdexcept>
17#include <string>
18
19namespace xrpl {
20
21void
22initStateDB(soci::session& session, BasicConfig const& config, std::string const& dbName)
23{
24 open(session, config, dbName);
25
26 session << "PRAGMA synchronous=FULL;";
27
28 session << "CREATE TABLE IF NOT EXISTS DbState ("
29 " Key INTEGER PRIMARY KEY,"
30 " WritableDb TEXT,"
31 " ArchiveDb TEXT,"
32 " LastRotatedLedger INTEGER"
33 ");";
34
35 session << "CREATE TABLE IF NOT EXISTS CanDelete ("
36 " Key INTEGER PRIMARY KEY,"
37 " CanDeleteSeq INTEGER"
38 ");";
39
40 std::int64_t count = 0;
41 {
42 // SOCI requires boost::optional (not std::optional) as the parameter.
43 boost::optional<std::int64_t> countO;
44 session << "SELECT COUNT(Key) FROM DbState WHERE Key = 1;", soci::into(countO);
45 if (!countO)
46 Throw<std::runtime_error>("Failed to fetch Key Count from DbState.");
47 count = *countO;
48 }
49
50 if (count == 0)
51 {
52 session << "INSERT INTO DbState VALUES (1, '', '', 0);";
53 }
54
55 {
56 // SOCI requires boost::optional (not std::optional) as the parameter.
57 boost::optional<std::int64_t> countO;
58 session << "SELECT COUNT(Key) FROM CanDelete WHERE Key = 1;", soci::into(countO);
59 if (!countO)
60 Throw<std::runtime_error>("Failed to fetch Key Count from CanDelete.");
61 count = *countO;
62 }
63
64 if (count == 0)
65 {
66 session << "INSERT INTO CanDelete VALUES (1, 0);";
67 }
68}
69
71getCanDelete(soci::session& session)
72{
73 LedgerIndex seq = 0;
74 session << "SELECT CanDeleteSeq FROM CanDelete WHERE Key = 1;", soci::into(seq);
75 ;
76 return seq;
77}
78
80setCanDelete(soci::session& session, LedgerIndex canDelete)
81{
82 session << "UPDATE CanDelete SET CanDeleteSeq = :canDelete WHERE Key = 1;",
83 soci::use(canDelete);
84 return canDelete;
85}
86
87SavedState
88getSavedState(soci::session& session)
89{
90 SavedState state;
91 session << "SELECT WritableDb, ArchiveDb, LastRotatedLedger"
92 " FROM DbState WHERE Key = 1;",
93 soci::into(state.writableDb), soci::into(state.archiveDb), soci::into(state.lastRotated);
94
95 return state;
96}
97
98void
99setSavedState(soci::session& session, SavedState const& state)
100{
101 session << "UPDATE DbState"
102 " SET WritableDb = :writableDb,"
103 " ArchiveDb = :archiveDb,"
104 " LastRotatedLedger = :lastRotated"
105 " WHERE Key = 1;",
106 soci::use(state.writableDb), soci::use(state.archiveDb), soci::use(state.lastRotated);
107}
108
109void
110setLastRotated(soci::session& session, LedgerIndex seq)
111{
112 session << "UPDATE DbState SET LastRotatedLedger = :seq"
113 " WHERE Key = 1;",
114 soci::use(seq);
115}
116
117} // namespace xrpl
Holds unparsed configuration information.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void initStateDB(soci::session &session, BasicConfig const &config, std::string const &dbName)
initStateDB Opens a session with the State database.
Definition State.cpp:22
void setSavedState(soci::session &session, SavedState const &state)
setSavedState Saves the given state.
Definition State.cpp:99
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:370
void setLastRotated(soci::session &session, LedgerIndex seq)
setLastRotated Updates the last rotated ledger sequence.
Definition State.cpp:110
SavedState getSavedState(soci::session &session)
getSavedState Returns the saved state.
Definition State.cpp:88
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.
Definition SociDB.cpp:90
LedgerIndex setCanDelete(soci::session &session, LedgerIndex canDelete)
setCanDelete Updates the ledger sequence which can be deleted.
Definition State.cpp:80
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
LedgerIndex getCanDelete(soci::session &session)
getCanDelete Returns the ledger sequence which can be deleted.
Definition State.cpp:71
std::string writableDb
Definition State.h:13
LedgerIndex lastRotated
Definition State.h:15
std::string archiveDb
Definition State.h:14