rippled
SQLiteDatabase.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 <ripple/app/ledger/AcceptedLedger.h>
21 #include <ripple/app/ledger/LedgerMaster.h>
22 #include <ripple/app/ledger/LedgerToJson.h>
23 #include <ripple/app/ledger/TransactionMaster.h>
24 #include <ripple/app/misc/Manifest.h>
25 #include <ripple/app/misc/impl/AccountTxPaging.h>
26 #include <ripple/app/rdb/backend/SQLiteDatabase.h>
27 #include <ripple/app/rdb/backend/detail/Node.h>
28 #include <ripple/app/rdb/backend/detail/Shard.h>
29 #include <ripple/basics/BasicConfig.h>
30 #include <ripple/basics/StringUtilities.h>
31 #include <ripple/core/DatabaseCon.h>
32 #include <ripple/core/SociDB.h>
33 #include <ripple/json/to_string.h>
34 #include <ripple/nodestore/DatabaseShard.h>
35 #include <soci/sqlite3/soci-sqlite3.h>
36 
37 namespace ripple {
38 
39 class SQLiteDatabaseImp final : public SQLiteDatabase
40 {
41 public:
43  Application& app,
44  Config const& config,
45  JobQueue& jobQueue)
46  : app_(app)
47  , useTxTables_(config.useTxTables())
48  , j_(app_.journal("SQLiteDatabaseImp"))
49  {
50  DatabaseCon::Setup const setup = setup_DatabaseCon(config, j_);
51  if (!makeLedgerDBs(
52  config,
53  setup,
55  {
56  std::string_view constexpr error =
57  "Failed to create ledger databases";
58 
59  JLOG(j_.fatal()) << error;
60  Throw<std::runtime_error>(error.data());
61  }
62 
63  if (app.getShardStore() &&
64  !makeMetaDBs(
65  config,
66  setup,
68  {
69  std::string_view constexpr error =
70  "Failed to create metadata databases";
71 
72  JLOG(j_.fatal()) << error;
73  Throw<std::runtime_error>(error.data());
74  }
75  }
76 
78  getMinLedgerSeq() override;
79 
81  getTransactionsMinLedgerSeq() override;
82 
85 
87  getMaxLedgerSeq() override;
88 
89  void
90  deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override;
91 
92  void
93  deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
94 
95  void
97 
98  void
100 
102  getTransactionCount() override;
103 
105  getAccountTransactionCount() override;
106 
108  getLedgerCountMinMax() override;
109 
110  bool
112  std::shared_ptr<Ledger const> const& ledger,
113  bool current) override;
114 
116  getLedgerInfoByIndex(LedgerIndex ledgerSeq) override;
117 
119  getNewestLedgerInfo() override;
120 
122  getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
123 
125  getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
126 
128  getLedgerInfoByHash(uint256 const& ledgerHash) override;
129 
130  uint256
131  getHashByIndex(LedgerIndex ledgerIndex) override;
132 
134  getHashesByIndex(LedgerIndex ledgerIndex) override;
135 
137  getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) override;
138 
140  getTxHistory(LedgerIndex startIndex) override;
141 
142  AccountTxs
143  getOldestAccountTxs(AccountTxOptions const& options) override;
144 
145  AccountTxs
146  getNewestAccountTxs(AccountTxOptions const& options) override;
147 
149  getOldestAccountTxsB(AccountTxOptions const& options) override;
150 
152  getNewestAccountTxsB(AccountTxOptions const& options) override;
153 
155  oldestAccountTxPage(AccountTxPageOptions const& options) override;
156 
158  newestAccountTxPage(AccountTxPageOptions const& options) override;
159 
161  oldestAccountTxPageB(AccountTxPageOptions const& options) override;
162 
164  newestAccountTxPageB(AccountTxPageOptions const& options) override;
165 
168  uint256 const& id,
170  error_code_i& ec) override;
171 
172  bool
173  ledgerDbHasSpace(Config const& config) override;
174 
175  bool
176  transactionDbHasSpace(Config const& config) override;
177 
179  getKBUsedAll() override;
180 
182  getKBUsedLedger() override;
183 
185  getKBUsedTransaction() override;
186 
187  void
188  closeLedgerDB() override;
189 
190  void
191  closeTransactionDB() override;
192 
193 private:
195  bool const useTxTables_;
199 
208  bool
210  Config const& config,
211  DatabaseCon::Setup const& setup,
212  DatabaseCon::CheckpointerSetup const& checkpointerSetup);
213 
222  bool
223  makeMetaDBs(
224  Config const& config,
225  DatabaseCon::Setup const& setup,
226  DatabaseCon::CheckpointerSetup const& checkpointerSetup);
227 
236  {
237  return app_.getShardStore()->seqToShardIndex(ledgerSeq);
238  }
239 
248  {
249  return app_.getShardStore()->firstLedgerSeq(shardIndex);
250  }
251 
260  {
261  return app_.getShardStore()->lastLedgerSeq(shardIndex);
262  }
263 
268  bool
270  {
271  return static_cast<bool>(lgrdb_);
272  }
273 
279  bool
281  {
282  return static_cast<bool>(txdb_);
283  }
284 
289  bool
291  {
292  return app_.getShardStore() != nullptr;
293  }
294 
300  auto
302  {
303  return lgrdb_->checkoutDb();
304  }
305 
311  auto
313  {
314  return txdb_->checkoutDb();
315  }
316 
325  bool
327  LedgerIndex ledgerSeq,
328  std::function<bool(soci::session& session)> const& callback)
329  {
331  ledgerSeq, callback);
332  }
333 
342  bool
344  LedgerIndex ledgerSeq,
345  std::function<bool(soci::session& session)> const& callback)
346  {
348  ledgerSeq, callback);
349  }
350 
362  bool
364  std::optional<std::uint32_t> firstIndex,
366  bool(soci::session& session, std::uint32_t shardIndex)> const&
367  callback)
368  {
370  firstIndex, callback);
371  }
372 
385  bool
387  std::optional<std::uint32_t> firstIndex,
389  bool(soci::session& session, std::uint32_t shardIndex)> const&
390  callback)
391  {
393  firstIndex, callback);
394  }
395 
408  bool
410  std::optional<std::uint32_t> firstIndex,
412  bool(soci::session& session, std::uint32_t shardIndex)> const&
413  callback)
414  {
416  firstIndex, callback);
417  }
418 
431  bool
433  std::optional<std::uint32_t> firstIndex,
435  bool(soci::session& session, std::uint32_t shardIndex)> const&
436  callback)
437  {
439  firstIndex, callback);
440  }
441 };
442 
443 bool
445  Config const& config,
446  DatabaseCon::Setup const& setup,
447  DatabaseCon::CheckpointerSetup const& checkpointerSetup)
448 {
449  auto [lgr, tx, res] =
450  detail::makeLedgerDBs(config, setup, checkpointerSetup);
451  txdb_ = std::move(tx);
452  lgrdb_ = std::move(lgr);
453  return res;
454 }
455 
456 bool
458  Config const& config,
459  DatabaseCon::Setup const& setup,
460  DatabaseCon::CheckpointerSetup const& checkpointerSetup)
461 {
462  auto [lgrMetaDB, txMetaDB] =
463  detail::makeMetaDBs(config, setup, checkpointerSetup);
464 
465  txMetaDB_ = std::move(txMetaDB);
466  lgrMetaDB_ = std::move(lgrMetaDB);
467 
468  return true;
469 }
470 
473 {
474  /* if databases exists, use it */
475  if (existsLedger())
476  {
477  auto db = checkoutLedger();
479  }
480 
481  /* else use shard databases, if available */
482  if (shardStoreExists())
483  {
486  {}, [&](soci::session& session, std::uint32_t shardIndex) {
488  session, detail::TableType::Ledgers);
489  return !res;
490  });
491  return res;
492  }
493 
494  /* else return empty value */
495  return {};
496 }
497 
500 {
501  if (!useTxTables_)
502  return {};
503 
504  if (existsTransaction())
505  {
506  auto db = checkoutTransaction();
508  }
509 
510  if (shardStoreExists())
511  {
514  {}, [&](soci::session& session, std::uint32_t shardIndex) {
517  return !res;
518  });
519  return res;
520  }
521 
522  return {};
523 }
524 
527 {
528  if (!useTxTables_)
529  return {};
530 
531  if (existsTransaction())
532  {
533  auto db = checkoutTransaction();
536  }
537 
538  if (shardStoreExists())
539  {
542  {}, [&](soci::session& session, std::uint32_t shardIndex) {
545  return !res;
546  });
547  return res;
548  }
549 
550  return {};
551 }
552 
555 {
556  if (existsLedger())
557  {
558  auto db = checkoutLedger();
560  }
561 
562  if (shardStoreExists())
563  {
566  {}, [&](soci::session& session, std::uint32_t shardIndex) {
568  session, detail::TableType::Ledgers);
569  return !res;
570  });
571  return res;
572  }
573 
574  return {};
575 }
576 
577 void
579 {
580  if (!useTxTables_)
581  return;
582 
583  if (existsTransaction())
584  {
585  auto db = checkoutTransaction();
587  *db, detail::TableType::Transactions, ledgerSeq);
588  return;
589  }
590 
591  if (shardStoreExists())
592  {
593  doTransaction(ledgerSeq, [&](soci::session& session) {
595  session, detail::TableType::Transactions, ledgerSeq);
596  return true;
597  });
598  }
599 }
600 
601 void
603 {
604  if (existsLedger())
605  {
606  auto db = checkoutLedger();
608  *db, detail::TableType::Ledgers, ledgerSeq);
609  return;
610  }
611 
612  if (shardStoreExists())
613  {
615  seqToShardIndex(ledgerSeq),
616  [&](soci::session& session, std::uint32_t shardIndex) {
618  session, detail::TableType::Ledgers, ledgerSeq);
619  return true;
620  });
621  }
622 }
623 
624 void
626 {
627  if (!useTxTables_)
628  return;
629 
630  if (existsTransaction())
631  {
632  auto db = checkoutTransaction();
634  *db, detail::TableType::Transactions, ledgerSeq);
635  return;
636  }
637 
638  if (shardStoreExists())
639  {
641  seqToShardIndex(ledgerSeq),
642  [&](soci::session& session, std::uint32_t shardIndex) {
644  session, detail::TableType::Transactions, ledgerSeq);
645  return true;
646  });
647  }
648 }
649 
650 void
652  LedgerIndex ledgerSeq)
653 {
654  if (!useTxTables_)
655  return;
656 
657  if (existsTransaction())
658  {
659  auto db = checkoutTransaction();
662  return;
663  }
664 
665  if (shardStoreExists())
666  {
668  seqToShardIndex(ledgerSeq),
669  [&](soci::session& session, std::uint32_t shardIndex) {
671  session, detail::TableType::AccountTransactions, ledgerSeq);
672  return true;
673  });
674  }
675 }
676 
679 {
680  if (!useTxTables_)
681  return 0;
682 
683  if (existsTransaction())
684  {
685  auto db = checkoutTransaction();
687  }
688 
689  if (shardStoreExists())
690  {
691  std::size_t rows = 0;
693  {}, [&](soci::session& session, std::uint32_t shardIndex) {
694  rows +=
696  return true;
697  });
698  return rows;
699  }
700 
701  return 0;
702 }
703 
706 {
707  if (!useTxTables_)
708  return 0;
709 
710  if (existsTransaction())
711  {
712  auto db = checkoutTransaction();
714  }
715 
716  if (shardStoreExists())
717  {
718  std::size_t rows = 0;
720  {}, [&](soci::session& session, std::uint32_t shardIndex) {
721  rows += detail::getRows(
723  return true;
724  });
725  return rows;
726  }
727 
728  return 0;
729 }
730 
733 {
734  if (existsLedger())
735  {
736  auto db = checkoutLedger();
738  }
739 
740  if (shardStoreExists())
741  {
742  CountMinMax res{0, 0, 0};
744  {}, [&](soci::session& session, std::uint32_t shardIndex) {
745  auto r =
747  if (r.numberOfRows)
748  {
749  res.numberOfRows += r.numberOfRows;
750  if (res.minLedgerSequence == 0)
751  res.minLedgerSequence = r.minLedgerSequence;
752  res.maxLedgerSequence = r.maxLedgerSequence;
753  }
754  return true;
755  });
756  return res;
757  }
758 
759  return {0, 0, 0};
760 }
761 
762 bool
764  std::shared_ptr<Ledger const> const& ledger,
765  bool current)
766 {
767  if (existsLedger())
768  {
770  *lgrdb_, *txdb_, app_, ledger, current))
771  return false;
772  }
773 
774  if (auto shardStore = app_.getShardStore(); shardStore)
775  {
776  if (ledger->info().seq < shardStore->earliestLedgerSeq())
777  // For the moment return false only when the ShardStore
778  // should accept the ledger, but fails when attempting
779  // to do so, i.e. when saveLedgerMeta fails. Later when
780  // the ShardStore supercedes the NodeStore, change this
781  // line to return false if the ledger is too early.
782  return true;
783 
784  auto lgrMetaSession = lgrMetaDB_->checkoutDb();
785  auto txMetaSession = txMetaDB_->checkoutDb();
786 
787  return detail::saveLedgerMeta(
788  ledger,
789  app_,
790  *lgrMetaSession,
791  *txMetaSession,
792  shardStore->seqToShardIndex(ledger->info().seq));
793  }
794 
795  return true;
796 }
797 
800 {
801  if (existsLedger())
802  {
803  auto db = checkoutLedger();
804  auto const res = detail::getLedgerInfoByIndex(*db, ledgerSeq, j_);
805 
806  if (res.has_value())
807  return res;
808  }
809 
810  if (shardStoreExists())
811  {
813  doLedger(ledgerSeq, [&](soci::session& session) {
814  res = detail::getLedgerInfoByIndex(session, ledgerSeq, j_);
815  return true;
816  });
817  return res;
818  }
819 
820  return {};
821 }
822 
825 {
826  if (existsLedger())
827  {
828  auto db = checkoutLedger();
829  auto const res = detail::getNewestLedgerInfo(*db, j_);
830 
831  if (res.has_value())
832  return res;
833  }
834 
835  if (shardStoreExists())
836  {
839  {}, [&](soci::session& session, std::uint32_t shardIndex) {
840  if (auto info = detail::getNewestLedgerInfo(session, j_))
841  {
842  res = info;
843  return false;
844  }
845  return true;
846  });
847 
848  return res;
849  }
850 
851  return {};
852 }
853 
856 {
857  if (existsLedger())
858  {
859  auto db = checkoutLedger();
860  auto const res =
861  detail::getLimitedOldestLedgerInfo(*db, ledgerFirstIndex, j_);
862 
863  if (res.has_value())
864  return res;
865  }
866 
867  if (shardStoreExists())
868  {
871  seqToShardIndex(ledgerFirstIndex),
872  [&](soci::session& session, std::uint32_t shardIndex) {
873  if (auto info = detail::getLimitedOldestLedgerInfo(
874  session, ledgerFirstIndex, j_))
875  {
876  res = info;
877  return false;
878  }
879  return true;
880  });
881 
882  return res;
883  }
884 
885  return {};
886 }
887 
890 {
891  if (existsLedger())
892  {
893  auto db = checkoutLedger();
894  auto const res =
895  detail::getLimitedNewestLedgerInfo(*db, ledgerFirstIndex, j_);
896 
897  if (res.has_value())
898  return res;
899  }
900 
901  if (shardStoreExists())
902  {
905  {}, [&](soci::session& session, std::uint32_t shardIndex) {
906  if (auto info = detail::getLimitedNewestLedgerInfo(
907  session, ledgerFirstIndex, j_))
908  {
909  res = info;
910  return false;
911  }
912  return shardIndex >= seqToShardIndex(ledgerFirstIndex);
913  });
914 
915  return res;
916  }
917 
918  return {};
919 }
920 
923 {
924  if (existsLedger())
925  {
926  auto db = checkoutLedger();
927  auto const res = detail::getLedgerInfoByHash(*db, ledgerHash, j_);
928 
929  if (res.has_value())
930  return res;
931  }
932 
933  if (auto shardStore = app_.getShardStore())
934  {
936  auto lgrMetaSession = lgrMetaDB_->checkoutDb();
937 
938  if (auto const shardIndex =
939  detail::getShardIndexforLedger(*lgrMetaSession, ledgerHash))
940  {
941  shardStore->callForLedgerSQLByShardIndex(
942  *shardIndex, [&](soci::session& session) {
943  res = detail::getLedgerInfoByHash(session, ledgerHash, j_);
944  return false; // unused
945  });
946  }
947 
948  return res;
949  }
950 
951  return {};
952 }
953 
954 uint256
956 {
957  if (existsLedger())
958  {
959  auto db = checkoutLedger();
960  auto const res = detail::getHashByIndex(*db, ledgerIndex);
961 
962  if (res.isNonZero())
963  return res;
964  }
965 
966  if (shardStoreExists())
967  {
968  uint256 hash;
969  doLedger(ledgerIndex, [&](soci::session& session) {
970  hash = detail::getHashByIndex(session, ledgerIndex);
971  return true;
972  });
973  return hash;
974  }
975 
976  return uint256();
977 }
978 
981 {
982  if (existsLedger())
983  {
984  auto db = checkoutLedger();
985  auto const res = detail::getHashesByIndex(*db, ledgerIndex, j_);
986 
987  if (res.has_value())
988  return res;
989  }
990 
991  if (shardStoreExists())
992  {
994  doLedger(ledgerIndex, [&](soci::session& session) {
995  res = detail::getHashesByIndex(session, ledgerIndex, j_);
996  return true;
997  });
998  return res;
999  }
1000 
1001  return {};
1002 }
1003 
1006 {
1007  if (existsLedger())
1008  {
1009  auto db = checkoutLedger();
1010  auto const res = detail::getHashesByIndex(*db, minSeq, maxSeq, j_);
1011 
1012  if (!res.empty())
1013  return res;
1014  }
1015 
1016  if (shardStoreExists())
1017  {
1019  while (minSeq <= maxSeq)
1020  {
1021  LedgerIndex shardMaxSeq = lastLedgerSeq(seqToShardIndex(minSeq));
1022  if (shardMaxSeq > maxSeq)
1023  shardMaxSeq = maxSeq;
1024  doLedger(minSeq, [&](soci::session& session) {
1025  auto r =
1026  detail::getHashesByIndex(session, minSeq, shardMaxSeq, j_);
1027  res.insert(r.begin(), r.end());
1028  return true;
1029  });
1030  minSeq = shardMaxSeq + 1;
1031  }
1032 
1033  return res;
1034  }
1035 
1036  return {};
1037 }
1038 
1041 {
1042  if (!useTxTables_)
1043  return {};
1044 
1045  if (existsTransaction())
1046  {
1047  auto db = checkoutTransaction();
1048  auto const res =
1049  detail::getTxHistory(*db, app_, startIndex, 20, false).first;
1050 
1051  if (!res.empty())
1052  return res;
1053  }
1054 
1055  if (shardStoreExists())
1056  {
1058  int quantity = 20;
1060  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1061  auto [tx, total] = detail::getTxHistory(
1062  session, app_, startIndex, quantity, true);
1063  txs.insert(txs.end(), tx.begin(), tx.end());
1064  if (total > 0)
1065  {
1066  quantity -= total;
1067  if (quantity <= 0)
1068  return false;
1069  startIndex = 0;
1070  }
1071  else
1072  {
1073  startIndex += total;
1074  }
1075  return true;
1076  });
1077 
1078  return txs;
1079  }
1080 
1081  return {};
1082 }
1083 
1086 {
1087  if (!useTxTables_)
1088  return {};
1089 
1091 
1092  if (existsTransaction())
1093  {
1094  auto db = checkoutTransaction();
1096  *db, app_, ledgerMaster, options, {}, j_)
1097  .first;
1098  }
1099 
1100  if (shardStoreExists())
1101  {
1102  AccountTxs ret;
1103  AccountTxOptions opt = options;
1104  int limit_used = 0;
1108  [&](soci::session& session, std::uint32_t shardIndex) {
1109  if (opt.maxLedger &&
1110  shardIndex > seqToShardIndex(opt.maxLedger))
1111  return false;
1112  auto [r, total] = detail::getOldestAccountTxs(
1113  session, app_, ledgerMaster, opt, limit_used, j_);
1114  ret.insert(ret.end(), r.begin(), r.end());
1115  if (!total)
1116  return false;
1117  if (total > 0)
1118  {
1119  limit_used += total;
1120  opt.offset = 0;
1121  }
1122  else
1123  {
1124  /*
1125  * If total < 0, then -total means number of transactions
1126  * skipped, see definition of return value of function
1127  * ripple::getOldestAccountTxs().
1128  */
1129  total = -total;
1130  if (opt.offset <= total)
1131  opt.offset = 0;
1132  else
1133  opt.offset -= total;
1134  }
1135  return true;
1136  });
1137 
1138  return ret;
1139  }
1140 
1141  return {};
1142 }
1143 
1144 RelationalDatabase::AccountTxs
1145 SQLiteDatabaseImp::getNewestAccountTxs(AccountTxOptions const& options)
1146 {
1147  if (!useTxTables_)
1148  return {};
1149 
1151 
1152  if (existsTransaction())
1153  {
1154  auto db = checkoutTransaction();
1155  return detail::getNewestAccountTxs(
1156  *db, app_, ledgerMaster, options, {}, j_)
1157  .first;
1158  }
1159 
1160  if (shardStoreExists())
1161  {
1162  AccountTxs ret;
1163  AccountTxOptions opt = options;
1164  int limit_used = 0;
1165  iterateTransactionBack(
1166  opt.maxLedger ? seqToShardIndex(opt.maxLedger)
1168  [&](soci::session& session, std::uint32_t shardIndex) {
1169  if (opt.minLedger &&
1170  shardIndex < seqToShardIndex(opt.minLedger))
1171  return false;
1172  auto [r, total] = detail::getNewestAccountTxs(
1173  session, app_, ledgerMaster, opt, limit_used, j_);
1174  ret.insert(ret.end(), r.begin(), r.end());
1175  if (!total)
1176  return false;
1177  if (total > 0)
1178  {
1179  limit_used += total;
1180  opt.offset = 0;
1181  }
1182  else
1183  {
1184  /*
1185  * If total < 0, then -total means number of transactions
1186  * skipped, see definition of return value of function
1187  * ripple::getNewestAccountTxs().
1188  */
1189  total = -total;
1190  if (opt.offset <= total)
1191  opt.offset = 0;
1192  else
1193  opt.offset -= total;
1194  }
1195  return true;
1196  });
1197 
1198  return ret;
1199  }
1200 
1201  return {};
1202 }
1203 
1204 RelationalDatabase::MetaTxsList
1205 SQLiteDatabaseImp::getOldestAccountTxsB(AccountTxOptions const& options)
1206 {
1207  if (!useTxTables_)
1208  return {};
1209 
1210  if (existsTransaction())
1211  {
1212  auto db = checkoutTransaction();
1213  return detail::getOldestAccountTxsB(*db, app_, options, {}, j_).first;
1214  }
1215 
1216  if (shardStoreExists())
1217  {
1218  MetaTxsList ret;
1219  AccountTxOptions opt = options;
1220  int limit_used = 0;
1221  iterateTransactionForward(
1222  opt.minLedger ? seqToShardIndex(opt.minLedger)
1224  [&](soci::session& session, std::uint32_t shardIndex) {
1225  if (opt.maxLedger &&
1226  shardIndex > seqToShardIndex(opt.maxLedger))
1227  return false;
1228  auto [r, total] = detail::getOldestAccountTxsB(
1229  session, app_, opt, limit_used, j_);
1230  ret.insert(ret.end(), r.begin(), r.end());
1231  if (!total)
1232  return false;
1233  if (total > 0)
1234  {
1235  limit_used += total;
1236  opt.offset = 0;
1237  }
1238  else
1239  {
1240  /*
1241  * If total < 0, then -total means number of transactions
1242  * skipped, see definition of return value of function
1243  * ripple::getOldestAccountTxsB().
1244  */
1245  total = -total;
1246  if (opt.offset <= total)
1247  opt.offset = 0;
1248  else
1249  opt.offset -= total;
1250  }
1251  return true;
1252  });
1253 
1254  return ret;
1255  }
1256 
1257  return {};
1258 }
1259 
1260 RelationalDatabase::MetaTxsList
1261 SQLiteDatabaseImp::getNewestAccountTxsB(AccountTxOptions const& options)
1262 {
1263  if (!useTxTables_)
1264  return {};
1265 
1266  if (existsTransaction())
1267  {
1268  auto db = checkoutTransaction();
1269  return detail::getNewestAccountTxsB(*db, app_, options, {}, j_).first;
1270  }
1271 
1272  if (shardStoreExists())
1273  {
1274  MetaTxsList ret;
1275  AccountTxOptions opt = options;
1276  int limit_used = 0;
1277  iterateTransactionBack(
1278  opt.maxLedger ? seqToShardIndex(opt.maxLedger)
1280  [&](soci::session& session, std::uint32_t shardIndex) {
1281  if (opt.minLedger &&
1282  shardIndex < seqToShardIndex(opt.minLedger))
1283  return false;
1284  auto [r, total] = detail::getNewestAccountTxsB(
1285  session, app_, opt, limit_used, j_);
1286  ret.insert(ret.end(), r.begin(), r.end());
1287  if (!total)
1288  return false;
1289  if (total > 0)
1290  {
1291  limit_used += total;
1292  opt.offset = 0;
1293  }
1294  else
1295  {
1296  /*
1297  * If total < 0, then -total means number of transactions
1298  * skipped, see definition of return value of function
1299  * ripple::getNewestAccountTxsB().
1300  */
1301  total = -total;
1302  if (opt.offset <= total)
1303  opt.offset = 0;
1304  else
1305  opt.offset -= total;
1306  }
1307  return true;
1308  });
1309 
1310  return ret;
1311  }
1312 
1313  return {};
1314 }
1315 
1316 std::pair<
1317  RelationalDatabase::AccountTxs,
1319 SQLiteDatabaseImp::oldestAccountTxPage(AccountTxPageOptions const& options)
1320 {
1321  if (!useTxTables_)
1322  return {};
1323 
1324  static std::uint32_t const page_length(200);
1325  auto onUnsavedLedger =
1326  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1327  AccountTxs ret;
1328  Application& app = app_;
1329  auto onTransaction = [&ret, &app](
1330  std::uint32_t ledger_index,
1331  std::string const& status,
1332  Blob&& rawTxn,
1333  Blob&& rawMeta) {
1334  convertBlobsToTxResult(ret, ledger_index, status, rawTxn, rawMeta, app);
1335  };
1336 
1337  if (existsTransaction())
1338  {
1339  auto db = checkoutTransaction();
1340  auto newmarker =
1341  detail::oldestAccountTxPage(
1342  *db, onUnsavedLedger, onTransaction, options, 0, page_length)
1343  .first;
1344  return {ret, newmarker};
1345  }
1346 
1347  if (shardStoreExists())
1348  {
1349  AccountTxPageOptions opt = options;
1350  int limit_used = 0;
1351  iterateTransactionForward(
1352  opt.minLedger ? seqToShardIndex(opt.minLedger)
1354  [&](soci::session& session, std::uint32_t shardIndex) {
1355  if (opt.maxLedger != UINT32_MAX &&
1356  shardIndex > seqToShardIndex(opt.minLedger))
1357  return false;
1358  auto [marker, total] = detail::oldestAccountTxPage(
1359  session,
1360  onUnsavedLedger,
1361  onTransaction,
1362  opt,
1363  limit_used,
1364  page_length);
1365  opt.marker = marker;
1366  if (total < 0)
1367  return false;
1368  limit_used += total;
1369  return true;
1370  });
1371 
1372  return {ret, opt.marker};
1373  }
1374 
1375  return {};
1376 }
1377 
1378 std::pair<
1381 SQLiteDatabaseImp::newestAccountTxPage(AccountTxPageOptions const& options)
1382 {
1383  if (!useTxTables_)
1384  return {};
1385 
1386  static std::uint32_t const page_length(200);
1387  auto onUnsavedLedger =
1388  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1389  AccountTxs ret;
1390  Application& app = app_;
1391  auto onTransaction = [&ret, &app](
1392  std::uint32_t ledger_index,
1393  std::string const& status,
1394  Blob&& rawTxn,
1395  Blob&& rawMeta) {
1396  convertBlobsToTxResult(ret, ledger_index, status, rawTxn, rawMeta, app);
1397  };
1398 
1399  if (existsTransaction())
1400  {
1401  auto db = checkoutTransaction();
1402  auto newmarker =
1403  detail::newestAccountTxPage(
1404  *db, onUnsavedLedger, onTransaction, options, 0, page_length)
1405  .first;
1406  return {ret, newmarker};
1407  }
1408 
1409  if (shardStoreExists())
1410  {
1411  AccountTxPageOptions opt = options;
1412  int limit_used = 0;
1413  iterateTransactionBack(
1414  opt.maxLedger != UINT32_MAX ? seqToShardIndex(opt.maxLedger)
1416  [&](soci::session& session, std::uint32_t shardIndex) {
1417  if (opt.minLedger &&
1418  shardIndex < seqToShardIndex(opt.minLedger))
1419  return false;
1420  auto [marker, total] = detail::newestAccountTxPage(
1421  session,
1422  onUnsavedLedger,
1423  onTransaction,
1424  opt,
1425  limit_used,
1426  page_length);
1427  opt.marker = marker;
1428  if (total < 0)
1429  return false;
1430  limit_used += total;
1431  return true;
1432  });
1433 
1434  return {ret, opt.marker};
1435  }
1436 
1437  return {};
1438 }
1439 
1440 std::pair<
1443 SQLiteDatabaseImp::oldestAccountTxPageB(AccountTxPageOptions const& options)
1444 {
1445  if (!useTxTables_)
1446  return {};
1447 
1448  static std::uint32_t const page_length(500);
1449  auto onUnsavedLedger =
1450  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1451  MetaTxsList ret;
1452  auto onTransaction = [&ret](
1453  std::uint32_t ledgerIndex,
1454  std::string const& status,
1455  Blob&& rawTxn,
1456  Blob&& rawMeta) {
1457  ret.emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1458  };
1459 
1460  if (existsTransaction())
1461  {
1462  auto db = checkoutTransaction();
1463  auto newmarker =
1464  detail::oldestAccountTxPage(
1465  *db, onUnsavedLedger, onTransaction, options, 0, page_length)
1466  .first;
1467  return {ret, newmarker};
1468  }
1469 
1470  if (shardStoreExists())
1471  {
1472  AccountTxPageOptions opt = options;
1473  int limit_used = 0;
1474  iterateTransactionForward(
1475  opt.minLedger ? seqToShardIndex(opt.minLedger)
1477  [&](soci::session& session, std::uint32_t shardIndex) {
1478  if (opt.maxLedger != UINT32_MAX &&
1479  shardIndex > seqToShardIndex(opt.minLedger))
1480  return false;
1481  auto [marker, total] = detail::oldestAccountTxPage(
1482  session,
1483  onUnsavedLedger,
1484  onTransaction,
1485  opt,
1486  limit_used,
1487  page_length);
1488  opt.marker = marker;
1489  if (total < 0)
1490  return false;
1491  limit_used += total;
1492  return true;
1493  });
1494 
1495  return {ret, opt.marker};
1496  }
1497 
1498  return {};
1499 }
1500 
1501 std::pair<
1504 SQLiteDatabaseImp::newestAccountTxPageB(AccountTxPageOptions const& options)
1505 {
1506  if (!useTxTables_)
1507  return {};
1508 
1509  static std::uint32_t const page_length(500);
1510  auto onUnsavedLedger =
1511  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1512  MetaTxsList ret;
1513  auto onTransaction = [&ret](
1514  std::uint32_t ledgerIndex,
1515  std::string const& status,
1516  Blob&& rawTxn,
1517  Blob&& rawMeta) {
1518  ret.emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1519  };
1520 
1521  if (existsTransaction())
1522  {
1523  auto db = checkoutTransaction();
1524  auto newmarker =
1525  detail::newestAccountTxPage(
1526  *db, onUnsavedLedger, onTransaction, options, 0, page_length)
1527  .first;
1528  return {ret, newmarker};
1529  }
1530 
1531  if (shardStoreExists())
1532  {
1533  AccountTxPageOptions opt = options;
1534  int limit_used = 0;
1535  iterateTransactionBack(
1536  opt.maxLedger != UINT32_MAX ? seqToShardIndex(opt.maxLedger)
1538  [&](soci::session& session, std::uint32_t shardIndex) {
1539  if (opt.minLedger &&
1540  shardIndex < seqToShardIndex(opt.minLedger))
1541  return false;
1542  auto [marker, total] = detail::newestAccountTxPage(
1543  session,
1544  onUnsavedLedger,
1545  onTransaction,
1546  opt,
1547  limit_used,
1548  page_length);
1549  opt.marker = marker;
1550  if (total < 0)
1551  return false;
1552  limit_used += total;
1553  return true;
1554  });
1555 
1556  return {ret, opt.marker};
1557  }
1558 
1559  return {};
1560 }
1561 
1563 SQLiteDatabaseImp::getTransaction(
1564  uint256 const& id,
1566  error_code_i& ec)
1567 {
1568  if (!useTxTables_)
1569  return TxSearched::unknown;
1570 
1571  if (existsTransaction())
1572  {
1573  auto db = checkoutTransaction();
1574  return detail::getTransaction(*db, app_, id, range, ec);
1575  }
1576 
1577  if (auto shardStore = app_.getShardStore(); shardStore)
1578  {
1579  std::variant<AccountTx, TxSearched> res(TxSearched::unknown);
1580  auto txMetaSession = txMetaDB_->checkoutDb();
1581 
1582  if (auto const shardIndex =
1583  detail::getShardIndexforTransaction(*txMetaSession, id))
1584  {
1585  shardStore->callForTransactionSQLByShardIndex(
1586  *shardIndex, [&](soci::session& session) {
1588  if (range)
1589  {
1590  std::uint32_t const low = std::max(
1591  range->lower(), firstLedgerSeq(*shardIndex));
1592  std::uint32_t const high = std::min(
1593  range->upper(), lastLedgerSeq(*shardIndex));
1594  if (low <= high)
1595  range1 = ClosedInterval<std::uint32_t>(low, high);
1596  }
1597  res = detail::getTransaction(session, app_, id, range1, ec);
1598 
1599  return res.index() == 1 &&
1600  std::get<TxSearched>(res) !=
1601  TxSearched::unknown; // unused
1602  });
1603  }
1604 
1605  return res;
1606  }
1607 
1608  return TxSearched::unknown;
1609 }
1610 
1611 bool
1612 SQLiteDatabaseImp::ledgerDbHasSpace(Config const& config)
1613 {
1614  if (existsLedger())
1615  {
1616  auto db = checkoutLedger();
1617  return detail::dbHasSpace(*db, config, j_);
1618  }
1619 
1620  if (shardStoreExists())
1621  {
1622  return iterateLedgerBack(
1623  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1624  return detail::dbHasSpace(session, config, j_);
1625  });
1626  }
1627 
1628  return true;
1629 }
1630 
1631 bool
1632 SQLiteDatabaseImp::transactionDbHasSpace(Config const& config)
1633 {
1634  if (!useTxTables_)
1635  return true;
1636 
1637  if (existsTransaction())
1638  {
1639  auto db = checkoutTransaction();
1640  return detail::dbHasSpace(*db, config, j_);
1641  }
1642 
1643  if (shardStoreExists())
1644  {
1645  return iterateTransactionBack(
1646  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1647  return detail::dbHasSpace(session, config, j_);
1648  });
1649  }
1650 
1651  return true;
1652 }
1653 
1655 SQLiteDatabaseImp::getKBUsedAll()
1656 {
1657  if (existsLedger())
1658  {
1659  return ripple::getKBUsedAll(lgrdb_->getSession());
1660  }
1661 
1662  if (shardStoreExists())
1663  {
1664  std::uint32_t sum = 0;
1665  iterateLedgerBack(
1666  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1667  sum += ripple::getKBUsedAll(session);
1668  return true;
1669  });
1670  return sum;
1671  }
1672 
1673  return 0;
1674 }
1675 
1677 SQLiteDatabaseImp::getKBUsedLedger()
1678 {
1679  if (existsLedger())
1680  {
1681  return ripple::getKBUsedDB(lgrdb_->getSession());
1682  }
1683 
1684  if (shardStoreExists())
1685  {
1686  std::uint32_t sum = 0;
1687  iterateLedgerBack(
1688  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1689  sum += ripple::getKBUsedDB(session);
1690  return true;
1691  });
1692  return sum;
1693  }
1694 
1695  return 0;
1696 }
1697 
1699 SQLiteDatabaseImp::getKBUsedTransaction()
1700 {
1701  if (!useTxTables_)
1702  return 0;
1703 
1704  if (existsTransaction())
1705  {
1706  return ripple::getKBUsedDB(txdb_->getSession());
1707  }
1708 
1709  if (shardStoreExists())
1710  {
1711  std::uint32_t sum = 0;
1712  iterateTransactionBack(
1713  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1714  sum += ripple::getKBUsedDB(session);
1715  return true;
1716  });
1717  return sum;
1718  }
1719 
1720  return 0;
1721 }
1722 
1723 void
1724 SQLiteDatabaseImp::closeLedgerDB()
1725 {
1726  lgrdb_.reset();
1727 }
1728 
1729 void
1730 SQLiteDatabaseImp::closeTransactionDB()
1731 {
1732  txdb_.reset();
1733 }
1734 
1736 getSQLiteDatabase(Application& app, Config const& config, JobQueue& jobQueue)
1737 {
1738  return std::make_unique<SQLiteDatabaseImp>(app, config, jobQueue);
1739 }
1740 
1741 } // namespace ripple
ripple::SQLiteDatabase
Definition: SQLiteDatabase.h:27
ripple::detail::deleteByLedgerSeq
void deleteByLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteByLedgerSeq Deletes all entries in given table for the ledger with given sequence.
Definition: Node.cpp:143
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
ripple::NodeStore::Database::lastLedgerSeq
std::uint32_t lastLedgerSeq(std::uint32_t shardIndex) const noexcept
Calculates the last ledger sequence for a given shard index.
Definition: Database.h:271
ripple::Application
Definition: Application.h:115
ripple::detail::TableType::Ledgers
@ Ledgers
ripple::HashPrefix::ledgerMaster
@ ledgerMaster
ledger master data for signing
ripple::detail::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo(soci::session &session, beast::Journal j)
getNewestLedgerInfo Returns info of newest saved ledger.
Definition: Node.cpp:467
ripple::NodeStore::DatabaseShard::callForLedgerSQLByLedgerSeq
virtual bool callForLedgerSQLByLedgerSeq(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)=0
Invoke a callback on the SQLite db holding the corresponding ledger.
std::bind
T bind(T... args)
std::string
STL class.
std::shared_ptr
STL class.
ripple::SQLiteDatabaseImp::lgrMetaDB_
std::unique_ptr< DatabaseCon > lgrMetaDB_
Definition: SQLiteDatabase.cpp:198
ripple::SQLiteDatabaseImp::deleteBeforeLedgerSeq
void deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteBeforeLedgerSeq Deletes all ledgers with a sequence number less than or equal to the given ledg...
Definition: SQLiteDatabase.cpp:602
ripple::SQLiteDatabaseImp::app_
Application & app_
Definition: SQLiteDatabase.cpp:194
ripple::DatabaseCon::Setup
Definition: DatabaseCon.h:84
ripple::detail::deleteBeforeLedgerSeq
void deleteBeforeLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteBeforeLedgerSeq Deletes all entries in given table for the ledgers with given sequence and all ...
Definition: Node.cpp:150
ripple::detail::saveLedgerMeta
bool saveLedgerMeta(std::shared_ptr< Ledger const > const &ledger, Application &app, soci::session &lgrMetaSession, soci::session &txnMetaSession, std::uint32_t const shardIndex)
saveLedgerMeta Stores (transaction ID -> shard index) and (ledger hash -> shard index) mappings in th...
Definition: app/rdb/backend/detail/impl/Shard.cpp:56
std::string_view
STL class.
ripple::SQLiteDatabaseImp::existsLedger
bool existsLedger()
existsLedger Checks if the node store ledger database exists.
Definition: SQLiteDatabase.cpp:269
ripple::SQLiteDatabaseImp::seqToShardIndex
std::uint32_t seqToShardIndex(LedgerIndex ledgerSeq)
seqToShardIndex Provides the index of the shard that stores the ledger with the given sequence.
Definition: SQLiteDatabase.cpp:235
ripple::SQLiteDatabaseImp::getKBUsedLedger
std::uint32_t getKBUsedLedger() override
getKBUsedLedger Returns the amount of space space used by the ledger database.
Definition: SQLiteDatabase.cpp:1677
std::pair
ripple::SQLiteDatabaseImp::iterateLedgerForward
bool iterateLedgerForward(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)
iterateLedgerForward Checks out ledger databases for all shards in ascending order starting from the ...
Definition: SQLiteDatabase.cpp:363
ripple::SQLiteDatabaseImp::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq) override
getLedgerInfoByIndex Returns a ledger by its sequence.
Definition: SQLiteDatabase.cpp:799
ripple::LedgerMaster
Definition: LedgerMaster.h:70
ripple::SQLiteDatabaseImp::saveValidatedLedger
bool saveValidatedLedger(std::shared_ptr< Ledger const > const &ledger, bool current) override
saveValidatedLedger Saves a ledger into the database.
Definition: SQLiteDatabase.cpp:763
ripple::setup_DatabaseCon
DatabaseCon::Setup setup_DatabaseCon(Config const &c, std::optional< beast::Journal > j=std::nullopt)
Definition: DatabaseCon.cpp:106
std::vector
STL class.
ripple::detail::makeMetaDBs
DatabasePair makeMetaDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeMetaDBs Opens ledger and transaction 'meta' databases which map ledger hashes and transaction IDs...
Definition: app/rdb/backend/detail/impl/Shard.cpp:32
ripple::SQLiteDatabaseImp::getKBUsedTransaction
std::uint32_t getKBUsedTransaction() override
getKBUsedTransaction Returns the amount of space used by the transaction database.
Definition: SQLiteDatabase.cpp:1699
ripple::detail::getRowsMinMax
RelationalDatabase::CountMinMax getRowsMinMax(soci::session &session, TableType type)
getRowsMinMax Returns minimum ledger sequence, maximum ledger sequence and total number of rows in gi...
Definition: Node.cpp:172
ripple::RelationalDatabase::AccountTxPageOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDatabase.h:77
ripple::SQLiteDatabaseImp::makeMetaDBs
bool makeMetaDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeMetaDBs Opens shard index lookup databases, and stores their descriptors in private member variab...
Definition: SQLiteDatabase.cpp:457
ripple::convertBlobsToTxResult
void convertBlobsToTxResult(RelationalDatabase::AccountTxs &to, std::uint32_t ledger_index, std::string const &status, Blob const &rawTxn, Blob const &rawMeta, Application &app)
Definition: AccountTxPaging.cpp:33
ripple::RelationalDatabase::AccountTxs
std::vector< AccountTx > AccountTxs
Definition: RelationalDatabase.h:86
ripple::SQLiteDatabaseImp::doLedger
bool doLedger(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)
doLedger Checks out the ledger database owned by the shard containing the given ledger,...
Definition: SQLiteDatabase.cpp:326
ripple::DatabaseCon::CheckpointerSetup
Definition: DatabaseCon.h:107
ripple::getKBUsedDB
std::uint32_t getKBUsedDB(soci::session &s)
Definition: SociDB.cpp:139
ripple::SQLiteDatabaseImp::iterateLedgerBack
bool iterateLedgerBack(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)
iterateLedgerBack Checks out ledger databases for all shards in descending order starting from the gi...
Definition: SQLiteDatabase.cpp:409
ripple::detail::TableType::Transactions
@ Transactions
ripple::RelationalDatabase::AccountTxOptions
Definition: RelationalDatabase.h:64
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::NodeStore::DatabaseShard::callForTransactionSQLByLedgerSeq
virtual bool callForTransactionSQLByLedgerSeq(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)=0
Invoke a callback on the transaction SQLite db for the corresponding ledger.
ripple::SQLiteDatabaseImp::transactionDbHasSpace
bool transactionDbHasSpace(Config const &config) override
transactionDbHasSpace Checks if the transaction database has available space.
Definition: SQLiteDatabase.cpp:1632
ripple::RelationalDatabase::AccountTxOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDatabase.h:67
ripple::SQLiteDatabaseImp::newestAccountTxPage
std::pair< AccountTxs, std::optional< AccountTxMarker > > newestAccountTxPage(AccountTxPageOptions const &options) override
newestAccountTxPage Returns the newest transactions for the account that matches the given criteria s...
Definition: SQLiteDatabase.cpp:1381
std::function
ripple::detail::getLimitedOldestLedgerInfo
std::optional< LedgerInfo > getLimitedOldestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedOldestLedgerInfo Returns info of oldest ledger from ledgers with sequences greather or equa...
Definition: Node.cpp:475
ripple::SQLiteDatabaseImp::firstLedgerSeq
LedgerIndex firstLedgerSeq(std::uint32_t shardIndex)
firstLedgerSeq Returns the sequence of the first ledger stored in the shard specified by the shard in...
Definition: SQLiteDatabase.cpp:247
ripple::SQLiteDatabaseImp::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq() override
getMaxLedgerSeq Returns the maximum ledger sequence in the Ledgers table.
Definition: SQLiteDatabase.cpp:554
ripple::SQLiteDatabaseImp::deleteAccountTransactionsBeforeLedgerSeq
void deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteAccountTransactionsBeforeLedgerSeq Deletes all account transactions with a sequence number less...
Definition: SQLiteDatabase.cpp:651
ripple::detail::getHashByIndex
uint256 getHashByIndex(soci::session &session, LedgerIndex ledgerIndex)
getHashByIndex Returns hash of ledger with given sequence.
Definition: Node.cpp:510
ripple::detail::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(soci::session &session, uint256 const &ledgerHash, beast::Journal j)
getLedgerInfoByHash Returns info of ledger with given hash.
Definition: Node.cpp:499
std::unique_ptr::reset
T reset(T... args)
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::RelationalDatabase::AccountTxPageOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDatabase.h:78
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::detail::saveValidatedLedger
bool saveValidatedLedger(DatabaseCon &ldgDB, DatabaseCon &txnDB, Application &app, std::shared_ptr< Ledger const > const &ledger, bool current)
saveValidatedLedger Saves ledger into database.
Definition: Node.cpp:187
ripple::detail::getLimitedNewestLedgerInfo
std::optional< LedgerInfo > getLimitedNewestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedNewestLedgerInfo Returns info of newest ledger from ledgers with sequences greather or equa...
Definition: Node.cpp:487
ripple::SQLiteDatabaseImp::ledgerDbHasSpace
bool ledgerDbHasSpace(Config const &config) override
ledgerDbHasSpace Checks if the ledger database has available space.
Definition: SQLiteDatabase.cpp:1612
ripple::RelationalDatabase::CountMinMax
Definition: RelationalDatabase.h:51
ripple::base_uint< 256 >
ripple::SQLiteDatabaseImp::makeLedgerDBs
bool makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens ledger and transaction databases for the node store, and stores their descriptors...
Definition: SQLiteDatabase.cpp:444
ripple::SQLiteDatabaseImp::txdb_
std::unique_ptr< DatabaseCon > txdb_
Definition: SQLiteDatabase.cpp:197
ripple::NodeStore::Database::firstLedgerSeq
std::uint32_t firstLedgerSeq(std::uint32_t shardIndex) const noexcept
Calculates the first ledger sequence for a given shard index.
Definition: Database.h:257
ripple::getKBUsedAll
std::uint32_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:130
ripple::detail::getTxHistory
std::pair< std::vector< std::shared_ptr< Transaction > >, int > getTxHistory(soci::session &session, Application &app, LedgerIndex startIndex, int quantity, bool count)
getTxHistory Returns given number of most recent transactions starting from given number of entry.
Definition: Node.cpp:616
ripple::SQLiteDatabaseImp::getAccountTransactionsMinLedgerSeq
std::optional< LedgerIndex > getAccountTransactionsMinLedgerSeq() override
getAccountTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the AccountTransacti...
Definition: SQLiteDatabase.cpp:526
ripple::SQLiteDatabaseImp::getHashByIndex
uint256 getHashByIndex(LedgerIndex ledgerIndex) override
getHashByIndex Returns the hash of the ledger with the given sequence.
Definition: SQLiteDatabase.cpp:955
ripple::SQLiteDatabaseImp::lastLedgerSeq
LedgerIndex lastLedgerSeq(std::uint32_t shardIndex)
lastLedgerSeq Returns the sequence of the last ledger stored in the shard specified by the shard inde...
Definition: SQLiteDatabase.cpp:259
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::SQLiteDatabaseImp::getAccountTransactionCount
std::size_t getAccountTransactionCount() override
getAccountTransactionCount Returns the number of account transactions.
Definition: SQLiteDatabase.cpp:705
ripple::RelationalDatabase::MetaTxsList
std::vector< txnMetaLedgerType > MetaTxsList
Definition: RelationalDatabase.h:88
ripple::Config
Definition: Config.h:89
ripple::NodeStore::DatabaseShard::iterateLedgerSQLsBack
virtual bool iterateLedgerSQLsBack(std::optional< std::uint32_t > maxShardIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)=0
iterateLedgerSQLsBack Checks out ledger databases for all shards in descending order starting from gi...
ripple::SQLiteDatabaseImp::existsTransaction
bool existsTransaction()
existsTransaction Checks if the node store transaction database exists.
Definition: SQLiteDatabase.cpp:280
ripple::NodeStore::DatabaseShard::iterateLedgerSQLsForward
virtual bool iterateLedgerSQLsForward(std::optional< std::uint32_t > minShardIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)=0
iterateLedgerSQLsForward Checks out ledger databases for all shards in ascending order starting from ...
ripple::saveLedgerAsync
void saveLedgerAsync(Application &app, std::uint32_t seq)
Definition: AccountTxPaging.cpp:57
ripple::SQLiteDatabaseImp::newestAccountTxPageB
std::pair< MetaTxsList, std::optional< AccountTxMarker > > newestAccountTxPageB(AccountTxPageOptions const &options) override
newestAccountTxPageB Returns the newest transactions in binary form for the account that matches the ...
Definition: SQLiteDatabase.cpp:1504
ripple::SQLiteDatabaseImp::checkoutTransaction
auto checkoutTransaction()
checkoutTransaction Checks out and returns the node store transaction database.
Definition: SQLiteDatabase.cpp:312
ripple::detail::makeLedgerDBs
DatabasePairValid makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens ledger and transactions databases.
Definition: Node.cpp:67
ripple::SQLiteDatabaseImp::getOldestAccountTxsB
MetaTxsList getOldestAccountTxsB(AccountTxOptions const &options) override
getOldestAccountTxsB Returns the oldest transactions in binary form for the account that matches the ...
Definition: SQLiteDatabase.cpp:1205
ripple::SQLiteDatabaseImp::iterateTransactionForward
bool iterateTransactionForward(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)
iterateTransactionForward Checks out transaction databases for all shards in ascending order starting...
Definition: SQLiteDatabase.cpp:386
ripple::SQLiteDatabaseImp::getTransactionsMinLedgerSeq
std::optional< LedgerIndex > getTransactionsMinLedgerSeq() override
getTransactionsMinLedgerSeq Returns the minimum ledger sequence stored in the Transactions table.
Definition: SQLiteDatabase.cpp:499
ripple::SQLiteDatabaseImp::getNewestAccountTxs
AccountTxs getNewestAccountTxs(AccountTxOptions const &options) override
getNewestAccountTxs Returns the newest transactions for the account that matches the given criteria s...
Definition: SQLiteDatabase.cpp:1145
ripple::Application::logs
virtual Logs & logs()=0
ripple::SQLiteDatabaseImp::getLimitedOldestLedgerInfo
std::optional< LedgerInfo > getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedOldestLedgerInfo Returns the info of the oldest ledger whose sequence number is greater tha...
Definition: SQLiteDatabase.cpp:855
ripple::ValStatus::current
@ current
This was a new validation and was added.
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::SQLiteDatabaseImp::getKBUsedAll
std::uint32_t getKBUsedAll() override
getKBUsedAll Returns the amount of space used by all databases.
Definition: SQLiteDatabase.cpp:1655
ripple::SQLiteDatabaseImp::deleteTransactionsBeforeLedgerSeq
void deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionsBeforeLedgerSeq Deletes all transactions with a sequence number less than or equal ...
Definition: SQLiteDatabase.cpp:625
std::map
STL class.
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::detail::getRows
std::size_t getRows(soci::session &session, TableType type)
getRows Returns number of rows in given table.
Definition: Node.cpp:160
ripple::detail::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(soci::session &session, LedgerIndex ledgerSeq, beast::Journal j)
getLedgerInfoByIndex Returns ledger by its sequence.
Definition: Node.cpp:456
ripple::detail::getShardIndexforLedger
std::optional< std::uint32_t > getShardIndexforLedger(soci::session &session, LedgerHash const &hash)
getShardIndexforLedger Queries the ledger meta database to retrieve the index of the shard that conta...
Definition: app/rdb/backend/detail/impl/Shard.cpp:117
ripple::SQLiteDatabaseImp::getTransaction
std::variant< AccountTx, TxSearched > getTransaction(uint256 const &id, std::optional< ClosedInterval< std::uint32_t >> const &range, error_code_i &ec) override
Definition: SQLiteDatabase.cpp:1563
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::RelationalDatabase::AccountTxOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDatabase.h:68
std::min
T min(T... args)
std::variant::index
T index(T... args)
ripple::SQLiteDatabaseImp
Definition: SQLiteDatabase.cpp:39
ripple::SQLiteDatabaseImp::closeLedgerDB
void closeLedgerDB() override
Closes the ledger database.
Definition: SQLiteDatabase.cpp:1724
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq(soci::session &session, TableType type)
getMinLedgerSeq Returns minimum ledger sequence in given table.
Definition: Node.cpp:123
ripple::SQLiteDatabaseImp::j_
beast::Journal j_
Definition: SQLiteDatabase.cpp:196
ripple::SQLiteDatabaseImp::oldestAccountTxPageB
std::pair< MetaTxsList, std::optional< AccountTxMarker > > oldestAccountTxPageB(AccountTxPageOptions const &options) override
oldestAccountTxPageB Returns the oldest transactions in binary form for the account that matches the ...
Definition: SQLiteDatabase.cpp:1443
ripple::SQLiteDatabaseImp::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo() override
getNewestLedgerInfo Returns the info of the newest saved ledger.
Definition: SQLiteDatabase.cpp:824
ripple::SQLiteDatabaseImp::lgrdb_
std::unique_ptr< DatabaseCon > lgrdb_
Definition: SQLiteDatabase.cpp:197
ripple::SQLiteDatabaseImp::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(uint256 const &ledgerHash) override
getLedgerInfoByHash Returns the info of the ledger with given hash.
Definition: SQLiteDatabase.cpp:922
ripple::NodeStore::Database::seqToShardIndex
std::uint32_t seqToShardIndex(std::uint32_t ledgerSeq) const noexcept
Calculates the shard index for a given ledger sequence.
Definition: Database.h:283
ripple::SQLiteDatabaseImp::checkoutLedger
auto checkoutLedger()
checkoutTransaction Checks out and returns node store ledger database.
Definition: SQLiteDatabase.cpp:301
std::map::insert
T insert(T... args)
ripple::SQLiteDatabaseImp::SQLiteDatabaseImp
SQLiteDatabaseImp(Application &app, Config const &config, JobQueue &jobQueue)
Definition: SQLiteDatabase.cpp:42
ripple::detail::TableType::AccountTransactions
@ AccountTransactions
ripple::SQLiteDatabaseImp::oldestAccountTxPage
std::pair< AccountTxs, std::optional< AccountTxMarker > > oldestAccountTxPage(AccountTxPageOptions const &options) override
oldestAccountTxPage Returns the oldest transactions for the account that matches the given criteria s...
Definition: SQLiteDatabase.cpp:1319
ripple::SQLiteDatabaseImp::deleteTransactionByLedgerSeq
void deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionByLedgerSeq Deletes transactions from the ledger with the given sequence.
Definition: SQLiteDatabase.cpp:578
ripple::SQLiteDatabaseImp::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq() override
getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers table.
Definition: SQLiteDatabase.cpp:472
ripple::SQLiteDatabaseImp::getTxHistory
std::vector< std::shared_ptr< Transaction > > getTxHistory(LedgerIndex startIndex) override
getTxHistory Returns the 20 most recent transactions starting from the given number.
Definition: SQLiteDatabase.cpp:1040
ripple::SQLiteDatabaseImp::doTransaction
bool doTransaction(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)
doTransaction Checks out the transaction database owned by the shard containing the given ledger,...
Definition: SQLiteDatabase.cpp:343
ripple::SQLiteDatabaseImp::useTxTables_
const bool useTxTables_
Definition: SQLiteDatabase.cpp:195
ripple::SQLiteDatabaseImp::closeTransactionDB
void closeTransactionDB() override
Closes the transaction database.
Definition: SQLiteDatabase.cpp:1730
std::optional
std::size_t
ripple::SQLiteDatabaseImp::getLimitedNewestLedgerInfo
std::optional< LedgerInfo > getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedNewestLedgerInfo Returns the info of the newest ledger whose sequence number is greater tha...
Definition: SQLiteDatabase.cpp:889
std::vector::end
T end(T... args)
ripple::SQLiteDatabaseImp::getNewestAccountTxsB
MetaTxsList getNewestAccountTxsB(AccountTxOptions const &options) override
getNewestAccountTxsB Returns the newest transactions in binary form for the account that matches the ...
Definition: SQLiteDatabase.cpp:1261
ripple::SQLiteDatabaseImp::getLedgerCountMinMax
RelationalDatabase::CountMinMax getLedgerCountMinMax() override
getLedgerCountMinMax Returns the minimum ledger sequence, maximum ledger sequence and total number of...
Definition: SQLiteDatabase.cpp:732
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:44
ripple::SQLiteDatabaseImp::iterateTransactionBack
bool iterateTransactionBack(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t shardIndex)> const &callback)
iterateTransactionBack Checks out transaction databases for all shards in descending order starting f...
Definition: SQLiteDatabase.cpp:432
std::max
T max(T... args)
ripple::detail::getOldestAccountTxs
std::pair< RelationalDatabase::AccountTxs, int > getOldestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDatabase::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getOldestAccountTxs Returns oldest transactions for given account which match given criteria starting...
Definition: Node.cpp:899
ripple::detail::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq(soci::session &session, TableType type)
getMaxLedgerSeq Returns maximum ledger sequence in given table.
Definition: Node.cpp:133
ripple::SQLiteDatabaseImp::getTransactionCount
std::size_t getTransactionCount() override
getTransactionCount Returns the number of transactions.
Definition: SQLiteDatabase.cpp:678
std::unique_ptr
STL class.
ripple::detail::getHashesByIndex
std::optional< LedgerHashPair > getHashesByIndex(soci::session &session, LedgerIndex ledgerIndex, beast::Journal j)
getHashesByIndex Returns hash of the ledger and hash of parent ledger for the ledger of given sequenc...
Definition: Node.cpp:540
ripple::SQLiteDatabaseImp::getHashesByIndex
std::optional< LedgerHashPair > getHashesByIndex(LedgerIndex ledgerIndex) override
getHashesByIndex Returns the hashes of the ledger and its parent as specified by the ledgerIndex.
Definition: SQLiteDatabase.cpp:980
ripple::SQLiteDatabaseImp::getOldestAccountTxs
AccountTxs getOldestAccountTxs(AccountTxOptions const &options) override
getOldestAccountTxs Returns the oldest transactions for the account that matches the given criteria s...
Definition: SQLiteDatabase.cpp:1085
ripple::RelationalDatabase::AccountTxPageOptions::marker
std::optional< AccountTxMarker > marker
Definition: RelationalDatabase.h:79
ripple::RelationalDatabase::AccountTxPageOptions
Definition: RelationalDatabase.h:74
std::ref
T ref(T... args)
ripple::getSQLiteDatabase
std::unique_ptr< RelationalDatabase > getSQLiteDatabase(Application &app, Config const &config, JobQueue &jobQueue)
Definition: SQLiteDatabase.cpp:1736
ripple::SQLiteDatabaseImp::shardStoreExists
bool shardStoreExists()
shardStoreExists Checks whether the shard store exists
Definition: SQLiteDatabase.cpp:290
std::variant
ripple::SQLiteDatabaseImp::txMetaDB_
std::unique_ptr< DatabaseCon > txMetaDB_
Definition: SQLiteDatabase.cpp:198