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 namespace NodeStore {
27 
28 ManagerImp&
30 {
31  static ManagerImp _;
32  return _;
33 }
34 
35 void
37 {
38  Throw<std::runtime_error>(
39  "Your rippled.cfg is missing a [node_db] entry, "
40  "please see the rippled-example.cfg file!");
41 }
42 
45  Section const& parameters,
46  std::size_t burstSize,
47  Scheduler& scheduler,
48  beast::Journal journal)
49 {
50  std::string const type{get<std::string>(parameters, "type")};
51  if (type.empty())
53 
54  auto factory{find(type)};
55  if (!factory)
57 
58  return factory->createInstance(
59  NodeObject::keyBytes, parameters, burstSize, scheduler, journal);
60 }
61 
64  std::string const& name,
65  std::size_t burstSize,
66  Scheduler& scheduler,
67  int readThreads,
68  Stoppable& parent,
69  Section const& config,
70  beast::Journal journal)
71 {
72  auto backend{make_Backend(config, burstSize, scheduler, journal)};
73  backend->open();
74  return std::make_unique<DatabaseNodeImp>(
75  name,
76  scheduler,
77  readThreads,
78  parent,
79  std::move(backend),
80  config,
81  journal);
82 }
83 
84 void
86 {
88  list_.push_back(&factory);
89 }
90 
91 void
93 {
95  auto const iter =
96  std::find_if(list_.begin(), list_.end(), [&factory](Factory* other) {
97  return other == &factory;
98  });
99  assert(iter != list_.end());
100  list_.erase(iter);
101 }
102 
103 Factory*
105 {
107  auto const iter =
108  std::find_if(list_.begin(), list_.end(), [&name](Factory* other) {
109  return boost::iequals(name, other->getName());
110  });
111  if (iter == list_.end())
112  return nullptr;
113  return *iter;
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 Manager&
120 {
121  return ManagerImp::instance();
122 }
123 
124 //------------------------------------------------------------------------------
125 
128  Section const& config,
129  std::size_t burstSize,
130  Scheduler& scheduler,
131  beast::Journal journal)
132 {
134  config, burstSize, scheduler, journal);
135 }
136 
137 } // namespace NodeStore
138 } // 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:31
ripple::NodeStore::Factory
Base class for backend factories.
Definition: Factory.h:32
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:127
std::string
STL class.
ripple::NodeStore::ManagerImp::insert
void insert(Factory &factory) override
Add a factory.
Definition: ManagerImp.cpp:85
ripple::NodeStore::ManagerImp
Definition: ManagerImp.h:28
std::find_if
T find_if(T... args)
ripple::NodeStore::ManagerImp::missing_backend
static void missing_backend()
Definition: ManagerImp.cpp:36
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:44
ripple::NodeStore::ManagerImp::find
Factory * find(std::string const &name) override
Return a pointer to the matching factory if it exists.
Definition: ManagerImp.cpp:104
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::NodeStore::ManagerImp::instance
static ManagerImp & instance()
Definition: ManagerImp.cpp:29
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:61
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:32
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:31
std::size_t
ripple::NodeStore::Manager::instance
static Manager & instance()
Returns the instance of the manager singleton.
Definition: ManagerImp.cpp:119
std::unique_ptr
STL class.
ripple::NodeStore::ManagerImp::erase
void erase(Factory &factory) override
Remove a factory.
Definition: ManagerImp.cpp:92
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:63