xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
rdb
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
17
namespace
xrpl
{
18
19
class
CheckpointersCollection
20
{
21
std::uintptr_t
nextId_
{0};
22
// Mutex protects the CheckpointersCollection
23
std::mutex
mutex_
;
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
27
std::unordered_map<std::uintptr_t, std::shared_ptr<Checkpointer>
>
checkpointers_
;
28
29
public
:
30
std::shared_ptr<Checkpointer>
31
fromId
(
std::uintptr_t
id
)
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
41
erase
(
std::uintptr_t
id
)
42
{
43
std::scoped_lock
const
lock{
mutex_
};
44
checkpointers_
.erase(
id
);
45
}
46
47
std::shared_ptr<Checkpointer>
48
create
(
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
61
CheckpointersCollection
gCheckpointers
;
62
63
std::shared_ptr<Checkpointer>
64
checkpointerFromId
(
std::uintptr_t
id
)
65
{
66
return
gCheckpointers
.fromId(
id
);
67
}
68
69
DatabaseCon::~DatabaseCon
()
70
{
71
if
(
checkpointer_
)
72
{
73
gCheckpointers
.erase(
checkpointer_
->id());
74
75
std::weak_ptr<Checkpointer>
const
wk(
checkpointer_
);
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
{
85
std::this_thread::sleep_for
(
std::chrono::milliseconds
(100));
86
}
87
}
88
}
89
90
std::unique_ptr<std::vector<std::string>
const
>
DatabaseCon::Setup::globalPragma
;
91
92
void
93
DatabaseCon::setupCheckpointing
(
JobQueue
* q,
ServiceRegistry
& registry)
94
{
95
if
(q ==
nullptr
)
96
Throw<std::logic_error>
(
"No JobQueue"
);
97
checkpointer_
=
gCheckpointers
.create(
session_
, *q, registry);
98
}
99
100
}
// namespace xrpl
chrono
xrpl::CheckpointersCollection
Definition
DatabaseCon.cpp:20
xrpl::CheckpointersCollection::mutex_
std::mutex mutex_
Definition
DatabaseCon.cpp:23
xrpl::CheckpointersCollection::nextId_
std::uintptr_t nextId_
Definition
DatabaseCon.cpp:21
xrpl::CheckpointersCollection::create
std::shared_ptr< Checkpointer > create(std::shared_ptr< soci::session > const &session, JobQueue &jobQueue, ServiceRegistry ®istry)
Definition
DatabaseCon.cpp:48
xrpl::CheckpointersCollection::erase
void erase(std::uintptr_t id)
Definition
DatabaseCon.cpp:41
xrpl::CheckpointersCollection::fromId
std::shared_ptr< Checkpointer > fromId(std::uintptr_t id)
Definition
DatabaseCon.cpp:31
xrpl::CheckpointersCollection::checkpointers_
std::unordered_map< std::uintptr_t, std::shared_ptr< Checkpointer > > checkpointers_
Definition
DatabaseCon.cpp:27
xrpl::DatabaseCon::setupCheckpointing
void setupCheckpointing(JobQueue *, ServiceRegistry &)
Definition
DatabaseCon.cpp:93
xrpl::DatabaseCon::session_
std::shared_ptr< soci::session > const session_
Definition
DatabaseCon.h:231
xrpl::DatabaseCon::checkpointer_
std::shared_ptr< Checkpointer > checkpointer_
Definition
DatabaseCon.h:232
xrpl::DatabaseCon::~DatabaseCon
~DatabaseCon()
Definition
DatabaseCon.cpp:69
xrpl::JobQueue
A pool of threads to perform work.
Definition
JobQueue.h:60
xrpl::ServiceRegistry
Service registry for dependency injection.
Definition
ServiceRegistry.h:98
cstdint
std::chrono::milliseconds
std::uintptr_t
memory
mutex
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::gCheckpointers
CheckpointersCollection gCheckpointers
Definition
DatabaseCon.cpp:61
xrpl::makeCheckpointer
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:333
xrpl::checkpointerFromId
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)
Definition
DatabaseCon.cpp:64
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::scoped_lock
std::shared_ptr
std::this_thread::sleep_for
T sleep_for(T... args)
stdexcept
string
xrpl::DatabaseCon::Setup::globalPragma
static std::unique_ptr< std::vector< std::string > const > globalPragma
Definition
DatabaseCon.h:97
thread
std::unique_ptr
unordered_map
std::weak_ptr::use_count
T use_count(T... args)
vector
std::weak_ptr
Generated by
1.17.0