rippled
RelationalDBInterfaceSqlite.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/RelationalDBInterface_nodes.h>
27 #include <ripple/app/rdb/RelationalDBInterface_postgres.h>
28 #include <ripple/app/rdb/RelationalDBInterface_shards.h>
29 #include <ripple/app/rdb/backend/RelationalDBInterfaceSqlite.h>
30 #include <ripple/basics/BasicConfig.h>
31 #include <ripple/basics/StringUtilities.h>
32 #include <ripple/core/DatabaseCon.h>
33 #include <ripple/core/SociDB.h>
34 #include <ripple/json/to_string.h>
35 #include <ripple/nodestore/DatabaseShard.h>
36 #include <boost/algorithm/string.hpp>
37 #include <boost/range/adaptor/transformed.hpp>
38 #include <soci/sqlite3/soci-sqlite3.h>
39 
40 namespace ripple {
41 
43 {
44 public:
46  Application& app,
47  Config const& config,
48  JobQueue& jobQueue)
49  : app_(app), j_(app_.journal("Ledger"))
50  {
51  DatabaseCon::Setup setup = setup_DatabaseCon(config, j_);
52  if (!makeLedgerDBs(
53  config,
54  setup,
56  {
57  JLOG(app_.journal("RelationalDBInterfaceSqlite").fatal())
58  << "AccountTransactions database should not have a primary key";
59  Throw<std::runtime_error>(
60  "AccountTransactions database initialization failed.");
61  }
62 
63  if (app.getShardStore() &&
64  !makeMetaDBs(
65  config,
66  setup,
68  {
69  JLOG(app_.journal("RelationalDBInterfaceSqlite").fatal())
70  << "Error during meta DB init";
71  Throw<std::runtime_error>(
72  "Shard meta database initialization failed.");
73  }
74  }
75 
77  getMinLedgerSeq() override;
78 
80  getTransactionsMinLedgerSeq() override;
81 
84 
86  getMaxLedgerSeq() override;
87 
88  void
89  deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override;
90 
91  void
92  deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
93 
94  void
96 
97  void
99 
101  getTransactionCount() override;
102 
104  getAccountTransactionCount() override;
105 
107  getLedgerCountMinMax() override;
108 
109  bool
111  std::shared_ptr<Ledger const> const& ledger,
112  bool current) override;
113 
115  getLedgerInfoByIndex(LedgerIndex ledgerSeq) override;
116 
118  getNewestLedgerInfo() override;
119 
121  getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
122 
124  getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
125 
127  getLedgerInfoByHash(uint256 const& ledgerHash) override;
128 
129  uint256
130  getHashByIndex(LedgerIndex ledgerIndex) override;
131 
133  getHashesByIndex(LedgerIndex ledgerIndex) override;
134 
136  getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) override;
137 
139  getTxHistory(LedgerIndex startIndex) override;
140 
141  AccountTxs
142  getOldestAccountTxs(AccountTxOptions const& options) override;
143 
144  AccountTxs
145  getNewestAccountTxs(AccountTxOptions const& options) override;
146 
148  getOldestAccountTxsB(AccountTxOptions const& options) override;
149 
151  getNewestAccountTxsB(AccountTxOptions const& options) override;
152 
154  oldestAccountTxPage(AccountTxPageOptions const& options) override;
155 
157  newestAccountTxPage(AccountTxPageOptions const& options) override;
158 
160  oldestAccountTxPageB(AccountTxPageOptions const& options) override;
161 
163  newestAccountTxPageB(AccountTxPageOptions const& options) override;
164 
167  uint256 const& id,
169  error_code_i& ec) override;
170 
171  bool
172  ledgerDbHasSpace(Config const& config) override;
173 
174  bool
175  transactionDbHasSpace(Config const& config) override;
176 
178  getKBUsedAll() override;
179 
181  getKBUsedLedger() override;
182 
184  getKBUsedTransaction() override;
185 
186  void
187  closeLedgerDB() override;
188 
189  void
190  closeTransactionDB() override;
191 
192 private:
197 
206  bool
208  Config const& config,
209  DatabaseCon::Setup const& setup,
210  DatabaseCon::CheckpointerSetup const& checkpointerSetup);
211 
220  bool
221  makeMetaDBs(
222  Config const& config,
223  DatabaseCon::Setup const& setup,
224  DatabaseCon::CheckpointerSetup const& checkpointerSetup);
225 
233  {
234  return app_.getShardStore()->seqToShardIndex(ledgerSeq);
235  }
236 
244  {
245  return app_.getShardStore()->firstLedgerSeq(shardIndex);
246  }
247 
255  {
256  return app_.getShardStore()->lastLedgerSeq(shardIndex);
257  }
258 
263  bool
265  {
266  return !!lgrdb_;
267  }
268 
273  bool
275  {
276  return !!txdb_;
277  }
278 
283  auto
285  {
286  return lgrdb_->checkoutDb();
287  }
288 
293  auto
295  {
296  return txdb_->checkoutDb();
297  }
298 
307  bool
309  LedgerIndex ledgerSeq,
310  std::function<bool(soci::session& session)> const& callback)
311  {
313  ledgerSeq, callback);
314  }
315 
324  bool
326  LedgerIndex ledgerSeq,
327  std::function<bool(soci::session& session)> const& callback)
328  {
330  ledgerSeq, callback);
331  }
332 
344  bool
346  std::optional<std::uint32_t> firstIndex,
348  bool(soci::session& session, std::uint32_t shardIndex)> const&
349  callback)
350  {
352  firstIndex, callback);
353  }
354 
366  bool
368  std::optional<std::uint32_t> firstIndex,
370  bool(soci::session& session, std::uint32_t shardIndex)> const&
371  callback)
372  {
374  firstIndex, callback);
375  }
376 
388  bool
390  std::optional<std::uint32_t> firstIndex,
392  bool(soci::session& session, std::uint32_t shardIndex)> const&
393  callback)
394  {
396  firstIndex, callback);
397  }
398 
410  bool
412  std::optional<std::uint32_t> firstIndex,
414  bool(soci::session& session, std::uint32_t shardIndex)> const&
415  callback)
416  {
418  firstIndex, callback);
419  }
420 };
421 
422 bool
424  Config const& config,
425  DatabaseCon::Setup const& setup,
426  DatabaseCon::CheckpointerSetup const& checkpointerSetup)
427 {
428  auto [lgr, tx, res] =
429  ripple::makeLedgerDBs(config, setup, checkpointerSetup);
430  txdb_ = std::move(tx);
431  lgrdb_ = std::move(lgr);
432  return res;
433 }
434 
435 bool
437  Config const& config,
438  DatabaseCon::Setup const& setup,
439  DatabaseCon::CheckpointerSetup const& checkpointerSetup)
440 {
441  auto [lgrMetaDB, txMetaDB] =
442  ripple::makeMetaDBs(config, setup, checkpointerSetup);
443 
444  txMetaDB_ = std::move(txMetaDB);
445  lgrMetaDB_ = std::move(lgrMetaDB);
446 
447  return true;
448 }
449 
452 {
453  /* if databases exists, use it */
454  if (existsLedger())
455  {
456  auto db = checkoutLedger();
458  }
459 
460  /* else use shard databases */
463  {}, [&](soci::session& session, std::uint32_t shardIndex) {
465  return !res;
466  });
467  return res;
468 }
469 
472 {
473  /* if databases exists, use it */
474  if (existsTransaction())
475  {
476  auto db = checkoutTransaction();
478  }
479 
480  /* else use shard databases */
483  {}, [&](soci::session& session, std::uint32_t shardIndex) {
485  return !res;
486  });
487  return res;
488 }
489 
492 {
493  /* if databases exists, use it */
494  if (existsTransaction())
495  {
496  auto db = checkoutTransaction();
498  }
499 
500  /* else use shard databases */
503  {}, [&](soci::session& session, std::uint32_t shardIndex) {
506  return !res;
507  });
508  return res;
509 }
510 
513 {
514  /* if databases exists, use it */
515  if (existsLedger())
516  {
517  auto db = checkoutLedger();
519  }
520 
521  /* else use shard databases */
524  {}, [&](soci::session& session, std::uint32_t shardIndex) {
526  return !res;
527  });
528  return res;
529 }
530 
531 void
533  LedgerIndex ledgerSeq)
534 {
535  /* if database exists, use it */
536  if (existsTransaction())
537  {
538  auto db = checkoutTransaction();
540  return;
541  }
542 
543  /* else use shard database */
544  doTransaction(ledgerSeq, [&](soci::session& session) {
546  return true;
547  });
548 }
549 
550 void
552 {
553  /* if database exists, use it */
554  if (existsLedger())
555  {
556  auto db = checkoutLedger();
558  return;
559  }
560 
561  /* else use shard databases */
563  seqToShardIndex(ledgerSeq),
564  [&](soci::session& session, std::uint32_t shardIndex) {
566  session, TableType::Ledgers, ledgerSeq);
567  return true;
568  });
569 }
570 
571 void
573  LedgerIndex ledgerSeq)
574 {
575  /* if database exists, use it */
576  if (existsTransaction())
577  {
578  auto db = checkoutTransaction();
580  return;
581  }
582 
583  /* else use shard databases */
585  seqToShardIndex(ledgerSeq),
586  [&](soci::session& session, std::uint32_t shardIndex) {
588  session, TableType::Transactions, ledgerSeq);
589  return true;
590  });
591 }
592 
593 void
595  LedgerIndex ledgerSeq)
596 {
597  /* if database exists, use it */
598  if (existsTransaction())
599  {
600  auto db = checkoutTransaction();
602  *db, TableType::AccountTransactions, ledgerSeq);
603  return;
604  }
605 
606  /* else use shard databases */
608  seqToShardIndex(ledgerSeq),
609  [&](soci::session& session, std::uint32_t shardIndex) {
611  session, TableType::AccountTransactions, ledgerSeq);
612  return true;
613  });
614 }
615 
618 {
619  /* if database exists, use it */
620  if (existsTransaction())
621  {
622  auto db = checkoutTransaction();
624  }
625 
626  /* else use shard databases */
627  std::size_t rows = 0;
629  {}, [&](soci::session& session, std::uint32_t shardIndex) {
630  rows += ripple::getRows(session, TableType::Transactions);
631  return true;
632  });
633  return rows;
634 }
635 
638 {
639  /* if database exists, use it */
640  if (existsTransaction())
641  {
642  auto db = checkoutTransaction();
644  }
645 
646  /* else use shard databases */
647  std::size_t rows = 0;
649  {}, [&](soci::session& session, std::uint32_t shardIndex) {
651  return true;
652  });
653  return rows;
654 }
655 
658 {
659  /* if database exists, use it */
660  if (existsLedger())
661  {
662  auto db = checkoutLedger();
664  }
665 
666  /* else use shard databases */
667  CountMinMax res{0, 0, 0};
669  {}, [&](soci::session& session, std::uint32_t shardIndex) {
670  auto r = ripple::getRowsMinMax(session, TableType::Ledgers);
671  if (r.numberOfRows)
672  {
673  res.numberOfRows += r.numberOfRows;
674  if (res.minLedgerSequence == 0)
675  res.minLedgerSequence = r.minLedgerSequence;
676  res.maxLedgerSequence = r.maxLedgerSequence;
677  }
678  return true;
679  });
680  return res;
681 }
682 
683 bool
685  std::shared_ptr<Ledger const> const& ledger,
686  bool current)
687 {
688  /* if databases exists, use them */
689  if (existsLedger() && existsTransaction())
690  {
692  *lgrdb_, *txdb_, app_, ledger, current))
693  return false;
694  }
695 
696  if (auto shardStore = app_.getShardStore())
697  {
698  if (ledger->info().seq < shardStore->earliestLedgerSeq())
699  // For the moment return false only when the ShardStore
700  // should accept the ledger, but fails when attempting
701  // to do so, i.e. when saveLedgerMeta fails. Later when
702  // the ShardStore supercedes the NodeStore, change this
703  // line to return false if the ledger is too early.
704  return true;
705 
706  auto lgrMetaSession = lgrMetaDB_->checkoutDb();
707  auto txMetaSession = txMetaDB_->checkoutDb();
708 
709  return ripple::saveLedgerMeta(
710  ledger,
711  app_,
712  *lgrMetaSession,
713  *txMetaSession,
714  shardStore->seqToShardIndex(ledger->info().seq));
715  }
716 
717  return true;
718 }
719 
722 {
723  /* if database exists, use it */
724  if (existsLedger())
725  {
726  auto db = checkoutLedger();
727  return ripple::getLedgerInfoByIndex(*db, ledgerSeq, j_);
728  }
729 
730  /* else use shard database */
732  doLedger(ledgerSeq, [&](soci::session& session) {
733  res = ripple::getLedgerInfoByIndex(session, ledgerSeq, j_);
734  return true;
735  });
736  return res;
737 }
738 
741 {
742  /* if database exists, use it */
743  if (existsLedger())
744  {
745  auto db = checkoutLedger();
746  return ripple::getNewestLedgerInfo(*db, j_);
747  }
748 
749  /* else use shard databases */
752  {}, [&](soci::session& session, std::uint32_t shardIndex) {
753  if (auto info = ripple::getNewestLedgerInfo(session, j_))
754  {
755  res = info;
756  return false;
757  }
758  return true;
759  });
760 
761  return res;
762 }
763 
766  LedgerIndex ledgerFirstIndex)
767 {
768  /* if database exists, use it */
769  if (existsLedger())
770  {
771  auto db = checkoutLedger();
772  return ripple::getLimitedOldestLedgerInfo(*db, ledgerFirstIndex, j_);
773  }
774 
775  /* else use shard databases */
778  seqToShardIndex(ledgerFirstIndex),
779  [&](soci::session& session, std::uint32_t shardIndex) {
780  if (auto info = ripple::getLimitedOldestLedgerInfo(
781  session, ledgerFirstIndex, j_))
782  {
783  res = info;
784  return false;
785  }
786  return true;
787  });
788 
789  return res;
790 }
791 
794  LedgerIndex ledgerFirstIndex)
795 {
796  /* if database exists, use it */
797  if (existsLedger())
798  {
799  auto db = checkoutLedger();
800  return ripple::getLimitedNewestLedgerInfo(*db, ledgerFirstIndex, j_);
801  }
802 
803  /* else use shard databases */
806  {}, [&](soci::session& session, std::uint32_t shardIndex) {
807  if (auto info = ripple::getLimitedNewestLedgerInfo(
808  session, ledgerFirstIndex, j_))
809  {
810  res = info;
811  return false;
812  }
813  return shardIndex >= seqToShardIndex(ledgerFirstIndex);
814  });
815 
816  return res;
817 }
818 
821 {
822  /* if database exists, use it */
823  if (existsLedger())
824  {
825  auto db = checkoutLedger();
826  return ripple::getLedgerInfoByHash(*db, ledgerHash, j_);
827  }
828 
829  /* else use shard databases */
830  if (auto shardStore = app_.getShardStore())
831  {
833  auto lgrMetaSession = lgrMetaDB_->checkoutDb();
834 
835  if (auto const shardIndex =
836  ripple::getShardIndexforLedger(*lgrMetaSession, ledgerHash))
837  {
838  shardStore->callForLedgerSQLByShardIndex(
839  *shardIndex, [&](soci::session& session) {
840  res = ripple::getLedgerInfoByHash(session, ledgerHash, j_);
841  return false; // unused
842  });
843  }
844 
845  return res;
846  }
847 
848  return {};
849 }
850 
851 uint256
853 {
854  /* if database exists, use it */
855  if (existsLedger())
856  {
857  auto db = checkoutLedger();
858  return ripple::getHashByIndex(*db, ledgerIndex);
859  }
860 
861  /* else use shard database */
862  uint256 hash;
863  doLedger(ledgerIndex, [&](soci::session& session) {
864  hash = ripple::getHashByIndex(session, ledgerIndex);
865  return true;
866  });
867  return hash;
868 }
869 
872 {
873  /* if database exists, use it */
874  if (existsLedger())
875  {
876  auto db = checkoutLedger();
877  return ripple::getHashesByIndex(*db, ledgerIndex, j_);
878  }
879 
880  /* else use shard database */
882  doLedger(ledgerIndex, [&](soci::session& session) {
883  res = ripple::getHashesByIndex(session, ledgerIndex, j_);
884  return true;
885  });
886  return res;
887 }
888 
891  LedgerIndex minSeq,
892  LedgerIndex maxSeq)
893 {
894  /* if database exists, use it */
895  if (existsLedger())
896  {
897  auto db = checkoutLedger();
898  return ripple::getHashesByIndex(*db, minSeq, maxSeq, j_);
899  }
900 
901  /* else use shard databases */
903  while (minSeq <= maxSeq)
904  {
905  LedgerIndex shardMaxSeq = lastLedgerSeq(seqToShardIndex(minSeq));
906  if (shardMaxSeq > maxSeq)
907  shardMaxSeq = maxSeq;
908  doLedger(minSeq, [&](soci::session& session) {
909  auto r = ripple::getHashesByIndex(session, minSeq, shardMaxSeq, j_);
910  res.insert(r.begin(), r.end());
911  return true;
912  });
913  minSeq = shardMaxSeq + 1;
914  }
915 
916  return res;
917 }
918 
921 {
922  /* if database exists, use it */
923  if (existsTransaction())
924  {
925  auto db = checkoutTransaction();
926  return ripple::getTxHistory(*db, app_, startIndex, 20, false).first;
927  }
928 
929  /* else use shard databases */
931  int quantity = 20;
933  {}, [&](soci::session& session, std::uint32_t shardIndex) {
934  auto [tx, total] =
935  ripple::getTxHistory(session, app_, startIndex, quantity, true);
936  txs.insert(txs.end(), tx.begin(), tx.end());
937  if (total > 0)
938  {
939  quantity -= total;
940  if (quantity <= 0)
941  return false;
942  startIndex = 0;
943  }
944  else
945  {
946  startIndex += total;
947  }
948  return true;
949  });
950 
951  return txs;
952 }
953 
956  AccountTxOptions const& options)
957 {
959 
960  /* if database exists, use it */
961  if (existsTransaction())
962  {
963  auto db = checkoutTransaction();
965  *db, app_, ledgerMaster, options, {}, j_)
966  .first;
967  }
968 
969  /* else use shard databases */
970  AccountTxs ret;
971  AccountTxOptions opt = options;
972  int limit_used = 0;
976  [&](soci::session& session, std::uint32_t shardIndex) {
977  if (opt.maxLedger && shardIndex > seqToShardIndex(opt.maxLedger))
978  return false;
979  auto [r, total] = ripple::getOldestAccountTxs(
980  session, app_, ledgerMaster, opt, limit_used, j_);
981  ret.insert(ret.end(), r.begin(), r.end());
982  if (!total)
983  return false;
984  if (total > 0)
985  {
986  limit_used += total;
987  opt.offset = 0;
988  }
989  else
990  {
991  total = ~total;
992  if (opt.offset <= total)
993  opt.offset = 0;
994  else
995  opt.offset -= total;
996  }
997  return true;
998  });
999 
1000  return ret;
1001 }
1002 
1003 RelationalDBInterface::AccountTxs
1004 RelationalDBInterfaceSqliteImp::getNewestAccountTxs(
1005  AccountTxOptions const& options)
1006 {
1008 
1009  /* if database exists, use it */
1010  if (existsTransaction())
1011  {
1012  auto db = checkoutTransaction();
1014  *db, app_, ledgerMaster, options, {}, j_)
1015  .first;
1016  }
1017 
1018  /* else use shard databases */
1019  AccountTxs ret;
1020  AccountTxOptions opt = options;
1021  int limit_used = 0;
1022  iterateTransactionBack(
1023  opt.maxLedger ? seqToShardIndex(opt.maxLedger)
1025  [&](soci::session& session, std::uint32_t shardIndex) {
1026  if (opt.minLedger && shardIndex < seqToShardIndex(opt.minLedger))
1027  return false;
1028  auto [r, total] = ripple::getNewestAccountTxs(
1029  session, app_, ledgerMaster, opt, limit_used, j_);
1030  ret.insert(ret.end(), r.begin(), r.end());
1031  if (!total)
1032  return false;
1033  if (total > 0)
1034  {
1035  limit_used += total;
1036  opt.offset = 0;
1037  }
1038  else
1039  {
1040  total = ~total;
1041  if (opt.offset <= total)
1042  opt.offset = 0;
1043  else
1044  opt.offset -= total;
1045  }
1046  return true;
1047  });
1048 
1049  return ret;
1050 }
1051 
1052 RelationalDBInterface::MetaTxsList
1053 RelationalDBInterfaceSqliteImp::getOldestAccountTxsB(
1054  AccountTxOptions const& options)
1055 {
1056  /* if database exists, use it */
1057  if (existsTransaction())
1058  {
1059  auto db = checkoutTransaction();
1060  return ripple::getOldestAccountTxsB(*db, app_, options, {}, j_).first;
1061  }
1062 
1063  /* else use shard databases */
1064  MetaTxsList ret;
1065  AccountTxOptions opt = options;
1066  int limit_used = 0;
1067  iterateTransactionForward(
1068  opt.minLedger ? seqToShardIndex(opt.minLedger)
1070  [&](soci::session& session, std::uint32_t shardIndex) {
1071  if (opt.maxLedger && shardIndex > seqToShardIndex(opt.maxLedger))
1072  return false;
1073  auto [r, total] = ripple::getOldestAccountTxsB(
1074  session, app_, opt, limit_used, j_);
1075  ret.insert(ret.end(), r.begin(), r.end());
1076  if (!total)
1077  return false;
1078  if (total > 0)
1079  {
1080  limit_used += total;
1081  opt.offset = 0;
1082  }
1083  else
1084  {
1085  total = ~total;
1086  if (opt.offset <= total)
1087  opt.offset = 0;
1088  else
1089  opt.offset -= total;
1090  }
1091  return true;
1092  });
1093 
1094  return ret;
1095 }
1096 
1097 RelationalDBInterface::MetaTxsList
1098 RelationalDBInterfaceSqliteImp::getNewestAccountTxsB(
1099  AccountTxOptions const& options)
1100 {
1101  /* if database exists, use it */
1102  if (existsTransaction())
1103  {
1104  auto db = checkoutTransaction();
1105  return ripple::getNewestAccountTxsB(*db, app_, options, {}, j_).first;
1106  }
1107 
1108  /* else use shard databases */
1109  MetaTxsList ret;
1110  AccountTxOptions opt = options;
1111  int limit_used = 0;
1112  iterateTransactionBack(
1113  opt.maxLedger ? seqToShardIndex(opt.maxLedger)
1115  [&](soci::session& session, std::uint32_t shardIndex) {
1116  if (opt.minLedger && shardIndex < seqToShardIndex(opt.minLedger))
1117  return false;
1118  auto [r, total] = ripple::getNewestAccountTxsB(
1119  session, app_, opt, limit_used, j_);
1120  ret.insert(ret.end(), r.begin(), r.end());
1121  if (!total)
1122  return false;
1123  if (total > 0)
1124  {
1125  limit_used += total;
1126  opt.offset = 0;
1127  }
1128  else
1129  {
1130  total = ~total;
1131  if (opt.offset <= total)
1132  opt.offset = 0;
1133  else
1134  opt.offset -= total;
1135  }
1136  return true;
1137  });
1138 
1139  return ret;
1140 }
1141 
1142 std::pair<
1143  RelationalDBInterface::AccountTxs,
1145 RelationalDBInterfaceSqliteImp::oldestAccountTxPage(
1146  AccountTxPageOptions const& options)
1147 {
1148  static std::uint32_t const page_length(200);
1149  auto& idCache = app_.accountIDCache();
1150  auto onUnsavedLedger =
1151  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1152  AccountTxs ret;
1153  Application& app = app_;
1154  auto onTransaction = [&ret, &app](
1155  std::uint32_t ledger_index,
1156  std::string const& status,
1157  Blob&& rawTxn,
1158  Blob&& rawMeta) {
1159  convertBlobsToTxResult(ret, ledger_index, status, rawTxn, rawMeta, app);
1160  };
1161 
1162  /* if database exists, use it */
1163  if (existsTransaction())
1164  {
1165  auto db = checkoutTransaction();
1166  auto newmarker = ripple::oldestAccountTxPage(
1167  *db,
1168  idCache,
1169  onUnsavedLedger,
1170  onTransaction,
1171  options,
1172  0,
1173  page_length)
1174  .first;
1175  return {ret, newmarker};
1176  }
1177 
1178  /* else use shard databases */
1179  AccountTxPageOptions opt = options;
1180  int limit_used = 0;
1181  iterateTransactionForward(
1182  opt.minLedger ? seqToShardIndex(opt.minLedger)
1184  [&](soci::session& session, std::uint32_t shardIndex) {
1185  if (opt.maxLedger != UINT32_MAX &&
1186  shardIndex > seqToShardIndex(opt.minLedger))
1187  return false;
1188  auto [marker, total] = ripple::oldestAccountTxPage(
1189  session,
1190  idCache,
1191  onUnsavedLedger,
1192  onTransaction,
1193  opt,
1194  limit_used,
1195  page_length);
1196  opt.marker = marker;
1197  if (total < 0)
1198  return false;
1199  limit_used += total;
1200  return true;
1201  });
1202 
1203  return {ret, opt.marker};
1204 }
1205 
1206 std::pair<
1209 RelationalDBInterfaceSqliteImp::newestAccountTxPage(
1210  AccountTxPageOptions const& options)
1211 {
1212  static std::uint32_t const page_length(200);
1213  auto& idCache = app_.accountIDCache();
1214  auto onUnsavedLedger =
1215  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1216  AccountTxs ret;
1217  Application& app = app_;
1218  auto onTransaction = [&ret, &app](
1219  std::uint32_t ledger_index,
1220  std::string const& status,
1221  Blob&& rawTxn,
1222  Blob&& rawMeta) {
1223  convertBlobsToTxResult(ret, ledger_index, status, rawTxn, rawMeta, app);
1224  };
1225 
1226  /* if database exists, use it */
1227  if (existsTransaction())
1228  {
1229  auto db = checkoutTransaction();
1230  auto newmarker = ripple::newestAccountTxPage(
1231  *db,
1232  idCache,
1233  onUnsavedLedger,
1234  onTransaction,
1235  options,
1236  0,
1237  page_length)
1238  .first;
1239  return {ret, newmarker};
1240  }
1241 
1242  /* else use shard databases */
1243  AccountTxPageOptions opt = options;
1244  int limit_used = 0;
1245  iterateTransactionBack(
1246  opt.maxLedger != UINT32_MAX ? seqToShardIndex(opt.maxLedger)
1248  [&](soci::session& session, std::uint32_t shardIndex) {
1249  if (opt.minLedger && shardIndex < seqToShardIndex(opt.minLedger))
1250  return false;
1251  auto [marker, total] = ripple::newestAccountTxPage(
1252  session,
1253  idCache,
1254  onUnsavedLedger,
1255  onTransaction,
1256  opt,
1257  limit_used,
1258  page_length);
1259  opt.marker = marker;
1260  if (total < 0)
1261  return false;
1262  limit_used += total;
1263  return true;
1264  });
1265 
1266  return {ret, opt.marker};
1267 }
1268 
1269 std::pair<
1272 RelationalDBInterfaceSqliteImp::oldestAccountTxPageB(
1273  AccountTxPageOptions const& options)
1274 {
1275  static std::uint32_t const page_length(500);
1276  auto& idCache = app_.accountIDCache();
1277  auto onUnsavedLedger =
1278  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1279  MetaTxsList ret;
1280  auto onTransaction = [&ret](
1281  std::uint32_t ledgerIndex,
1282  std::string const& status,
1283  Blob&& rawTxn,
1284  Blob&& rawMeta) {
1285  ret.emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1286  };
1287 
1288  /* if database exists, use it */
1289  if (existsTransaction())
1290  {
1291  auto db = checkoutTransaction();
1292  auto newmarker = ripple::oldestAccountTxPage(
1293  *db,
1294  idCache,
1295  onUnsavedLedger,
1296  onTransaction,
1297  options,
1298  0,
1299  page_length)
1300  .first;
1301  return {ret, newmarker};
1302  }
1303 
1304  /* else use shard databases */
1305  AccountTxPageOptions opt = options;
1306  int limit_used = 0;
1307  iterateTransactionForward(
1308  opt.minLedger ? seqToShardIndex(opt.minLedger)
1310  [&](soci::session& session, std::uint32_t shardIndex) {
1311  if (opt.maxLedger != UINT32_MAX &&
1312  shardIndex > seqToShardIndex(opt.minLedger))
1313  return false;
1314  auto [marker, total] = ripple::oldestAccountTxPage(
1315  session,
1316  idCache,
1317  onUnsavedLedger,
1318  onTransaction,
1319  opt,
1320  limit_used,
1321  page_length);
1322  opt.marker = marker;
1323  if (total < 0)
1324  return false;
1325  limit_used += total;
1326  return true;
1327  });
1328 
1329  return {ret, opt.marker};
1330 }
1331 
1332 std::pair<
1335 RelationalDBInterfaceSqliteImp::newestAccountTxPageB(
1336  AccountTxPageOptions const& options)
1337 {
1338  static std::uint32_t const page_length(500);
1339  auto& idCache = app_.accountIDCache();
1340  auto onUnsavedLedger =
1341  std::bind(saveLedgerAsync, std::ref(app_), std::placeholders::_1);
1342  MetaTxsList ret;
1343  auto onTransaction = [&ret](
1344  std::uint32_t ledgerIndex,
1345  std::string const& status,
1346  Blob&& rawTxn,
1347  Blob&& rawMeta) {
1348  ret.emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1349  };
1350 
1351  /* if database exists, use it */
1352  if (existsTransaction())
1353  {
1354  auto db = checkoutTransaction();
1355  auto newmarker = ripple::newestAccountTxPage(
1356  *db,
1357  idCache,
1358  onUnsavedLedger,
1359  onTransaction,
1360  options,
1361  0,
1362  page_length)
1363  .first;
1364  return {ret, newmarker};
1365  }
1366 
1367  /* else use shard databases */
1368  AccountTxPageOptions opt = options;
1369  int limit_used = 0;
1370  iterateTransactionBack(
1371  opt.maxLedger != UINT32_MAX ? seqToShardIndex(opt.maxLedger)
1373  [&](soci::session& session, std::uint32_t shardIndex) {
1374  if (opt.minLedger && shardIndex < seqToShardIndex(opt.minLedger))
1375  return false;
1376  auto [marker, total] = ripple::newestAccountTxPage(
1377  session,
1378  idCache,
1379  onUnsavedLedger,
1380  onTransaction,
1381  opt,
1382  limit_used,
1383  page_length);
1384  opt.marker = marker;
1385  if (total < 0)
1386  return false;
1387  limit_used += total;
1388  return true;
1389  });
1390 
1391  return {ret, opt.marker};
1392 }
1393 
1395 RelationalDBInterfaceSqliteImp::getTransaction(
1396  uint256 const& id,
1398  error_code_i& ec)
1399 {
1400  /* if database exists, use it */
1401  if (existsTransaction())
1402  {
1403  auto db = checkoutTransaction();
1404  return ripple::getTransaction(*db, app_, id, range, ec);
1405  }
1406 
1407  /* else use shard databases */
1408  if (auto shardStore = app_.getShardStore())
1409  {
1410  std::variant<AccountTx, TxSearched> res(TxSearched::unknown);
1411  auto txMetaSession = txMetaDB_->checkoutDb();
1412 
1413  if (auto const shardIndex =
1414  ripple::getShardIndexforTransaction(*txMetaSession, id))
1415  {
1416  shardStore->callForTransactionSQLByShardIndex(
1417  *shardIndex, [&](soci::session& session) {
1419  if (range)
1420  {
1421  std::uint32_t const low = std::max(
1422  range->lower(), firstLedgerSeq(*shardIndex));
1423  std::uint32_t const high = std::min(
1424  range->upper(), lastLedgerSeq(*shardIndex));
1425  if (low <= high)
1426  range1 = ClosedInterval<std::uint32_t>(low, high);
1427  }
1428  res = ripple::getTransaction(session, app_, id, range1, ec);
1429 
1430  return res.index() == 1 &&
1431  std::get<TxSearched>(res) !=
1432  TxSearched::unknown; // unused
1433  });
1434  }
1435 
1436  return res;
1437  }
1438 
1439  return {TxSearched::unknown};
1440 }
1441 
1442 bool
1443 RelationalDBInterfaceSqliteImp::ledgerDbHasSpace(Config const& config)
1444 {
1445  /* if database exists, use it */
1446  if (existsLedger())
1447  {
1448  auto db = checkoutLedger();
1449  return ripple::dbHasSpace(*db, config, j_);
1450  }
1451 
1452  /* else use shard databases */
1453  return iterateLedgerBack(
1454  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1455  return ripple::dbHasSpace(session, config, j_);
1456  });
1457 }
1458 
1459 bool
1460 RelationalDBInterfaceSqliteImp::transactionDbHasSpace(Config const& config)
1461 {
1462  /* if database exists, use it */
1463  if (existsTransaction())
1464  {
1465  auto db = checkoutTransaction();
1466  return ripple::dbHasSpace(*db, config, j_);
1467  }
1468 
1469  /* else use shard databases */
1470  return iterateTransactionBack(
1471  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1472  return ripple::dbHasSpace(session, config, j_);
1473  });
1474 }
1475 
1477 RelationalDBInterfaceSqliteImp::getKBUsedAll()
1478 {
1479  /* if database exists, use it */
1480  if (existsLedger())
1481  {
1482  return ripple::getKBUsedAll(lgrdb_->getSession());
1483  }
1484 
1485  /* else use shard databases */
1486  std::uint32_t sum = 0;
1487  iterateLedgerBack(
1488  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1489  sum += ripple::getKBUsedAll(session);
1490  return true;
1491  });
1492  return sum;
1493 }
1494 
1496 RelationalDBInterfaceSqliteImp::getKBUsedLedger()
1497 {
1498  /* if database exists, use it */
1499  if (existsLedger())
1500  {
1501  return ripple::getKBUsedDB(lgrdb_->getSession());
1502  }
1503 
1504  /* else use shard databases */
1505  std::uint32_t sum = 0;
1506  iterateLedgerBack(
1507  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1508  sum += ripple::getKBUsedDB(session);
1509  return true;
1510  });
1511  return sum;
1512 }
1513 
1515 RelationalDBInterfaceSqliteImp::getKBUsedTransaction()
1516 {
1517  /* if database exists, use it */
1518  if (existsTransaction())
1519  {
1520  return ripple::getKBUsedDB(txdb_->getSession());
1521  }
1522 
1523  /* else use shard databases */
1524  std::uint32_t sum = 0;
1525  iterateTransactionBack(
1526  {}, [&](soci::session& session, std::uint32_t shardIndex) {
1527  sum += ripple::getKBUsedDB(session);
1528  return true;
1529  });
1530  return sum;
1531 }
1532 
1533 void
1534 RelationalDBInterfaceSqliteImp::closeLedgerDB()
1535 {
1536  lgrdb_.reset();
1537 }
1538 
1539 void
1540 RelationalDBInterfaceSqliteImp::closeTransactionDB()
1541 {
1542  txdb_.reset();
1543 }
1544 
1547  Application& app,
1548  Config const& config,
1549  JobQueue& jobQueue)
1550 {
1551  return std::make_unique<RelationalDBInterfaceSqliteImp>(
1552  app, config, jobQueue);
1553 }
1554 
1555 } // namespace ripple
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::RelationalDBInterfaceSqliteImp::deleteTransactionByLedgerSeq
void deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionByLedgerSeq Deletes transactions from ledger with given sequence.
Definition: RelationalDBInterfaceSqlite.cpp:532
ripple::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(soci::session &session, LedgerIndex ledgerSeq, beast::Journal j)
getLedgerInfoByIndex Returns ledger by its sequence.
Definition: RelationalDBInterface_nodes.cpp:455
ripple::Application
Definition: Application.h:103
ripple::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: RelationalDBInterface_nodes.cpp:148
ripple::HashPrefix::ledgerMaster
@ ledgerMaster
ledger master data for signing
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::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: RelationalDBInterface_nodes.cpp:615
ripple::getHashByIndex
uint256 getHashByIndex(soci::session &session, LedgerIndex ledgerIndex)
getHashByIndex Returns hash of ledger with given sequence.
Definition: RelationalDBInterface_nodes.cpp:509
ripple::getRelationalDBInterfaceSqlite
std::unique_ptr< RelationalDBInterface > getRelationalDBInterfaceSqlite(Application &app, Config const &config, JobQueue &jobQueue)
Definition: RelationalDBInterfaceSqlite.cpp:1546
ripple::getOldestAccountTxs
std::pair< RelationalDBInterface::AccountTxs, int > getOldestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDBInterface::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: RelationalDBInterface_nodes.cpp:893
ripple::RelationalDBInterfaceSqliteImp::newestAccountTxPageB
std::pair< MetaTxsList, std::optional< AccountTxMarker > > newestAccountTxPageB(AccountTxPageOptions const &options) override
newestAccountTxPageB Returns newest transactions in binary form for given account which match given c...
Definition: RelationalDBInterfaceSqlite.cpp:1335
ripple::RelationalDBInterfaceSqliteImp::txMetaDB_
std::unique_ptr< DatabaseCon > txMetaDB_
Definition: RelationalDBInterfaceSqlite.cpp:196
ripple::DatabaseCon::Setup
Definition: DatabaseCon.h:84
ripple::getOldestAccountTxsB
std::pair< std::vector< RelationalDBInterface::txnMetaLedgerType >, int > getOldestAccountTxsB(soci::session &session, Application &app, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getOldestAccountTxsB Returns oldest transactions in binary form for given account which match given c...
Definition: RelationalDBInterface_nodes.cpp:1010
ripple::RelationalDBInterfaceSqliteImp::getNewestAccountTxsB
MetaTxsList getNewestAccountTxsB(AccountTxOptions const &options) override
getNewestAccountTxsB Returns newest transactions in binary form for given account which match given c...
Definition: RelationalDBInterfaceSqlite.cpp:1098
ripple::RelationalDBInterfaceSqliteImp::lgrdb_
std::unique_ptr< DatabaseCon > lgrdb_
Definition: RelationalDBInterfaceSqlite.cpp:195
std::pair
ripple::RelationalDBInterfaceSqliteImp::deleteAccountTransactionsBeforeLedgerSeq
void deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteAccountTransactionsBeforeLedgerSeq Deletes all account transactions with given ledger sequence ...
Definition: RelationalDBInterfaceSqlite.cpp:594
ripple::LedgerMaster
Definition: LedgerMaster.h:70
ripple::RelationalDBInterface::AccountTxOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDBInterface.h:68
ripple::RelationalDBInterfaceSqliteImp::oldestAccountTxPage
std::pair< AccountTxs, std::optional< AccountTxMarker > > oldestAccountTxPage(AccountTxPageOptions const &options) override
oldestAccountTxPage Returns oldest transactions for given account which match given criteria starting...
Definition: RelationalDBInterfaceSqlite.cpp:1145
ripple::RelationalDBInterfaceSqliteImp::oldestAccountTxPageB
std::pair< MetaTxsList, std::optional< AccountTxMarker > > oldestAccountTxPageB(AccountTxPageOptions const &options) override
oldestAccountTxPageB Returns oldest transactions in binary form for given account which match given c...
Definition: RelationalDBInterfaceSqlite.cpp:1272
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::RelationalDBInterfaceSqliteImp::iterateTransactionBack
bool iterateTransactionBack(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 descending order startin...
Definition: RelationalDBInterfaceSqlite.cpp:411
ripple::getRowsMinMax
RelationalDBInterface::CountMinMax getRowsMinMax(soci::session &session, TableType type)
getRowsMinMax Returns minumum ledger sequence, maximum ledger sequence and total number of rows in gi...
Definition: RelationalDBInterface_nodes.cpp:170
ripple::DatabaseCon::CheckpointerSetup
Definition: DatabaseCon.h:107
ripple::newestAccountTxPage
std::pair< std::optional< RelationalDBInterface::AccountTxMarker >, int > newestAccountTxPage(soci::session &session, AccountIDCache const &idCache, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDBInterface::AccountTxPageOptions const &options, int limit_used, std::uint32_t page_length)
newestAccountTxPage Searches newest transactions for given account which match given criteria startin...
Definition: RelationalDBInterface_nodes.cpp:1239
ripple::getKBUsedDB
std::uint32_t getKBUsedDB(soci::session &s)
Definition: SociDB.cpp:139
ripple::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(soci::session &session, uint256 const &ledgerHash, beast::Journal j)
getLedgerInfoByHash Returns info of ledger with given hash.
Definition: RelationalDBInterface_nodes.cpp:498
ripple::RelationalDBInterfaceSqliteImp::j_
beast::Journal j_
Definition: RelationalDBInterfaceSqlite.cpp:194
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::RelationalDBInterfaceSqliteImp::getAccountTransactionsMinLedgerSeq
std::optional< LedgerIndex > getAccountTransactionsMinLedgerSeq() override
getAccountTransactionsMinLedgerSeq Returns minimum ledger sequence among records in the AccountTransa...
Definition: RelationalDBInterfaceSqlite.cpp:491
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::RelationalDBInterfaceSqliteImp::lgrMetaDB_
std::unique_ptr< DatabaseCon > lgrMetaDB_
Definition: RelationalDBInterfaceSqlite.cpp:196
ripple::RelationalDBInterface::AccountTxPageOptions::marker
std::optional< AccountTxMarker > marker
Definition: RelationalDBInterface.h:79
ripple::RelationalDBInterfaceSqliteImp::checkoutTransaction
auto checkoutTransaction()
checkoutTransaction Checks out and returns node transaction DB.
Definition: RelationalDBInterfaceSqlite.cpp:294
std::function
ripple::RelationalDBInterfaceSqliteImp::txdb_
std::unique_ptr< DatabaseCon > txdb_
Definition: RelationalDBInterfaceSqlite.cpp:195
ripple::RelationalDBInterfaceSqliteImp::closeLedgerDB
void closeLedgerDB() override
Closes the ledger database.
Definition: RelationalDBInterfaceSqlite.cpp:1534
ripple::RelationalDBInterfaceSqliteImp::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 given ...
Definition: RelationalDBInterfaceSqlite.cpp:389
ripple::RelationalDBInterfaceSqliteImp::getHashByIndex
uint256 getHashByIndex(LedgerIndex ledgerIndex) override
getHashByIndex Returns hash of ledger with given sequence.
Definition: RelationalDBInterfaceSqlite.cpp:852
ripple::RelationalDBInterfaceSqliteImp::transactionDbHasSpace
bool transactionDbHasSpace(Config const &config) override
transactionDbHasSpace Checks if transaction database has available space.
Definition: RelationalDBInterfaceSqlite.cpp:1460
ripple::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: RelationalDBInterface_nodes.cpp:486
std::unique_ptr::reset
T reset(T... args)
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::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: RelationalDBInterface_nodes.cpp:474
ripple::RelationalDBInterfaceSqliteImp::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: RelationalDBInterfaceSqlite.cpp:367
ripple::Application::accountIDCache
virtual AccountIDCache const & accountIDCache() const =0
ripple::RelationalDBInterfaceSqliteImp::makeLedgerDBs
bool makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens node ledger and transaction databases, and saves its descriptors into internal va...
Definition: RelationalDBInterfaceSqlite.cpp:423
ripple::getNewestAccountTxs
std::pair< RelationalDBInterface::AccountTxs, int > getNewestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getNewestAccountTxs Returns newest transactions for given account which match given criteria starting...
Definition: RelationalDBInterface_nodes.cpp:906
ripple::RelationalDBInterface::CountMinMax
Definition: RelationalDBInterface.h:51
ripple::RelationalDBInterfaceSqliteImp::doLedger
bool doLedger(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)
doLedger Checks out ledger database for shard containing given ledger and calls given callback functi...
Definition: RelationalDBInterfaceSqlite.cpp:308
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:74
ripple::RelationalDBInterfaceSqliteImp::lastLedgerSeq
LedgerIndex lastLedgerSeq(std::uint32_t shardIndex)
lastLedgerSeq Returns last ledger sequence for given shard.
Definition: RelationalDBInterfaceSqlite.cpp:254
ripple::RelationalDBInterfaceSqliteImp::getOldestAccountTxsB
MetaTxsList getOldestAccountTxsB(AccountTxOptions const &options) override
getOldestAccountTxsB Returns oldest transactions in binary form for given account which match given c...
Definition: RelationalDBInterfaceSqlite.cpp:1053
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::RelationalDBInterfaceSqliteImp::getKBUsedAll
std::uint32_t getKBUsedAll() override
getKBUsedAll Returns space used by all databases.
Definition: RelationalDBInterfaceSqlite.cpp:1477
ripple::saveValidatedLedger
static bool saveValidatedLedger(Application &app, std::shared_ptr< Ledger const > const &ledger, bool current)
Definition: Ledger.cpp:916
ripple::RelationalDBInterfaceSqliteImp::deleteTransactionsBeforeLedgerSeq
void deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionsBeforeLedgerSeq Deletes all transactions with given ledger sequence and all sequenc...
Definition: RelationalDBInterfaceSqlite.cpp:572
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::Config
Definition: Config.h:68
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::RelationalDBInterfaceSqlite
Definition: RelationalDBInterfaceSqlite.h:27
ripple::RelationalDBInterfaceSqliteImp::getLimitedOldestLedgerInfo
std::optional< LedgerInfo > getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedOldestLedgerInfo Returns info of oldest ledger from ledgers with sequences greater or equal...
Definition: RelationalDBInterfaceSqlite.cpp:765
ripple::RelationalDBInterfaceSqliteImp::existsLedger
bool existsLedger()
checkoutLedger Checks if node ledger DB exists.
Definition: RelationalDBInterfaceSqlite.cpp:264
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::RelationalDBInterfaceSqliteImp::getTransactionCount
std::size_t getTransactionCount() override
getTransactionCount Returns number of transactions.
Definition: RelationalDBInterfaceSqlite.cpp:617
ripple::saveLedgerAsync
void saveLedgerAsync(Application &app, std::uint32_t seq)
Definition: AccountTxPaging.cpp:57
ripple::RelationalDBInterfaceSqliteImp::checkoutLedger
auto checkoutLedger()
checkoutTransaction Checks out and returns node ledger DB.
Definition: RelationalDBInterfaceSqlite.cpp:284
ripple::RelationalDBInterface::AccountTxOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDBInterface.h:67
ripple::RelationalDBInterfaceSqliteImp
Definition: RelationalDBInterfaceSqlite.cpp:42
ripple::RelationalDBInterfaceSqliteImp::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(uint256 const &ledgerHash) override
Definition: RelationalDBInterfaceSqlite.cpp:820
ripple::RelationalDBInterfaceSqliteImp::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo() override
getNewestLedgerInfo Returns info of newest saved ledger.
Definition: RelationalDBInterfaceSqlite.cpp:740
ripple::RelationalDBInterfaceSqliteImp::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq() override
getMinLedgerSeq Returns minimum ledger sequence in Ledgers table.
Definition: RelationalDBInterfaceSqlite.cpp:451
ripple::RelationalDBInterfaceSqliteImp::deleteBeforeLedgerSeq
void deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteBeforeLedgerSeq Deletes all ledgers with given sequence and all sequences below it.
Definition: RelationalDBInterfaceSqlite.cpp:551
ripple::deleteByLedgerSeq
void deleteByLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteByLedgerSeq Deletes all entries in given table for the ledger with given sequence.
Definition: RelationalDBInterface_nodes.cpp:141
ripple::RelationalDBInterfaceSqliteImp::makeMetaDBs
bool makeMetaDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeMetaDBs Opens shard index lookup databases, and saves their descriptors into internal variables.
Definition: RelationalDBInterfaceSqlite.cpp:436
ripple::Application::logs
virtual Logs & logs()=0
ripple::TableType::Transactions
@ Transactions
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
std::map
STL class.
ripple::RelationalDBInterface::AccountTxPageOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDBInterface.h:78
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::RelationalDBInterface::AccountTxPageOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDBInterface.h:77
ripple::RelationalDBInterfaceSqliteImp::getLedgerCountMinMax
RelationalDBInterface::CountMinMax getLedgerCountMinMax() override
getLedgerCountMinMax Returns minumum ledger sequence, maximum ledger sequence and total number of sav...
Definition: RelationalDBInterfaceSqlite.cpp:657
ripple::TableType::Ledgers
@ Ledgers
ripple::TableType::AccountTransactions
@ AccountTransactions
ripple::RelationalDBInterfaceSqliteImp::getLimitedNewestLedgerInfo
std::optional< LedgerInfo > getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedNewestLedgerInfo Returns info of newest ledger from ledgers with sequences greater or equal...
Definition: RelationalDBInterfaceSqlite.cpp:793
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::RelationalDBInterfaceSqliteImp::firstLedgerSeq
LedgerIndex firstLedgerSeq(std::uint32_t shardIndex)
firstLedgerSeq Returns first ledger sequence for given shard.
Definition: RelationalDBInterfaceSqlite.cpp:243
std::min
T min(T... args)
ripple::getNewestAccountTxsB
std::pair< std::vector< RelationalDBInterface::txnMetaLedgerType >, int > getNewestAccountTxsB(soci::session &session, Application &app, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getNewestAccountTxsB Returns newest transactions in binary form for given account which match given c...
Definition: RelationalDBInterface_nodes.cpp:1021
ripple::RelationalDBInterfaceSqliteImp::existsTransaction
bool existsTransaction()
checkoutTransaction Checks if node transaction DB exists.
Definition: RelationalDBInterfaceSqlite.cpp:274
ripple::RelationalDBInterfaceSqliteImp::getKBUsedTransaction
std::uint32_t getKBUsedTransaction() override
getKBUsedTransaction Returns space used by transaction database.
Definition: RelationalDBInterfaceSqlite.cpp:1515
std::variant::index
T index(T... args)
ripple::RelationalDBInterfaceSqliteImp::getTxHistory
std::vector< std::shared_ptr< Transaction > > getTxHistory(LedgerIndex startIndex) override
getTxHistory Returns most recent 20 transactions starting from given number or entry.
Definition: RelationalDBInterfaceSqlite.cpp:920
ripple::RelationalDBInterfaceSqliteImp::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq) override
getLedgerInfoByIndex Returns ledger by its sequence.
Definition: RelationalDBInterfaceSqlite.cpp:721
ripple::RelationalDBInterfaceSqliteImp::seqToShardIndex
std::uint32_t seqToShardIndex(LedgerIndex ledgerSeq)
seqToShardIndex Converts ledgers sequence to shard index.
Definition: RelationalDBInterfaceSqlite.cpp:232
std::vector::emplace_back
T emplace_back(T... args)
ripple::getTransaction
std::variant< RelationalDBInterface::AccountTx, TxSearched > getTransaction(soci::session &session, Application &app, uint256 const &id, std::optional< ClosedInterval< uint32_t >> const &range, error_code_i &ec)
getTransaction Returns transaction with given hash.
Definition: RelationalDBInterface_nodes.cpp:1262
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::getRows
std::size_t getRows(soci::session &session, TableType type)
getRows Returns number of rows in given table.
Definition: RelationalDBInterface_nodes.cpp:158
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
ripple::RelationalDBInterface::MetaTxsList
std::vector< txnMetaLedgerType > MetaTxsList
Definition: RelationalDBInterface.h:88
ripple::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: RelationalDBInterface_nodes.cpp:539
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::RelationalDBInterfaceSqliteImp::getHashesByIndex
std::optional< LedgerHashPair > getHashesByIndex(LedgerIndex ledgerIndex) override
getHashesByIndex Returns hash of the ledger and hash of parent ledger for the ledger of given sequenc...
Definition: RelationalDBInterfaceSqlite.cpp:871
ripple::RelationalDBInterfaceSqliteImp::getNewestAccountTxs
AccountTxs getNewestAccountTxs(AccountTxOptions const &options) override
getNewestAccountTxs Returns newest transactions for given account which match given criteria starting...
Definition: RelationalDBInterfaceSqlite.cpp:1004
std::map::insert
T insert(T... args)
ripple::convertBlobsToTxResult
void convertBlobsToTxResult(RelationalDBInterface::AccountTxs &to, std::uint32_t ledger_index, std::string const &status, Blob const &rawTxn, Blob const &rawMeta, Application &app)
Definition: AccountTxPaging.cpp:33
ripple::RelationalDBInterfaceSqliteImp::closeTransactionDB
void closeTransactionDB() override
Closes the transaction database.
Definition: RelationalDBInterfaceSqlite.cpp:1540
ripple::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: RelationalDBInterface_shards.cpp:36
ripple::RelationalDBInterfaceSqliteImp::app_
Application & app_
Definition: RelationalDBInterfaceSqlite.cpp:193
ripple::RelationalDBInterfaceSqliteImp::doTransaction
bool doTransaction(LedgerIndex ledgerSeq, std::function< bool(soci::session &session)> const &callback)
doTransaction Checks out transaction database for shard containing given ledger and calls given callb...
Definition: RelationalDBInterfaceSqlite.cpp:325
ripple::makeLedgerDBs
DatabasePairValid makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens ledger and transactions databases.
Definition: RelationalDBInterface_nodes.cpp:65
ripple::RelationalDBInterfaceSqliteImp::newestAccountTxPage
std::pair< AccountTxs, std::optional< AccountTxMarker > > newestAccountTxPage(AccountTxPageOptions const &options) override
newestAccountTxPage Returns newest transactions for given account which match given criteria starting...
Definition: RelationalDBInterfaceSqlite.cpp:1209
ripple::RelationalDBInterfaceSqliteImp::ledgerDbHasSpace
bool ledgerDbHasSpace(Config const &config) override
ledgerDbHasSpace Checks if ledger database has available space.
Definition: RelationalDBInterfaceSqlite.cpp:1443
ripple::oldestAccountTxPage
std::pair< std::optional< RelationalDBInterface::AccountTxMarker >, int > oldestAccountTxPage(soci::session &session, AccountIDCache const &idCache, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDBInterface::AccountTxPageOptions const &options, int limit_used, std::uint32_t page_length)
oldestAccountTxPage Searches oldest transactions for given account which match given criteria startin...
Definition: RelationalDBInterface_nodes.cpp:1216
ripple::RelationalDBInterfaceSqliteImp::getAccountTransactionCount
std::size_t getAccountTransactionCount() override
getAccountTransactionCount Returns number of account transactions.
Definition: RelationalDBInterfaceSqlite.cpp:637
ripple::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: RelationalDBInterface_shards.cpp:66
std::optional
std::size_t
ripple::RelationalDBInterfaceSqliteImp::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq() override
getMaxLedgerSeq Returns maximum ledger sequence in Ledgers table.
Definition: RelationalDBInterfaceSqlite.cpp:512
std::vector::end
T end(T... args)
ripple::RelationalDBInterfaceSqliteImp::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 give...
Definition: RelationalDBInterfaceSqlite.cpp:345
ripple::RelationalDBInterface::AccountTxPageOptions
Definition: RelationalDBInterface.h:74
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:44
std::max
T max(T... args)
ripple::dbHasSpace
bool dbHasSpace(soci::session &session, Config const &config, beast::Journal j)
dbHasSpace Checks if given database has available space.
Definition: RelationalDBInterface_nodes.cpp:1343
ripple::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq(soci::session &session, TableType type)
getMinLedgerSeq Returns minimum ledger sequence in given table.
Definition: RelationalDBInterface_nodes.cpp:121
ripple::RelationalDBInterfaceSqliteImp::getOldestAccountTxs
AccountTxs getOldestAccountTxs(AccountTxOptions const &options) override
getOldestAccountTxs Returns oldest transactions for given account which match given criteria starting...
Definition: RelationalDBInterfaceSqlite.cpp:955
ripple::getShardIndexforTransaction
std::optional< std::uint32_t > getShardIndexforTransaction(soci::session &session, TxID const &id)
getShardIndexforTransaction Queries the transaction meta database to retrieve the index of the shard ...
Definition: RelationalDBInterface_shards.cpp:144
std::unique_ptr
STL class.
ripple::RelationalDBInterfaceSqliteImp::saveValidatedLedger
bool saveValidatedLedger(std::shared_ptr< Ledger const > const &ledger, bool current) override
saveValidatedLedger Saves ledger into database.
Definition: RelationalDBInterfaceSqlite.cpp:684
ripple::RelationalDBInterface::AccountTxOptions
Definition: RelationalDBInterface.h:64
ripple::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo(soci::session &session, beast::Journal j)
getNewestLedgerInfo Returns info of newest saved ledger.
Definition: RelationalDBInterface_nodes.cpp:466
ripple::RelationalDBInterfaceSqliteImp::getTransactionsMinLedgerSeq
std::optional< LedgerIndex > getTransactionsMinLedgerSeq() override
getTransactionsMinLedgerSeq Returns minimum ledger sequence among records in the Transactions table.
Definition: RelationalDBInterfaceSqlite.cpp:471
ripple::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq(soci::session &session, TableType type)
getMaxLedgerSeq Returns maximum ledger sequence in given table.
Definition: RelationalDBInterface_nodes.cpp:131
ripple::RelationalDBInterfaceSqliteImp::RelationalDBInterfaceSqliteImp
RelationalDBInterfaceSqliteImp(Application &app, Config const &config, JobQueue &jobQueue)
Definition: RelationalDBInterfaceSqlite.cpp:45
std::ref
T ref(T... args)
std::variant
ripple::RelationalDBInterface::AccountTxs
std::vector< AccountTx > AccountTxs
Definition: RelationalDBInterface.h:86
ripple::RelationalDBInterfaceSqliteImp::getKBUsedLedger
std::uint32_t getKBUsedLedger() override
getKBUsedLedger Returns space used by ledger database.
Definition: RelationalDBInterfaceSqlite.cpp:1496
ripple::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: RelationalDBInterface_shards.cpp:130
ripple::RelationalDBInterfaceSqliteImp::getTransaction
std::variant< AccountTx, TxSearched > getTransaction(uint256 const &id, std::optional< ClosedInterval< std::uint32_t >> const &range, error_code_i &ec) override
Definition: RelationalDBInterfaceSqlite.cpp:1395