rippled
DatabaseShard.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2017 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 #ifndef RIPPLE_NODESTORE_DATABASESHARD_H_INCLUDED
21 #define RIPPLE_NODESTORE_DATABASESHARD_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/basics/RangeSet.h>
25 #include <ripple/core/DatabaseCon.h>
26 #include <ripple/nodestore/Database.h>
27 #include <ripple/nodestore/Types.h>
28 
29 #include <memory>
30 #include <optional>
31 
32 namespace ripple {
33 namespace NodeStore {
34 
37 class DatabaseShard : public Database
38 {
39 public:
50  std::string const& name,
51  Stoppable& parent,
52  Scheduler& scheduler,
53  int readThreads,
54  Section const& config,
55  beast::Journal journal)
56  : Database(name, parent, scheduler, readThreads, config, journal)
57  {
58  }
59 
64  virtual bool
65  init() = 0;
66 
80  prepareLedger(std::uint32_t validLedgerSeq) = 0;
81 
87  virtual bool
88  prepareShards(std::vector<std::uint32_t> const& shardIndexes) = 0;
89 
94  virtual void
95  removePreShard(std::uint32_t shardIndex) = 0;
96 
101  virtual std::string
102  getPreShards() = 0;
103 
111  virtual bool
112  importShard(
113  std::uint32_t shardIndex,
114  boost::filesystem::path const& srcDir) = 0;
115 
123  fetchLedger(uint256 const& hash, std::uint32_t seq) = 0;
124 
130  virtual void
131  setStored(std::shared_ptr<Ledger const> const& ledger) = 0;
132 
137  virtual std::string
138  getCompleteShards() = 0;
139 
148  virtual bool
150  LedgerIndex ledgerSeq,
151  std::function<bool(soci::session& session, std::uint32_t index)> const&
152  callback) = 0;
153 
162  virtual bool
164  LedgerIndex ledgerSeq,
165  std::function<bool(soci::session& session, std::uint32_t index)> const&
166  callback) = 0;
167 
179  virtual bool
181  std::optional<std::uint32_t> minShardIndex,
182  std::function<bool(soci::session& session, std::uint32_t index)> const&
183  callback) = 0;
184 
196  virtual bool
198  std::optional<std::uint32_t> minShardIndex,
199  std::function<bool(soci::session& session, std::uint32_t index)> const&
200  callback) = 0;
201 
213  virtual bool
215  std::optional<std::uint32_t> maxShardIndex,
216  std::function<bool(soci::session& session, std::uint32_t index)> const&
217  callback) = 0;
218 
230  virtual bool
232  std::optional<std::uint32_t> maxShardIndex,
233  std::function<bool(soci::session& session, std::uint32_t index)> const&
234  callback) = 0;
235 
238  virtual std::uint32_t
239  ledgersPerShard() const = 0;
240 
243  virtual std::uint32_t
244  earliestShardIndex() const = 0;
245 
251  virtual std::uint32_t
252  seqToShardIndex(std::uint32_t seq) const = 0;
253 
259  virtual std::uint32_t
260  firstLedgerSeq(std::uint32_t shardIndex) const = 0;
261 
267  virtual std::uint32_t
268  lastLedgerSeq(std::uint32_t shardIndex) const = 0;
269 
272  virtual boost::filesystem::path const&
273  getRootDir() const = 0;
274 
276  static constexpr std::uint32_t ledgersPerShardDefault{16384u};
277 };
278 
279 constexpr std::uint32_t
281  std::uint32_t ledgerSeq,
283 {
284  return (ledgerSeq - 1) / ledgersPerShard;
285 }
286 
289  Application& app,
290  Stoppable& parent,
291  Scheduler& scheduler,
292  int readThreads,
293  beast::Journal j);
294 
295 } // namespace NodeStore
296 } // namespace ripple
297 
298 #endif
ripple::NodeStore::make_ShardStore
std::unique_ptr< DatabaseShard > make_ShardStore(Application &app, Stoppable &parent, Scheduler &scheduler, int readThreads, beast::Journal j)
Definition: DatabaseShardImp.cpp:2048
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
ripple::Application
Definition: Application.h:102
ripple::NodeStore::Database
Persistency layer for NodeObject.
Definition: Database.h:50
ripple::NodeStore::DatabaseShard::earliestShardIndex
virtual std::uint32_t earliestShardIndex() const =0
std::string
STL class.
std::shared_ptr< Ledger >
ripple::NodeStore::DatabaseShard::seqToShardIndex
virtual std::uint32_t seqToShardIndex(std::uint32_t seq) const =0
Calculates the shard index for a given ledger sequence.
ripple::NodeStore::DatabaseShard::iterateTransactionSQLsForward
virtual bool iterateTransactionSQLsForward(std::optional< std::uint32_t > minShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateTransactionSQLsForward Checkouts transaction databases for all shards in ascending order start...
ripple::NodeStore::DatabaseShard::DatabaseShard
DatabaseShard(std::string const &name, Stoppable &parent, Scheduler &scheduler, int readThreads, Section const &config, beast::Journal journal)
Construct a shard store.
Definition: DatabaseShard.h:49
std::vector
STL class.
ripple::NodeStore::DatabaseShard::iterateTransactionSQLsBack
virtual bool iterateTransactionSQLsBack(std::optional< std::uint32_t > maxShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateTransactionSQLsBack Checkouts transaction databases for all shards in descending order startin...
ripple::NodeStore::DatabaseShard::lastLedgerSeq
virtual std::uint32_t lastLedgerSeq(std::uint32_t shardIndex) const =0
Calculates the last ledger sequence for a given shard index.
std::function
ripple::NodeStore::DatabaseShard::getCompleteShards
virtual std::string getCompleteShards()=0
Query which complete shards are stored.
ripple::NodeStore::DatabaseShard::getRootDir
virtual boost::filesystem::path const & getRootDir() const =0
Returns the root database directory.
ripple::NodeStore::DatabaseShard::prepareLedger
virtual std::optional< std::uint32_t > prepareLedger(std::uint32_t validLedgerSeq)=0
Prepare to store a new ledger in the shard being acquired.
ripple::base_uint< 256 >
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::DatabaseShard
A collection of historical shards.
Definition: DatabaseShard.h:37
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::NodeStore::seqToShardIndex
constexpr std::uint32_t seqToShardIndex(std::uint32_t ledgerSeq, std::uint32_t ledgersPerShard=DatabaseShard::ledgersPerShardDefault)
Definition: DatabaseShard.h:280
std::uint32_t
ripple::NodeStore::DatabaseShard::callForTransactionSQL
virtual bool callForTransactionSQL(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
callForTransactionSQL Checkouts transaction database for shard containing given ledger and calls give...
ripple::NodeStore::DatabaseShard::firstLedgerSeq
virtual std::uint32_t firstLedgerSeq(std::uint32_t shardIndex) const =0
Calculates the first ledger sequence for a given shard index.
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::DatabaseShard::prepareShards
virtual bool prepareShards(std::vector< std::uint32_t > const &shardIndexes)=0
Prepare one or more shard indexes to be imported into the database.
memory
ripple::NodeStore::DatabaseShard::fetchLedger
virtual std::shared_ptr< Ledger > fetchLedger(uint256 const &hash, std::uint32_t seq)=0
Fetch a ledger from the shard store.
ripple::NodeStore::DatabaseShard::ledgersPerShardDefault
static constexpr std::uint32_t ledgersPerShardDefault
The number of ledgers in a shard.
Definition: DatabaseShard.h:276
ripple::NodeStore::DatabaseShard::importShard
virtual bool importShard(std::uint32_t shardIndex, boost::filesystem::path const &srcDir)=0
Import a shard into the shard database.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::DatabaseShard::iterateLedgerSQLsBack
virtual bool iterateLedgerSQLsBack(std::optional< std::uint32_t > maxShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateLedgerSQLsBack Checkouts ledger databases for all shards in descending order starting from giv...
ripple::NodeStore::DatabaseShard::callForLedgerSQL
virtual bool callForLedgerSQL(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
callForLedgerSQL Checkouts ledger database for shard containing given ledger and calls given callback...
ripple::NodeStore::DatabaseShard::setStored
virtual void setStored(std::shared_ptr< Ledger const > const &ledger)=0
Notifies the database that the given ledger has been fully acquired and stored.
ripple::NodeStore::DatabaseShard::getPreShards
virtual std::string getPreShards()=0
Get shard indexes being imported.
ripple::NodeStore::DatabaseShard::init
virtual bool init()=0
Initialize the database.
optional
ripple::NodeStore::DatabaseShard::removePreShard
virtual void removePreShard(std::uint32_t shardIndex)=0
Remove a previously prepared shard index for import.
std::unique_ptr
STL class.
ripple::NodeStore::DatabaseShard::ledgersPerShard
virtual std::uint32_t ledgersPerShard() const =0
ripple::NodeStore::DatabaseShard::iterateLedgerSQLsForward
virtual bool iterateLedgerSQLsForward(std::optional< std::uint32_t > minShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateLedgerSQLsForward Checkouts ledger databases for all shards in ascending order starting from g...