rippled
Loading...
Searching...
No Matches
State.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2021 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/rdb/State.h>
21
22namespace ripple {
23
24void
26 soci::session& session,
27 BasicConfig const& config,
28 std::string const& dbName)
29{
30 open(session, config, dbName);
31
32 session << "PRAGMA synchronous=FULL;";
33
34 session << "CREATE TABLE IF NOT EXISTS DbState ("
35 " Key INTEGER PRIMARY KEY,"
36 " WritableDb TEXT,"
37 " ArchiveDb TEXT,"
38 " LastRotatedLedger INTEGER"
39 ");";
40
41 session << "CREATE TABLE IF NOT EXISTS CanDelete ("
42 " Key INTEGER PRIMARY KEY,"
43 " CanDeleteSeq INTEGER"
44 ");";
45
46 std::int64_t count = 0;
47 {
48 // SOCI requires boost::optional (not std::optional) as the parameter.
49 boost::optional<std::int64_t> countO;
50 session << "SELECT COUNT(Key) FROM DbState WHERE Key = 1;",
51 soci::into(countO);
52 if (!countO)
53 Throw<std::runtime_error>(
54 "Failed to fetch Key Count from DbState.");
55 count = *countO;
56 }
57
58 if (!count)
59 {
60 session << "INSERT INTO DbState VALUES (1, '', '', 0);";
61 }
62
63 {
64 // SOCI requires boost::optional (not std::optional) as the parameter.
65 boost::optional<std::int64_t> countO;
66 session << "SELECT COUNT(Key) FROM CanDelete WHERE Key = 1;",
67 soci::into(countO);
68 if (!countO)
69 Throw<std::runtime_error>(
70 "Failed to fetch Key Count from CanDelete.");
71 count = *countO;
72 }
73
74 if (!count)
75 {
76 session << "INSERT INTO CanDelete VALUES (1, 0);";
77 }
78}
79
81getCanDelete(soci::session& session)
82{
83 LedgerIndex seq;
84 session << "SELECT CanDeleteSeq FROM CanDelete WHERE Key = 1;",
85 soci::into(seq);
86 ;
87 return seq;
88}
89
91setCanDelete(soci::session& session, LedgerIndex canDelete)
92{
93 session << "UPDATE CanDelete SET CanDeleteSeq = :canDelete WHERE Key = 1;",
94 soci::use(canDelete);
95 return canDelete;
96}
97
98SavedState
99getSavedState(soci::session& session)
100{
101 SavedState state;
102 session << "SELECT WritableDb, ArchiveDb, LastRotatedLedger"
103 " FROM DbState WHERE Key = 1;",
104 soci::into(state.writableDb), soci::into(state.archiveDb),
105 soci::into(state.lastRotated);
106
107 return state;
108}
109
110void
111setSavedState(soci::session& session, SavedState const& state)
112{
113 session << "UPDATE DbState"
114 " SET WritableDb = :writableDb,"
115 " ArchiveDb = :archiveDb,"
116 " LastRotatedLedger = :lastRotated"
117 " WHERE Key = 1;",
118 soci::use(state.writableDb), soci::use(state.archiveDb),
119 soci::use(state.lastRotated);
120}
121
122void
123setLastRotated(soci::session& session, LedgerIndex seq)
124{
125 session << "UPDATE DbState SET LastRotatedLedger = :seq"
126 " WHERE Key = 1;",
127 soci::use(seq);
128}
129
130} // namespace ripple
Holds unparsed configuration information.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
LedgerIndex getCanDelete(soci::session &session)
getCanDelete Returns the ledger sequence which can be deleted.
Definition State.cpp:81
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:139
SavedState getSavedState(soci::session &session)
getSavedState Returns the saved state.
Definition State.cpp:99
void setSavedState(soci::session &session, SavedState const &state)
setSavedState Saves the given state.
Definition State.cpp:111
void initStateDB(soci::session &session, BasicConfig const &config, std::string const &dbName)
initStateDB Opens a session with the State database.
Definition State.cpp:25
@ 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:91
void setLastRotated(soci::session &session, LedgerIndex seq)
setLastRotated Updates the last rotated ledger sequence.
Definition State.cpp:123
LedgerIndex lastRotated
Definition State.h:37
std::string writableDb
Definition State.h:35
std::string archiveDb
Definition State.h:36