|
LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
|
High-performance, thread-safe container for managing large collections of LogEvent objects. More...
#include <EventsContainer.hpp>


Public Types | |
| using | FileMetadata = std::vector< std::pair< std::string, std::string > > |
| using | ViewPtr = std::shared_ptr< IView > |
| Shared pointer type for views. | |
Public Member Functions | |
| EventsContainer () | |
| Default constructor. | |
| virtual | ~EventsContainer () |
| Destructor. | |
| void | AddEvent (LogEvent &&event) |
| Adds a new event to the container. | |
| void | AddEventBatch (std::vector< std::pair< int, LogEvent::EventItems > > &&eventBatch) |
| Adds a batch of new events to the container. | |
| const LogEvent & | GetEvent (size_t index) |
| Retrieves an event by index. | |
| void | AddItem (LogEvent &&item) override |
| Adds a new item to the model. | |
| size_t | Size () const override |
| Gets the number of events in the container. | |
| void | SetFileMetadata (FileMetadata metadata) |
| Replaces the file metadata. Cleared by Clear(); MergeEvents() keeps it. | |
| FileMetadata | GetFileMetadata () const |
| std::uint64_t | Generation () const noexcept |
| Counter bumped whenever existing indices stop referring to the same events: Clear() of a non-empty container and MergeEvents(). | |
| std::shared_lock< std::shared_mutex > | LockAgainstInvalidation () const |
| Keeps the stored events in place while the returned lock is held. | |
| void | SetCurrentItem (const int index) override |
| Sets the currently selected item index. | |
| int | GetCurrentItemIndex () override |
| Gets the currently selected item index. | |
| void | Clear () override |
| Removes all events from the container. | |
| void | MergeEvents (EventsContainer &other, const std::string &existingAlias, const std::string &newAlias, const std::string ×tampField="timestamp") |
| Merges events from another container by timestamp (stable). | |
| db::LogEvent & | GetItem (size_t index) override |
| Gets an event by index. | |
| const db::LogEvent & | GetItem (size_t index) const override |
| void | SuspendNotifications () noexcept |
| Temporarily disables view notifications during bulk loading. | |
| void | ResumeNotifications () noexcept |
| Re-enables view notifications after bulk loading. | |
| virtual void | RegisterOndDataUpdated (IView *view) |
| void | RegisterView (ViewPtr view) |
| Registers a view as observer with automatic cleanup. | |
| void | UnregisterView (const IView *view) |
| Unregisters a view from observations. | |
| size_t | GetViewCount () const |
| Gets count of currently active observers. | |
Protected Member Functions | |
| void | NotifyCurrentIndexUpdated (int index) |
| Notifies all views of current item index change. | |
| void | CleanupDeadObservers () |
| Manually triggers cleanup of dead observers. | |
Protected Attributes | |
| std::vector< IView * > | m_views |
Private Member Functions | |
| void | NotifyDataChanged () override |
| void | CleanupDeadViews () |
| Internal method to clean up expired weak pointers. | |
Private Attributes | |
| std::deque< LogEvent > | m_data |
| FileMetadata | m_fileMetadata |
| See GetFileMetadata(). Guarded by m_mutex. | |
| std::atomic< int > | m_currentItem {-1} |
| std::shared_mutex | m_mutex |
| std::shared_mutex | m_invalidationMutex |
| See LockAgainstInvalidation(). Taken before m_mutex, never after it. | |
| std::atomic< bool > | m_notificationsEnabled {true} |
| std::atomic< std::uint64_t > | m_generation {0} |
| See Generation(). | |
| std::shared_mutex | m_viewsMutex |
| Storage for observer weak pointers - automatically manages cleanup. | |
| std::vector< std::weak_ptr< IView > > | m_weakViews |
High-performance, thread-safe container for managing large collections of LogEvent objects.
EventsContainer provides efficient storage and access to LogEvent collections, optimized for handling very large log files (potentially millions of events). The container supports random access, iteration, and provides methods for virtual list control integration.
std::shared_lock<std::shared_mutex> lock(m_eventMutex);std::unique_lock<std::shared_mutex> lock(m_eventMutex);Adding individual events:
Batch operations for efficiency:
Thread-safe concurrent access:
Merging events from multiple sources:
| using db::EventsContainer::FileMetadata = std::vector<std::pair<std::string, std::string> > |
Ordered key → value pairs a parser read from the file header (e.g. the # Test Case : … block of a safeAPI merged log). Empty when the format has no header.
|
inherited |
Shared pointer type for views.
| db::EventsContainer::EventsContainer | ( | ) |
Default constructor.
Creates an empty container ready to accept LogEvent objects.

|
virtual |
Destructor.
Cleans up resources and clears the internal event storage.

| void db::EventsContainer::AddEvent | ( | LogEvent && | event | ) |
Adds a new event to the container.
| event | The LogEvent to add (moved into the container) |
Moves the event into the internal vector storage (thread-safe).
Uses move semantics to avoid copying large event data structures. The vector will automatically manage memory allocation and resizing. Thread-safe: Uses exclusive lock for modification.


| void db::EventsContainer::AddEventBatch | ( | std::vector< std::pair< int, LogEvent::EventItems > > && | eventBatch | ) |
Adds a batch of new events to the container.
| eventBatch | A vector of pairs, each containing an integer and a LogEvent::EventItems object, representing the batch of events to add (moved into the container) |


|
overridevirtual |
Adds a new item to the model.
| item | The LogEvent to add (moved into the model) |
Implements mvc::IModel.

|
inlineprotectedinherited |
Manually triggers cleanup of dead observers.
Can be called explicitly to prune expired weak_ptr entries. Internally, cleanup is also performed on every RegisterView and NotifyDataChanged call.

|
inlineprivateinherited |
Internal method to clean up expired weak pointers.
Called during registration and notifications to remove dead observers. Must be called with unique_lock held.


|
overridevirtual |
Removes all events from the container.
After this call, Size() returns 0 and the container is ready for new events.
Clears all stored events and resets selection (thread-safe).
Removes all events from the container and resets the current item selection to indicate no selection. This method is typically called when loading a new file or clearing the current session. Thread-safe: Uses exclusive lock for modification.
Implements mvc::IModel.


|
inlinenoexcept |
Counter bumped whenever existing indices stop referring to the same events: Clear() of a non-empty container and MergeEvents().
Appending events does not change it, so index caches can tell "more events" from "a different data set".

|
overridevirtual |
Gets the currently selected item index.
Implements mvc::IModel.

| const LogEvent & db::EventsContainer::GetEvent | ( | size_t | index | ) |
Retrieves an event by index.
| index | The zero-based index of the event to retrieve |
| std::out_of_range | if index is >= Size() |


| EventsContainer::FileMetadata db::EventsContainer::GetFileMetadata | ( | ) | const |

|
overridevirtual |
Implements mvc::IModel.
|
overridevirtual |
Gets an event by index.
| index | The zero-based index of the event to retrieve |
| std::out_of_range | if index is >= Size() |
Implements mvc::IModel.

|
inlineinherited |
Gets count of currently active observers.
|
inline |
Keeps the stored events in place while the returned lock is held.
Clear() and MergeEvents() wait until every such lock is released; reading and appending (AddEvent / AddEventBatch) are not blocked. Meant for a background reader that scans the container with GetEvent() while the GUI thread may clear or merge it. Never call Clear() or MergeEvents() on a thread that holds this lock.

| void db::EventsContainer::MergeEvents | ( | EventsContainer & | other, |
| const std::string & | existingAlias, | ||
| const std::string & | newAlias, | ||
| const std::string & | timestampField = "timestamp" |
||
| ) |
Merges events from another container by timestamp (stable).
Performs a stable merge of two already ordered event sequences using the provided timestamp field.
Key property: events with the same timestamp keep their original order within each source container (no reordering of existing events).
Events missing the timestamp field are treated as having an empty timestamp and are appended after all events with valid timestamps (while still preserving their internal order).
| other | Container with events to merge |
| existingAlias | Identifier for events already in this container |
| newAlias | Identifier for events from the 'other' container |
| timestampField | Name of the field containing timestamp (e.g., "timestamp", "time") |
Merges events from another container, sorted by timestamp.
This method combines events from both containers and sorts them by the specified timestamp field. Events from this container get existingAlias, and events from the 'other' container get newAlias as their source. Events without a valid timestamp will be appended at the end.
| other | The container to merge from |
| existingAlias | The source identifier for existing events |
| newAlias | The source identifier for new events |
| timestampField | The field name containing the timestamp |
Thread-safe: Uses exclusive lock for modification.


|
inlineprotectedinherited |
Notifies all views of current item index change.
| index | The new current item index |


|
overrideprivatevirtual |
Override to disambiguate between IModel and IModelObservable versions Calls both old (raw pointer) and new (weak_ptr) notification patterns
Reimplemented from mvc::IModel.


|
inlinevirtualinherited |

|
inlineinherited |
Registers a view as observer with automatic cleanup.
The view is stored internally as weak_ptr so it's automatically removed when destroyed. No dangling pointers.
| view | Shared pointer to view to register |

|
inlinenoexcept |
Re-enables view notifications after bulk loading.

|
overridevirtual |
Sets the currently selected item index.
Updates the internal current item index for virtual list control synchronization.
| index | The index of the item to set as current |
Updates the current selection index.
This method is typically called by virtual list controls to maintain synchronization between the UI selection and the data model.
Implements mvc::IModel.


| void db::EventsContainer::SetFileMetadata | ( | FileMetadata | metadata | ) |
Replaces the file metadata. Cleared by Clear(); MergeEvents() keeps it.

|
overridevirtual |
Gets the number of events in the container.
Returns the current number of stored events (thread-safe).
Delegates to the underlying vector's size() method for O(1) performance. Thread-safe: Uses shared lock for concurrent reads.
Implements mvc::IModel.

|
inlinenoexcept |
Temporarily disables view notifications during bulk loading.
Call before a background-thread bulk insert to prevent Qt widget callbacks from being invoked on the wrong thread. Resume with ResumeNotifications() and then refresh the view manually.

|
inlineinherited |
Unregisters a view from observations.
| view | Pointer to the view to unregister |
Safe if view already unregistered or destroyed.

|
private |
Currently selected item index (-1 = no selection). Declared atomic so that GetCurrentItemIndex() / SetCurrentItem() are safe without acquiring m_mutex.
|
private |
Internal storage for events. A deque (not a vector) so that appending never relocates existing events: references returned by GetEvent() to readers on other threads stay valid while a parser appends.
|
private |
See GetFileMetadata(). Guarded by m_mutex.
|
private |
See Generation().
|
mutableprivate |
See LockAgainstInvalidation(). Taken before m_mutex, never after it.
|
mutableprivate |
Reader-writer lock for thread safety
|
private |
When false, AddEvent/AddEventBatch skip NotifyDataChanged so that Qt widget calls are not made from a background parser thread.
|
protectedinherited |
|
mutableprivateinherited |
Storage for observer weak pointers - automatically manages cleanup.
|
privateinherited |