rippled
BatchWriter.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/BatchWriter.h>
21 
22 namespace ripple {
23 namespace NodeStore {
24 
26  : m_callback(callback)
27  , m_scheduler(scheduler)
28  , mWriteLoad(0)
29  , mWritePending(false)
30 {
32 }
33 
35 {
37 }
38 
39 void
41 {
43 
44  // If the batch has reached its limit, we wait
45  // until the batch writer is finished
48 
49  mWriteSet.push_back(object);
50 
51  if (!mWritePending)
52  {
53  mWritePending = true;
54 
56  }
57 }
58 
59 int
61 {
63 
64  return std::max(mWriteLoad, static_cast<int>(mWriteSet.size()));
65 }
66 
67 void
69 {
70  writeBatch();
71 }
72 
73 void
75 {
76  for (;;)
77  {
79 
81 
82  {
84 
86  assert(mWriteSet.empty());
87  mWriteLoad = set.size();
88 
89  if (set.empty())
90  {
91  mWritePending = false;
93 
94  // VFALCO NOTE Fix this function to not return from the middle
95  return;
96  }
97  }
98 
99  BatchWriteReport report;
100  report.writeCount = set.size();
101  auto const before = std::chrono::steady_clock::now();
102 
104 
105  report.elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
106  std::chrono::steady_clock::now() - before);
107 
108  m_scheduler.onBatchWrite(report);
109  }
110 }
111 
112 void
114 {
116 
117  while (mWritePending)
118  mWriteCondition.wait(sl);
119 }
120 
121 } // namespace NodeStore
122 } // namespace ripple
std::shared_ptr< NodeObject >
ripple::NodeStore::BatchWriter::writeBatch
void writeBatch()
Definition: BatchWriter.cpp:74
ripple::NodeStore::BatchWriter::performScheduledTask
void performScheduledTask() override
Performs the task.
Definition: BatchWriter.cpp:68
ripple::NodeStore::BatchWriter::mWriteCondition
CondvarType mWriteCondition
Definition: BatchWriter.h:92
std::vector::reserve
T reserve(T... args)
ripple::NodeStore::BatchWriter::mWritePending
bool mWritePending
Definition: BatchWriter.h:94
std::vector
STL class.
std::vector::size
T size(T... args)
std::lock_guard
STL class.
ripple::NodeStore::BatchWriter::waitForWriting
void waitForWriting()
Definition: BatchWriter.cpp:113
ripple::NodeStore::batchWritePreallocationSize
@ batchWritePreallocationSize
Definition: nodestore/Types.h:34
std::vector::push_back
T push_back(T... args)
ripple::NodeStore::Scheduler::scheduleTask
virtual void scheduleTask(Task &task)=0
Schedules a task.
ripple::NodeStore::BatchWriter::Callback::writeBatch
virtual void writeBatch(Batch const &batch)=0
ripple::NodeStore::batchWriteLimitSize
@ batchWriteLimitSize
Definition: nodestore/Types.h:40
std::unique_lock
STL class.
ripple::set
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,...
Definition: BasicConfig.h:313
ripple::NodeStore::BatchWriter::Callback
This callback does the actual writing.
Definition: BatchWriter.h:44
std::condition_variable_any::wait
T wait(T... args)
ripple::NodeStore::Scheduler
Scheduling for asynchronous backend activity.
Definition: ripple/nodestore/Scheduler.h:60
ripple::NodeStore::BatchWriter::~BatchWriter
~BatchWriter()
Destroy a batch writer.
Definition: BatchWriter.cpp:34
ripple::NodeStore::BatchWriteReport
Contains information about a batch write operation.
Definition: ripple/nodestore/Scheduler.h:44
ripple::NodeStore::BatchWriter::mWriteMutex
LockType mWriteMutex
Definition: BatchWriter.h:91
std::vector::swap
T swap(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::BatchWriter::getWriteLoad
int getWriteLoad()
Get an estimate of the amount of writing I/O pending.
Definition: BatchWriter.cpp:60
ripple::NodeStore::BatchWriter::mWriteSet
Batch mWriteSet
Definition: BatchWriter.h:95
ripple::NodeStore::BatchWriter::BatchWriter
BatchWriter(Callback &callback, Scheduler &scheduler)
Create a batch writer.
Definition: BatchWriter.cpp:25
ripple::NodeStore::BatchWriter::store
void store(std::shared_ptr< NodeObject > const &object)
Store the object.
Definition: BatchWriter.cpp:40
ripple::NodeStore::BatchWriter::mWriteLoad
int mWriteLoad
Definition: BatchWriter.h:93
std::vector::empty
T empty(T... args)
ripple::NodeStore::BatchWriter::m_callback
Callback & m_callback
Definition: BatchWriter.h:89
ripple::NodeStore::BatchWriteReport::writeCount
int writeCount
Definition: ripple/nodestore/Scheduler.h:49
std::max
T max(T... args)
ripple::NodeStore::BatchWriteReport::elapsed
std::chrono::milliseconds elapsed
Definition: ripple/nodestore/Scheduler.h:48
std::condition_variable_any::notify_all
T notify_all(T... args)
ripple::NodeStore::Scheduler::onBatchWrite
virtual void onBatchWrite(BatchWriteReport const &report)=0
Reports the completion of a batch write Allows the scheduler to monitor the node store's performance.
ripple::NodeStore::BatchWriter::m_scheduler
Scheduler & m_scheduler
Definition: BatchWriter.h:90
std::chrono::steady_clock::now
T now(T... args)