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