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