xrpld
Loading...
Searching...
No Matches
DatabaseCon.cpp
1#include <xrpl/rdb/DatabaseCon.h>
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/core/ServiceRegistry.h>
5#include <xrpl/rdb/SociDB.h>
6
7#include <chrono>
8#include <cstdint>
9#include <memory>
10#include <mutex>
11#include <stdexcept>
12#include <string>
13#include <thread>
14#include <unordered_map>
15#include <vector>
16
17namespace xrpl {
18
20{
22 // Mutex protects the CheckpointersCollection
24 // Each checkpointer is given a unique id. All the checkpointers that are
25 // part of a DatabaseCon are part of this collection. When the DatabaseCon
26 // is destroyed, its checkpointer is removed from the collection
28
29public:
32 {
33 std::scoped_lock const l{mutex_};
34 auto it = checkpointers_.find(id);
35 if (it != checkpointers_.end())
36 return it->second;
37 return nullptr;
38 }
39
40 void
42 {
43 std::scoped_lock const lock{mutex_};
44 checkpointers_.erase(id);
45 }
46
49 std::shared_ptr<soci::session> const& session,
50 JobQueue& jobQueue,
51 ServiceRegistry& registry)
52 {
53 std::scoped_lock const lock{mutex_};
54 auto const id = nextId_++;
55 auto const r = makeCheckpointer(id, session, jobQueue, registry);
56 checkpointers_[id] = r;
57 return r;
58 }
59};
60
62
65{
66 return gCheckpointers.fromId(id);
67}
68
70{
71 if (checkpointer_)
72 {
73 gCheckpointers.erase(checkpointer_->id());
74
76 checkpointer_.reset();
77
78 // The references to our Checkpointer held by 'checkpointer_' and
79 // 'checkpointers' have been removed, so if the use count is nonzero, a
80 // checkpoint is currently in progress. Wait for it to end, otherwise
81 // creating a new DatabaseCon to the same database may fail due to the
82 // database being locked by our (now old) Checkpointer.
83 while (wk.use_count() != 0)
84 {
86 }
87 }
88}
89
91
92void
94{
95 if (q == nullptr)
96 Throw<std::logic_error>("No JobQueue");
97 checkpointer_ = gCheckpointers.create(session_, *q, registry);
98}
99
100} // 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:43
Service registry for dependency injection.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
CheckpointersCollection gCheckpointers
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:327
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T sleep_for(T... args)
static std::unique_ptr< std::vector< std::string > const > globalPragma
Definition DatabaseCon.h:90
T use_count(T... args)