LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
db::EventsContainer Class Reference

High-performance, thread-safe container for managing large collections of LogEvent objects. More...

#include <EventsContainer.hpp>

Inheritance diagram for db::EventsContainer:
Inheritance graph
Collaboration diagram for db::EventsContainer:
Collaboration graph

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 &timestampField="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
 

Detailed Description

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.

Design Goals
  • Performance: O(1) random access, efficient batch operations
  • Thread Safety: Safe for concurrent reads and exclusive writes
  • Integration: Works as IModel for MVC-based UI rendering
  • Scalability: Tested with millions of events
Performance Characteristics
  • Random access: O(1)
  • Insertion at end: O(1) amortized
  • Insertion via AddEventBatch: O(k) amortized, k = batch size
  • Memory usage: Optimized for large datasets with no fragmentation
  • Merge operation: O(n + m) for n + m total events
Thread Safety Model
Thread-safe: All public methods are protected by internal std::shared_mutex locks.
  • Multiple Concurrent Readers: Multiple threads can call GetEvent(), Size(), etc. simultaneously (shared_lock allows concurrent access)
  • Exclusive Writers: Write operations (AddEvent, AddEventBatch, Clear, MergeEvents) use unique_lock to ensure only one writer at a time
  • Safe Parser Integration: Parser threads can add events while UI thread reads
  • Lock Strategy Implementation:
    • Read operations: std::shared_lock<std::shared_mutex> lock(m_eventMutex);
    • Write operations: std::unique_lock<std::shared_mutex> lock(m_eventMutex);
Usage Examples

Adding individual events:

// Create and add event (moved into container)
db::LogEvent event(1, {
{"timestamp", "2025-03-15T12:00:00Z"},
{"level", "INFO"},
{"message", "Application started"}
});
container.AddEvent(std::move(event));
// Access the stored event
const auto& stored = container.GetEvent(0);
std::cout << stored.findByKey("message") << std::endl;
High-performance, thread-safe container for managing large collections of LogEvent objects.
Definition EventsContainer.hpp:129
void AddEvent(LogEvent &&event)
Adds a new event to the container.
Definition EventsContainer.cpp:28
const LogEvent & GetEvent(size_t index)
Retrieves an event by index.
Definition EventsContainer.cpp:64
Represents a single, structured log event with key-value data.
Definition LogEvent.hpp:97
const std::string findByKey(std::string_view key) const
Finds a value by its key in the event's items.
Definition LogEvent.cpp:25

Batch operations for efficiency:

std::vector<std::pair<int, db::LogEvent::EventItems>> batch;
for (int i = 0; i < 1000; ++i) {
batch.push_back({i, {
{"timestamp", "2025-03-15"},
{"sequence", std::to_string(i)}
}});
}
container.AddEventBatch(std::move(batch)); // Efficient bulk insertion
std::cout << "Container size: " << container.Size() << std::endl;
size_t Size() const override
Gets the number of events in the container.
Definition EventsContainer.cpp:151
void AddEventBatch(std::vector< std::pair< int, LogEvent::EventItems > > &&eventBatch)
Adds a batch of new events to the container.
Definition EventsContainer.cpp:47

Thread-safe concurrent access:

// Parser thread
std::thread parser([&container](const std::string& filename) {
// Parse and add events...
for (const auto& event_data : parsed_events) {
container.AddEvent(db::LogEvent(...)); // Thread-safe
}
});
// UI thread (concurrent read)
std::thread ui([&container]() {
for (size_t i = 0; i < container.Size(); ++i) {
const auto& event = container.GetEvent(i); // Thread-safe read
// Render event in UI...
}
});
Data parsing components and interfaces.
Definition IControler.hpp:22
Definition IErrorPresenter.hpp:5

Merging events from multiple sources:

// Parse into separate containers...
// Merge events by timestamp, preserving source information
log_file_1.MergeEvents(log_file_2, "app.log", "system.log", "timestamp");
// Events now interleaved by timestamp, with source identifiers preserved
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
See also
LogEvent for individual event representation
mvc::IModel for model interface details
FilterManager for filtering merged events

Member Typedef Documentation

◆ FileMetadata

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.

◆ ViewPtr

using mvc::IModelObservable::ViewPtr = std::shared_ptr<IView>
inherited

