rippled
Vacuum.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2021 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/rdb/Vacuum.h>
21 #include <boost/format.hpp>
22 
23 namespace ripple {
24 
25 bool
27 {
28  boost::filesystem::path dbPath = setup.dataDir / TxDBName;
29 
30  uintmax_t const dbSize = file_size(dbPath);
31  assert(dbSize != static_cast<uintmax_t>(-1));
32 
33  if (auto available = space(dbPath.parent_path()).available;
34  available < dbSize)
35  {
36  std::cerr << "The database filesystem must have at least as "
37  "much free space as the size of "
38  << dbPath.string() << ", which is " << dbSize
39  << " bytes. Only " << available << " bytes are available.\n";
40  return false;
41  }
42 
43  auto txnDB =
44  std::make_unique<DatabaseCon>(setup, TxDBName, TxDBPragma, TxDBInit);
45  auto& session = txnDB->getSession();
46  std::uint32_t pageSize;
47 
48  // Only the most trivial databases will fit in memory on typical
49  // (recommended) hardware. Force temp files to be written to disk
50  // regardless of the config settings.
51  session << boost::format(CommonDBPragmaTemp) % "file";
52  session << "PRAGMA page_size;", soci::into(pageSize);
53 
54  std::cout << "VACUUM beginning. page_size: " << pageSize << std::endl;
55 
56  session << "VACUUM;";
57  assert(setup.globalPragma);
58  for (auto const& p : *setup.globalPragma)
59  session << p;
60  session << "PRAGMA page_size;", soci::into(pageSize);
61 
62  std::cout << "VACUUM finished. page_size: " << pageSize << std::endl;
63 
64  return true;
65 }
66 
67 } // namespace ripple
ripple::DatabaseCon::Setup::globalPragma
static std::unique_ptr< std::vector< std::string > const > globalPragma
Definition: DatabaseCon.h:104
ripple::TxDBPragma
constexpr std::array< char const *, 4 > TxDBPragma
Definition: DBInit.h:78
ripple::DatabaseCon::Setup
Definition: DatabaseCon.h:84
std::cerr
ripple::DatabaseCon::Setup::dataDir
boost::filesystem::path dataDir
Definition: DatabaseCon.h:91
std::cout
ripple::TxDBName
constexpr auto TxDBName
Definition: DBInit.h:73
std::uint32_t
ripple::CommonDBPragmaTemp
constexpr char const * CommonDBPragmaTemp
Definition: DBInit.h:34
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::endl
T endl(T... args)
ripple::doVacuumDB
bool doVacuumDB(DatabaseCon::Setup const &setup)
doVacuumDB Creates, initialises, and performs cleanup on a database.
Definition: Vacuum.cpp:26
ripple::TxDBInit
constexpr std::array< char const *, 8 > TxDBInit
Definition: DBInit.h:94
ripple::PublisherStatus::available
@ available