xrpld
Loading...
Searching...
No Matches
BatchWriter.cpp
1#include <xrpl/nodestore/detail/BatchWriter.h>
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/nodestore/NodeObject.h>
5#include <xrpl/nodestore/Scheduler.h>
6#include <xrpl/nodestore/Types.h>
7
8#include <algorithm>
9#include <chrono>
10#include <memory>
11#include <mutex>
12#include <vector>
13
14namespace xrpl::NodeStore {
15
17 : callback_(callback), scheduler_(scheduler)
18{
20}
21
26
27void
29{
31
32 // If the batch has reached its limit, we wait
33 // until the batch writer is finished
35 writeCondition_.wait(sl);
36
37 writeSet_.push_back(object);
38
39 if (!writePending_)
40 {
41 writePending_ = true;
42
43 scheduler_.scheduleTask(*this);
44 }
45}
46
47int
49{
51
52 return std::max(writeLoad_, static_cast<int>(writeSet_.size()));
53}
54
55void
60
61void
63{
64 for (;;)
65 {
67
69
70 {
72
73 writeSet_.swap(set);
74 XRPL_ASSERT(
75 writeSet_.empty(), "xrpl::NodeStore::BatchWriter::writeBatch : writes not set");
76 writeLoad_ = set.size();
77
78 if (set.empty())
79 {
80 writePending_ = false;
81 writeCondition_.notify_all();
82
83 // VFALCO NOTE Fix this function to not return from the middle
84 return;
85 }
86 }
87
88 BatchWriteReport report{};
89 report.writeCount = set.size();
90 auto const before = std::chrono::steady_clock::now();
91
92 callback_.writeBatch(set);
93
96
97 scheduler_.onBatchWrite(report);
98 }
99}
100
101void
103{
105
106 while (writePending_)
107 writeCondition_.wait(sl);
108}
109
110} // namespace xrpl::NodeStore
void store(std::shared_ptr< NodeObject > const &object)
Store the object.
void performScheduledTask() override
Performs the task.
~BatchWriter() override
Destroy a batch writer.
int getWriteLoad()
Get an estimate of the amount of writing I/O pending.
BatchWriter(Callback &callback, Scheduler &scheduler)
Create a batch writer.
Scheduling for asynchronous backend activity.
T duration_cast(T... args)
T max(T... args)
static constexpr auto kBatchWritePreallocationSize
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Contains information about a batch write operation.
This callback does the actual writing.
Definition BatchWriter.h:25