Shared pointer type for views.

Constructor & Destructor Documentation

◆ EventsContainer()

db::EventsContainer::EventsContainer ( )

Default constructor.

Creates an empty container ready to accept LogEvent objects.

Here is the call graph for this function:

◆ ~EventsContainer()

db::EventsContainer::~EventsContainer ( )
virtual

Destructor.

Cleans up resources and clears the internal event storage.

Here is the call graph for this function:

Member Function Documentation

◆ AddEvent()

void db::EventsContainer::AddEvent ( LogEvent &&  event)

Adds a new event to the container.

Parameters
eventThe LogEvent to add (moved into the container)
Note
The event is moved to avoid unnecessary copying
Complexity
O(1) amortized

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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddEventBatch()

void db::EventsContainer::AddEventBatch ( std::vector< std::pair< int, LogEvent::EventItems > > &&  eventBatch)

Adds a batch of new events to the container.

Parameters
eventBatchA vector of pairs, each containing an integer and a LogEvent::EventItems object, representing the batch of events to add (moved into the container)
Note
The eventBatch is moved to avoid unnecessary copying
Complexity
O(k) amortized, where k is the number of events in the batch
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddItem()

void db::EventsContainer::AddItem ( LogEvent &&  item)
overridevirtual

Adds a new item to the model.

Parameters
itemThe LogEvent to add (moved into the model)
Note
This method is used to add new events to the model.

Implements mvc::IModel.

Here is the caller graph for this function:

◆ CleanupDeadObservers()

void mvc::IModelObservable::CleanupDeadObservers ( )
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.

Here is the call graph for this function:

◆ CleanupDeadViews()

void mvc::IModelObservable::CleanupDeadViews ( )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void db::EventsContainer::Clear ( )
overridevirtual

Removes all events from the container.

After this call, Size() returns 0 and the container is ready for new events.

Complexity
O(n) where n is the number of stored 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Generation()

std::uint64_t db::EventsContainer::Generation ( ) const
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".

Here is the caller graph for this function:

◆ GetCurrentItemIndex()

int db::EventsContainer::GetCurrentItemIndex ( )
overridevirtual

Gets the currently selected item index.

Returns
int The index of the currently selected item, or -1 if none selected

Implements mvc::IModel.

Here is the caller graph for this function:

◆ GetEvent()

const LogEvent & db::EventsContainer::GetEvent ( size_t  index)

Retrieves an event by index.

Parameters
indexThe zero-based index of the event to retrieve
Returns
const LogEvent& Reference to the event at the specified index
Exceptions
std::out_of_rangeif index is >= Size()
Note
The reference stays valid while events are appended (AddEvent / AddEventBatch); it is invalidated only by Clear() and MergeEvents().
Complexity
O(1)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFileMetadata()

EventsContainer::FileMetadata db::EventsContainer::GetFileMetadata ( ) const
Here is the caller graph for this function:

◆ GetItem() [1/2]

const LogEvent & db::EventsContainer::GetItem ( size_t  index) const
overridevirtual

Implements mvc::IModel.

◆ GetItem() [2/2]

LogEvent & db::EventsContainer::GetItem ( size_t  index)
overridevirtual

Gets an event by index.

Parameters
indexThe zero-based index of the event to retrieve
Returns
LogEvent& Reference to the event at the specified index
Exceptions
std::out_of_rangeif index is >= Size()

Implements mvc::IModel.

Here is the caller graph for this function:

◆ GetViewCount()

size_t mvc::IModelObservable::GetViewCount ( ) const
inlineinherited

Gets count of currently active observers.

Returns
Number of non-expired weak_ptr observers

◆ LockAgainstInvalidation()

std::shared_lock< std::shared_mutex > db::EventsContainer::LockAgainstInvalidation ( ) const
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.

Here is the caller graph for this function:

◆ MergeEvents()

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).

Parameters
otherContainer with events to merge
existingAliasIdentifier for events already in this container
newAliasIdentifier for events from the 'other' container
timestampFieldName of the field containing timestamp (e.g., "timestamp", "time")
Note
All existing events will have their source set to existingAlias
All events from 'other' will have their source set to newAlias

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.

