///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2025 Equinor ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #pragma once #include #include #include #include #include //================================================================================================== /// //================================================================================================== template class RiaKeyValueStore { public: RiaKeyValueStore() = default; // Store a complete array for a key (overwrites existing data) bool set( const std::string& key, const std::vector& values ) { std::scoped_lock lock( m_mutex ); m_store[key] = values; return true; } // Get the entire array for a key std::optional> get( const std::string& key ) const { std::scoped_lock lock( m_mutex ); if ( auto it = m_store.find( key ); it != m_store.end() ) { return it->second; } return std::nullopt; } // Remove an entire key-value pair bool remove( const std::string& key ) { std::scoped_lock lock( m_mutex ); return m_store.erase( key ) > 0; } // Check if a key exists bool exists( const std::string& key ) const { std::scoped_lock lock( m_mutex ); return m_store.find( key ) != m_store.end(); } // Get all keys std::vector keys() const { std::scoped_lock lock( m_mutex ); std::vector result; result.reserve( m_store.size() ); for ( const auto& [key, _] : m_store ) { result.push_back( key ); } return result; } // Clear all key-value pairs void clear() { std::scoped_lock lock( m_mutex ); m_store.clear(); } private: std::unordered_map> m_store; mutable std::mutex m_mutex; };