rippled
Loading...
Searching...
No Matches
DatabaseCon.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/core/ServiceRegistry.h>
3#include <xrpl/rdb/DatabaseCon.h>
4#include <xrpl/rdb/SociDB.h>
5
6#include <boost/algorithm/string.hpp>
7#include <boost/format.hpp>
8
9#include <memory>
10#include <unordered_map>
11
12namespace xrpl {
13
15{
17 // Mutex protects the CheckpointersCollection
19 // Each checkpointer is given a unique id. All the checkpointers that are
20 // part of a DatabaseCon are part of this collection. When the DatabaseCon
21 // is destroyed, its checkpointer is removed from the collection
23
24public:
27 {
28 std::lock_guard const l{mutex_};
29 auto it = checkpointers_.find(id);
30 if (it != checkpointers_.end())
31 return it->second;
32 return nullptr;
33 }
34
35 void
37 {
38 std::lock_guard const lock{mutex_};
39 checkpointers_.erase(id);
40 }
41
44 std::shared_ptr<soci::session> const& session,
45 JobQueue& jobQueue,
46 ServiceRegistry& registry)
47 {
48 std::lock_guard const lock{mutex_};
49 auto const id = nextId_++;
50 auto const r = makeCheckpointer(id, session, jobQueue, registry);
51 checkpointers_[id] = r;
52 return r;
53 }
54};
55
57
63
65{
66 if (checkpointer_)
67 {
69
71 checkpointer_.reset();
72
73 // The references to our Checkpointer held by 'checkpointer_' and
74 // 'checkpointers' have been removed, so if the use count is nonzero, a
75 // checkpoint is currently in progress. Wait for it to end, otherwise
76 // creating a new DatabaseCon to the same database may fail due to the
77 // database being locked by our (now old) Checkpointer.
78 while (wk.use_count() != 0)
79 {
81 }
82 }
83}
84
86
87void
89{
90 if (q == nullptr)
91 Throw<std::logic_error>("No JobQueue");
93}
94
95} // namespace xrpl
std::shared_ptr< Checkpointer > create(std::shared_ptr< soci::session > const &session, JobQueue &jobQueue, ServiceRegistry &registry)
void erase(std::uintptr_t id)
std::shared_ptr< Checkpointer > fromId(std::uintptr_t id)
std::unordered_map< std::uintptr_t, std::shared_ptr< Checkpointer > > checkpointers_
void setupCheckpointing(JobQueue *, ServiceRegistry &)
std::shared_ptr< soci::session > const session_
std::shared_ptr< Checkpointer > checkpointer_
A pool of threads to perform work.
Definition JobQueue.h:38
Service registry for dependency injection.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
CheckpointersCollection checkpointers
std::shared_ptr< Checkpointer > makeCheckpointer(std::uintptr_t id, std::weak_ptr< soci::session >, JobQueue &, ServiceRegistry &)
Returns a new checkpointer which makes checkpoints of a soci database every checkpointPageCount pages...
Definition SociDB.cpp:310
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)
T sleep_for(T... args)
static std::unique_ptr< std::vector< std::string > const > globalPragma
Definition DatabaseCon.h:90
T use_count(T... args)