rippled
ManagerImp.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/nodestore/impl/DatabaseNodeImp.h>
21 #include <ripple/nodestore/impl/ManagerImp.h>
22 
23 #include <boost/algorithm/string/predicate.hpp>
24 
25 namespace ripple {
26 
27 namespace NodeStore {
28 
29 ManagerImp&
31 {
32  static ManagerImp _;
33  return _;
34 }
35 
36 void
38 {
39  Throw<std::runtime_error>(
40  "Your rippled.cfg is missing a [node_db] entry, "
41  "please see the rippled-example.cfg file!");
42 }
43 
46  Section const& parameters,
47  std::size_t burstSize,
48  Scheduler& scheduler,
49  beast::Journal journal)
50 {
51  std::string const type{get(parameters, "type")};
52  if (type.empty())
54 
55  auto factory{find(type)};
56  if (!factory)
57  {
58 #ifndef RIPPLED_REPORTING
59  if (boost::iequals(type, "cassandra"))
60  Throw<std::runtime_error>(
61  "To use Cassandra as a nodestore, build rippled with "
62  "-Dreporting=ON");
63 #endif
65  }
66 
67  return factory->createInstance(
68  NodeObject::keyBytes, parameters, burstSize, scheduler, journal);
69 }
70 
73  std::size_t burstSize,
74  Scheduler& scheduler,
75  int readThreads,
76  Section const& config,
77  beast::Journal journal)
78 {
79  auto backend{make_Backend(config, burstSize, scheduler, journal)};
80  backend->open();
81  return std::make_unique<DatabaseNodeImp>(
82  scheduler, readThreads, std::move(backend), config, journal);
83 }
84 
85 void
87 {
89  list_.push_back(&factory);
90 }
91 
92 void
94 {
96  auto const iter =
97  std::find_if(list_.begin(), list_.end(), [&factory](Factory* other) {
98  return other == &factory;
99  });
100  assert(iter != list_.end());
101  list_.erase(iter);
102 }
103 
104 Factory*
106 {
108  auto const iter =
109  std::find_if(list_.begin(), list_.end(), [&name](Factory* other) {
110  return boost::iequals(name, other->getName());
111  });
112  if (iter == list_.end())
113  return nullptr;
114  return *iter;
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 Manager&
121 {
122  return ManagerImp::instance();
123 }
124 
125 } // namespace NodeStore
126 } // namespace ripple
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:42
ripple::NodeStore::Manager
Singleton for managing NodeStore factories and back ends.
Definition: Manager.h:32
ripple::NodeStore::Factory
Base class for backend factories.
Definition: Factory.h:33
std::string
STL class.
ripple::NodeStore::ManagerImp::insert
void insert(Factory &factory) override
Add a factory.
Definition: ManagerImp.cpp:86
ripple::NodeStore::ManagerImp
Definition: ManagerImp.h:29
std::find_if
T find_if(T... args)
ripple::NodeStore::ManagerImp::missing_backend
static void missing_backend()
Definition: ManagerImp.cpp:37
std::lock_guard
STL class.
ripple::NodeStore::ManagerImp::make_Database
std::unique_ptr< Database > make_Database(std::size_t burstSize, Scheduler &scheduler, int readThreads, Section const &config, beast::Journal journal) override
Construct a NodeStore database.
Definition: ManagerImp.cpp:72
ripple::NodeStore::ManagerImp::make_Backend
std::unique_ptr< Backend > make_Backend(Section const &parameters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal) override
Create a backend.
Definition: ManagerImp.cpp:45
ripple::NodeStore::ManagerImp::find
Factory * find(std::string const &name) override
Return a pointer to the matching factory if it exists.
Definition: ManagerImp.cpp:105
ripple::NodeStore::ManagerImp::instance
static ManagerImp & instance()
Definition: ManagerImp.cpp:30
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::SizedItem::burstSize
@ burstSize
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeObject::keyBytes
static constexpr std::size_t keyBytes
Definition: NodeObject.h:52
ripple::NodeStore::ManagerImp::list_
std::vector< Factory * > list_
Definition: ManagerImp.h:33
ripple::NodeStore::ManagerImp::mutex_
std::mutex mutex_
Definition: ManagerImp.h:32
std::size_t
ripple::NodeStore::Manager::instance
static Manager & instance()
Returns the instance of the manager singleton.
Definition: ManagerImp.cpp:120
std::unique_ptr
STL class.
ripple::NodeStore::ManagerImp::erase
void erase(Factory &factory) override
Remove a factory.
Definition: ManagerImp.cpp:93
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:118