LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
EventsContainer.hpp
Go to the documentation of this file.
1
8#pragma once
9#include "LogEvent.hpp"
10#include "IModel.hpp"
11#include "IModelObservable.hpp"
12#include <atomic>
13#include <cstdint>
14#include <deque>
15#include <shared_mutex>
16#include <string>
17#include <utility>
18#include <vector>
19
24namespace db
25{
26
129{
130 public:
142 virtual ~EventsContainer();
143
152 void AddEvent(LogEvent&& event);
153
164 void AddEventBatch(
165 std::vector<std::pair<int, LogEvent::EventItems>>&& eventBatch);
166
178 [[nodiscard]] const LogEvent& GetEvent(size_t index);
179
180 public: // IModel interface
185 void AddItem(LogEvent&& item) override;
186
194 size_t Size() const override;
195
199 using FileMetadata = std::vector<std::pair<std::string, std::string>>;
200
202 void SetFileMetadata(FileMetadata metadata);
203 [[nodiscard]] FileMetadata GetFileMetadata() const;
204
212 [[nodiscard]] std::uint64_t Generation() const noexcept
213 {
214 return m_generation.load(std::memory_order_acquire);
215 }
216
226 [[nodiscard]] std::shared_lock<std::shared_mutex> LockAgainstInvalidation() const
227 {
228 return std::shared_lock<std::shared_mutex>(m_invalidationMutex);
229 }
230
240 void SetCurrentItem(const int index) override;
241
248 int GetCurrentItemIndex() override;
249
258 void Clear() override;
259
280 void MergeEvents(EventsContainer& other,
281 const std::string& existingAlias,
282 const std::string& newAlias,
283 const std::string& timestampField = "timestamp");
284
292 db::LogEvent& GetItem(size_t index) override;
293 const db::LogEvent& GetItem(size_t index) const override;
294
302 void SuspendNotifications() noexcept
303 {
304 m_notificationsEnabled.store(false, std::memory_order_release);
305 }
306
310 void ResumeNotifications() noexcept
311 {
312 m_notificationsEnabled.store(true, std::memory_order_release);
313 }
314
315 private:
318 void NotifyDataChanged() override;
319
323 std::deque<LogEvent> m_data;
329 std::atomic<int> m_currentItem {-1};
330 mutable std::shared_mutex m_mutex;
332 mutable std::shared_mutex m_invalidationMutex;
335 std::atomic<bool> m_notificationsEnabled{true};
337 std::atomic<std::uint64_t> m_generation {0};
338};
339
340} // namespace db
Modern C++20 observer pattern with automatic lifetime management.
Defines the LogEvent class for representing structured log entries.
High-performance, thread-safe container for managing large collections of LogEvent objects.
Definition EventsContainer.hpp:129
std::shared_lock< std::shared_mutex > LockAgainstInvalidation() const
Keeps the stored events in place while the returned lock is held.
Definition EventsContainer.hpp:226
void SuspendNotifications() noexcept
Temporarily disables view notifications during bulk loading.
Definition EventsContainer.hpp:302
void MergeEvents(EventsContainer &other, const std::string &existingAlias, const std::string &newAlias, const std::string &timestampField="timestamp")
Merges events from another container by timestamp (stable).
Definition EventsContainer.cpp:175
void AddEvent(LogEvent &&event)
Adds a new event to the container.
Definition EventsContainer.cpp:28
FileMetadata GetFileMetadata() const
Definition EventsContainer.cpp:169
int GetCurrentItemIndex() override
Gets the currently selected item index.
Definition EventsContainer.cpp:70
size_t Size() const override
Gets the number of events in the container.
Definition EventsContainer.cpp:151
void SetCurrentItem(const int index) override
Sets the currently selected item index.
Definition EventsContainer.cpp:75
std::shared_mutex m_invalidationMutex
See LockAgainstInvalidation(). Taken before m_mutex, never after it.
Definition EventsContainer.hpp:332
FileMetadata m_fileMetadata
See GetFileMetadata(). Guarded by m_mutex.
Definition EventsContainer.hpp:325
const LogEvent & GetEvent(size_t index)
Retrieves an event by index.
Definition EventsContainer.cpp:64
void AddItem(LogEvent &&item) override
Adds a new item to the model.
Definition EventsContainer.cpp:93
void ResumeNotifications() noexcept
Re-enables view notifications after bulk loading.
Definition EventsContainer.hpp:310
std::vector< std::pair< std::string, std::string > > FileMetadata
Definition EventsContainer.hpp:199
virtual ~EventsContainer()
Destructor.
Definition EventsContainer.cpp:23
void AddEventBatch(std::vector< std::pair< int, LogEvent::EventItems > > &&eventBatch)
Adds a batch of new events to the container.
Definition EventsContainer.cpp:47
std::atomic< std::uint64_t > m_generation
See Generation().
Definition EventsContainer.hpp:337
void Clear() override
Removes all events from the container.
Definition EventsContainer.cpp:113
std::shared_mutex m_mutex
Definition EventsContainer.hpp:330
void NotifyDataChanged() override
Definition EventsContainer.cpp:338
std::atomic< bool > m_notificationsEnabled
Definition EventsContainer.hpp:335
std::deque< LogEvent > m_data
Definition EventsContainer.hpp:323
std::atomic< int > m_currentItem
Definition EventsContainer.hpp:329
EventsContainer()
Default constructor.
Definition EventsContainer.cpp:18
void SetFileMetadata(FileMetadata metadata)
Replaces the file metadata. Cleared by Clear(); MergeEvents() keeps it.
Definition EventsContainer.cpp:163
std::uint64_t Generation() const noexcept
Counter bumped whenever existing indices stop referring to the same events: Clear() of a non-empty co...
Definition EventsContainer.hpp:212
db::LogEvent & GetItem(size_t index) override
Gets an event by index.
Definition EventsContainer.cpp:101
Represents a single, structured log event with key-value data.
Definition LogEvent.hpp:97
Improved IModel with weak_ptr observers for automatic cleanup.
Definition IModelObservable.hpp:52
Definition IModel.hpp:13
Database and data model components for the LogViewer application.
Definition EventsContainer.cpp:17