| 1 |
/* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#ifndef CACHE_INCLUDED |
| 25 |
#define CACHE_INCLUDED |
| 26 |
|
| 27 |
#include <unordered_map> |
| 28 |
|
| 29 |
#include "components/keyrings/common/data/data.h" /* data::Data */ |
| 30 |
#include "components/keyrings/common/data/meta.h" /* meta::Metadata */ |
| 31 |
|
| 32 |
namespace keyring_common { |
| 33 |
|
| 34 |
namespace cache { |
| 35 |
|
| 36 |
template <typename Data_extension> |
| 37 |
using Cache = |
| 38 |
std::unordered_map<meta::Metadata, Data_extension, meta::Metadata::Hash>; |
| 39 |
|
| 40 |
template <typename Data_extension = data::Data> |
| 41 |
class Datacache final { |
| 42 |
public: |
| 43 |
/** Constructor */ |
| 44 |
explicit Datacache() { version_ = 0; } |
| 45 |
|
| 46 |
/** Disable copy */ |
| 47 |
Datacache(const Datacache &src) = delete; |
| 48 |
Datacache(Datacache &&src) = delete; |
| 49 |
Datacache &operator=(const Datacache &src) = delete; |
| 50 |
Datacache &operator=(Datacache &&src) = delete; |
| 51 |
|
| 52 |
/** Destructor */ |
| 53 |
~Datacache() = default; |
| 54 |
|
| 55 |
/** |
| 56 |
Retrieve an element from cache |
| 57 |
@param [in] metadata Key to search data |
| 58 |
@param [out] data Fetched data. Can be empty. |
| 59 |
|
| 60 |
@returns status of find operation |
| 61 |
@retval true Success. data contains retrieved data. |
| 62 |
@retval false Failure. data may not contain a valid value. |
| 63 |
*/ |
| 64 |
bool get(const meta::Metadata metadata, Data_extension &data) const { |
| 65 |
auto it = cache_.find(metadata); |
| 66 |
if (it == cache_.end()) return false; |
| 67 |
data = it->second; |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
Store and element in cache |
| 73 |
@param [in] metadata Key to store data |
| 74 |
@param [in] data Actual data. Can be empty. |
| 75 |
|
| 76 |
@returns status of insert operation |
| 77 |
@retval true Success |
| 78 |
@retval false Error. Element already exists in the cache. |
| 79 |
*/ |
| 80 |
bool store(const meta::Metadata metadata, const Data_extension data) { |
| 81 |
bool ok = cache_.insert({metadata, data}).second; |
| 82 |
if (ok) ++version_; |
| 83 |
return ok; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
Remove an entry from cache |
| 88 |
@param [in] metadata Key to entry to be erased |
| 89 |
|
| 90 |
@returns status of find operation |
| 91 |
@retval true Success. Data removed successfully. |
| 92 |
@retval false Failure. Either key is not present or removal failed. |
| 93 |
*/ |
| 94 |
bool erase(const meta::Metadata metadata) { |
| 95 |
bool removed = cache_.erase(metadata) != 0; |
| 96 |
if (removed) ++version_; |
| 97 |
return removed; |
| 98 |
} |
| 99 |
|
| 100 |
/** Clear the cache */ |
| 101 |
void clear() { cache_.clear(); } |
| 102 |
|
| 103 |
/** Check if cache is empty */ |
| 104 |
bool empty() const { return cache_.empty(); } |
| 105 |
|
| 106 |
/** Get size */ |
| 107 |
size_t size() const { return cache_.size(); } |
| 108 |
|
| 109 |
/** Get cache version */ |
| 110 |
size_t version() const { return version_; } |
| 111 |
|
| 112 |
/* Iterators */ |
| 113 |
typename Cache<Data_extension>::const_iterator begin() const { |
| 114 |
return cache_.cbegin(); |
| 115 |
} |
| 116 |
typename Cache<Data_extension>::const_iterator end() const { |
| 117 |
return cache_.cend(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
Retrieve iterator at an element from cache |
| 122 |
@param [in] metadata Key to search data |
| 123 |
*/ |
| 124 |
typename Cache<Data_extension>::const_iterator at( |
| 125 |
const meta::Metadata metadata) const { |
| 126 |
return cache_.find(metadata); |
| 127 |
} |
| 128 |
|
| 129 |
private: |
| 130 |
/** Sensitive data cache */ |
| 131 |
Cache<Data_extension> cache_; |
| 132 |
/** Cache version */ |
| 133 |
size_t version_{0}; |
| 134 |
}; |
| 135 |
|
| 136 |
} // namespace cache |
| 137 |
} // namespace keyring_common |
| 138 |
|
| 139 |
#endif // !CACHE_INCLUDED |