Parameters
otherThe container to merge from
existingAliasThe source identifier for existing events
newAliasThe source identifier for new events
timestampFieldThe field name containing the timestamp

Thread-safe: Uses exclusive lock for modification.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NotifyCurrentIndexUpdated()

void mvc::IModelObservable::NotifyCurrentIndexUpdated ( int  index)
inlineprotectedinherited

Notifies all views of current item index change.

Parameters
indexThe new current item index
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NotifyDataChanged()

void db::EventsContainer::NotifyDataChanged ( )
overrideprivatevirtual

Override to disambiguate between IModel and IModelObservable versions Calls both old (raw pointer) and new (weak_ptr) notification patterns

Reimplemented from mvc::IModel.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterOndDataUpdated()

virtual void mvc::IModel::RegisterOndDataUpdated ( IView *  view)
inlinevirtualinherited
Here is the caller graph for this function:

◆ RegisterView()

void mvc::IModelObservable::RegisterView ( ViewPtr  view)
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.

Parameters
viewShared pointer to view to register
Example
auto container = std::make_shared<EventsContainer>();
auto view = std::make_shared<ItemDetailsView>();
container->RegisterView(view); // Safe lifetime management
view.reset(); // View destroyed
container->NotifyDataChanged(); // Safe: weak_ptr detects deletion
Here is the call graph for this function:

◆ ResumeNotifications()

void db::EventsContainer::ResumeNotifications ( )
inlinenoexcept

Re-enables view notifications after bulk loading.

Here is the caller graph for this function:

◆ SetCurrentItem()

void db::EventsContainer::SetCurrentItem ( const int  index)
overridevirtual

Sets the currently selected item index.

Updates the internal current item index for virtual list control synchronization.

Parameters
indexThe index of the item to set as current
Note
This method is used for UI synchronization purposes

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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetFileMetadata()

void db::EventsContainer::SetFileMetadata ( FileMetadata  metadata)

Replaces the file metadata. Cleared by Clear(); MergeEvents() keeps it.

Here is the caller graph for this function:

◆ Size()

size_t db::EventsContainer::Size ( ) const
overridevirtual

Gets the number of events in the container.

Returns
size_t The number of stored events
Complexity
O(1)

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.

Here is the caller graph for this function:

◆ SuspendNotifications()

void db::EventsContainer::SuspendNotifications ( )
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.

Here is the caller graph for this function:

◆ UnregisterView()

void mvc::IModelObservable::UnregisterView ( const IView *  view)
inlineinherited

Unregisters a view from observations.

Parameters
viewPointer to the view to unregister

Safe if view already unregistered or destroyed.

Here is the call graph for this function:

Member Data Documentation

◆ m_currentItem

std::atomic<int> db::EventsContainer::m_currentItem {-1}
private

Currently selected item index (-1 = no selection). Declared atomic so that GetCurrentItemIndex() / SetCurrentItem() are safe without acquiring m_mutex.

◆ m_data

std::deque<LogEvent> db::EventsContainer::m_data
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.

◆ m_fileMetadata

FileMetadata db::EventsContainer::m_fileMetadata
private

See GetFileMetadata(). Guarded by m_mutex.

◆ m_generation

std::atomic<std::uint64_t> db::EventsContainer::m_generation {0}
private

◆ m_invalidationMutex

std::shared_mutex db::EventsContainer::m_invalidationMutex
mutableprivate

See LockAgainstInvalidation(). Taken before m_mutex, never after it.

◆ m_mutex

std::shared_mutex db::EventsContainer::m_mutex
mutableprivate

Reader-writer lock for thread safety

◆ m_notificationsEnabled

std::atomic<bool> db::EventsContainer::m_notificationsEnabled {true}
private

When false, AddEvent/AddEventBatch skip NotifyDataChanged so that Qt widget calls are not made from a background parser thread.

◆ m_views

std::vector<IView*> mvc::IModel::m_views
protectedinherited

◆ m_viewsMutex

std::shared_mutex mvc::IModelObservable::m_viewsMutex
mutableprivateinherited

Storage for observer weak pointers - automatically manages cleanup.

◆ m_weakViews

std::vector<std::weak_ptr<IView> > mvc::IModelObservable::m_weakViews
privateinherited

The documentation for this class was generated from the following files: