icinga2/lib/base/bulker.hpp
Yonas Habteab 91c7e60df8 Replace all existing copyright headers with SPDX headers
I've used the following command to replace the original copyright header
lines in a C-style comment block:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

For files that use shell-style comments (#) like CMakeLists.txt, I've
used this command:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

And for SQL files:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```
2026-02-04 14:00:05 +01:00

120 lines
2.4 KiB
C++

// SPDX-FileCopyrightText: 2022 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef BULKER_H
#define BULKER_H
#include <boost/config.hpp>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <utility>
#include <vector>
namespace icinga
{
/**
* A queue which outputs the input as bulks of a defined size
* or after a defined time, whichever is reached first
*
* @ingroup base
*/
template<class T>
class Bulker
{
private:
typedef std::chrono::steady_clock Clock;
public:
typedef std::vector<T> Container;
typedef typename Container::size_type SizeType;
typedef typename Clock::duration Duration;
Bulker(SizeType bulkSize, Duration threshold)
: m_BulkSize(bulkSize), m_Threshold(threshold), m_NextConsumption(NullTimePoint()) { }
void ProduceOne(T needle);
Container ConsumeMany();
SizeType Size();
inline SizeType GetBulkSize() const noexcept
{
return m_BulkSize;
}
private:
typedef std::chrono::time_point<Clock> TimePoint;
static inline
TimePoint NullTimePoint()
{
return TimePoint::min();
}
inline void UpdateNextConsumption()
{
m_NextConsumption = Clock::now() + m_Threshold;
}
const SizeType m_BulkSize;
const Duration m_Threshold;
std::mutex m_Mutex;
std::condition_variable m_CV;
std::queue<Container> m_Bulks;
TimePoint m_NextConsumption;
};
template<class T>
void Bulker<T>::ProduceOne(T needle)
{
std::unique_lock<std::mutex> lock (m_Mutex);
if (m_Bulks.empty() || m_Bulks.back().size() == m_BulkSize) {
m_Bulks.emplace();
}
m_Bulks.back().emplace_back(std::move(needle));
if (m_Bulks.size() == 1u && m_Bulks.back().size() == m_BulkSize) {
m_CV.notify_one();
}
}
template<class T>
typename Bulker<T>::Container Bulker<T>::ConsumeMany()
{
std::unique_lock<std::mutex> lock (m_Mutex);
if (BOOST_UNLIKELY(m_NextConsumption == NullTimePoint())) {
UpdateNextConsumption();
}
auto deadline (m_NextConsumption);
m_CV.wait_until(lock, deadline, [this]() { return !m_Bulks.empty() && m_Bulks.front().size() == m_BulkSize; });
UpdateNextConsumption();
if (m_Bulks.empty()) {
return Container();
}
auto haystack (std::move(m_Bulks.front()));
m_Bulks.pop();
return haystack;
}
template<class T>
typename Bulker<T>::SizeType Bulker<T>::Size()
{
std::unique_lock<std::mutex> lock (m_Mutex);
return m_Bulks.empty() ? 0 : (m_Bulks.size() - 1u) * m_BulkSize + m_Bulks.back().size();
}
}
#endif /* BULKER_H */