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<std::string>(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::string const& name,
74  std::size_t burstSize,
75  Scheduler& scheduler,
76  int readThreads,
77  Stoppable& parent,
78  Section const& config,
79  beast::Journal journal)
80 {
81  auto backend{make_Backend(config, burstSize, scheduler, journal)};
82  backend->open();
83  return std::make_unique<DatabaseNodeImp>(
84  name,
85  scheduler,
86  readThreads,
87  parent,
88  std::move(backend),
89  config,
90  journal);
91 }
92 
93 void
95 {
97  list_.push_back(&factory);
98 }
99 
100 void
102 {
104  auto const iter =
105  std::find_if(list_.begin(), list_.end(), [&factory](Factory* other) {
106  return other == &factory;
107  });
108  assert(iter != list_.end());
109  list_.erase(iter);
110 }
111 
112 Factory*
114 {
116  auto const iter =
117  std::find_if(list_.begin(), list_.end(), [&name](Factory* other) {
118  return boost::iequals(name, other->getName());
119  });
120  if (iter == list_.end())
121  return nullptr;
122  return *iter;
123 }
124 
125 //------------------------------------------------------------------------------
126 
127 Manager&
129 {
130  return ManagerImp::instance();
131 }
132 
133 //------------------------------------------------------------------------------
134 
137  Section const& config,
138  std::size_t burstSize,
139  Scheduler& scheduler,
140  beast::Journal journal)
141 {
143  config, burstSize, scheduler, journal);
144 }
145 
146 } // namespace NodeStore
147 } // namespace ripple
ripple::Section
Holds a collection of configuration values.
Definition: BasicConfig.h:43
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
ripple::NodeStore::make_Backend
std::unique_ptr< Backend > make_Backend(Section const &config, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)
Create a Backend.
Definition: ManagerImp.cpp:136
std::string
STL class.
ripple::NodeStore::ManagerImp::insert
void insert(Factory &factory) override
Add a factory.
Definition: ManagerImp.cpp:94
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_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:113
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
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:51
ripple::NodeStore::ManagerImp::list_
std::vector< Factory * > list_
Definition: ManagerImp.h:33
ripple::NodeStore::Manager::make_Backend
virtual std::unique_ptr< Backend > make_Backend(Section const &parameters, std::size_t burstSize, Scheduler &scheduler, beast::Journal journal)=0
Create a backend.
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:128
std::unique_ptr
STL class.
ripple::NodeStore::ManagerImp::erase
void erase(Factory &factory) override
Remove a factory.
Definition: ManagerImp.cpp:101
ripple::NodeStore::ManagerImp::make_Database
std::unique_ptr< Database > make_Database(std::string const &name, std::size_t burstSize, Scheduler &scheduler, int readThreads, Stoppable &parent, Section const &config, beast::Journal journal) override
Construct a NodeStore database.
Definition: ManagerImp.cpp:72