Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::snapshot_reader Class Reference

Snapshot-based reader for frequently accessed data. More...

#include <thread_safe_container.h>

Collaboration diagram for kcenon::container::snapshot_reader:
Collaboration graph

Public Types

using snapshot_ptr = std::shared_ptr<const thread_safe_container::value_map>
 

Public Member Functions

 snapshot_reader (std::shared_ptr< thread_safe_container > container)
 
template<concepts::ValueVariantType T>
std::optional< T > get (std::string_view key) const
 Get value from snapshot (lock-protected read of snapshot)
 
void update_snapshot ()
 Update snapshot from container.
 

Private Attributes

std::shared_ptr< thread_safe_containercontainer_
 
snapshot_ptr snapshot_
 
std::shared_mutex snapshot_mutex_
 

Detailed Description

Snapshot-based reader for frequently accessed data.

Uses a snapshot pattern to provide read access with reduced lock contention. Note: This is not truly lock-free, as it uses a shared_mutex internally. The snapshot is updated explicitly, allowing multiple reads without acquiring the main container lock. This is useful for read-heavy workloads where slightly stale data is acceptable.

Definition at line 316 of file thread_safe_container.h.

Member Typedef Documentation

◆ snapshot_ptr

Constructor & Destructor Documentation

◆ snapshot_reader()

kcenon::container::snapshot_reader::snapshot_reader ( std::shared_ptr< thread_safe_container > container)
inlineexplicit

Definition at line 320 of file thread_safe_container.h.

321 : container_(container) {
323 }
void update_snapshot()
Update snapshot from container.
std::shared_ptr< thread_safe_container > container_

References update_snapshot().

Here is the call graph for this function:

Member Function Documentation

◆ get()

template<concepts::ValueVariantType T>
std::optional< T > kcenon::container::snapshot_reader::get ( std::string_view key) const
inline

Get value from snapshot (lock-protected read of snapshot)

Template Parameters
TThe expected type (must be a valid variant type)
Note
The snapshot may be stale; call update_snapshot() to refresh

Definition at line 331 of file thread_safe_container.h.

331 {
332 std::shared_lock lock(snapshot_mutex_);
333 if (!snapshot_) return std::nullopt;
334
335 auto it = snapshot_->find(std::string(key));
336 if (it != snapshot_->end()) {
337 return it->second.get<T>();
338 }
339 return std::nullopt;
340 }

References snapshot_, and snapshot_mutex_.

◆ update_snapshot()

void kcenon::container::snapshot_reader::update_snapshot ( )

Update snapshot from container.

Note
This acquires the container's lock and should be called periodically

Definition at line 352 of file thread_safe_container.cpp.

353 {
354 auto new_snapshot = std::make_shared<thread_safe_container::value_map>(
355 container_->bulk_read([](const auto& map) { return map; })
356 );
357
358 std::unique_lock lock(snapshot_mutex_);
359 snapshot_ = new_snapshot;
360 }

References container_, snapshot_, and snapshot_mutex_.

Referenced by snapshot_reader().

Here is the caller graph for this function:

Member Data Documentation

◆ container_

std::shared_ptr<thread_safe_container> kcenon::container::snapshot_reader::container_
private

Definition at line 349 of file thread_safe_container.h.

Referenced by update_snapshot().

◆ snapshot_

snapshot_ptr kcenon::container::snapshot_reader::snapshot_
private

Definition at line 350 of file thread_safe_container.h.

Referenced by get(), and update_snapshot().

◆ snapshot_mutex_

std::shared_mutex kcenon::container::snapshot_reader::snapshot_mutex_
mutableprivate

Definition at line 351 of file thread_safe_container.h.

Referenced by get(), and update_snapshot().


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