![]() |
MySQL 26.10.0
Source Code Documentation
|
Bounded cache for prefix lookups with MRU/LRU recency tracking. More...
#include <ut0prefix_cache.h>
Classes | |
| struct | Entry |
Public Types | |
| using | sequence = std::basic_string< CharType > |
| using | sequence_view = std::basic_string_view< CharType > |
Public Member Functions | |
| prefix_cache (size_t capacity) | |
Create a cache with at most capacity stored prefixes. More... | |
| prefix_cache (const prefix_cache &)=delete | |
| prefix_cache & | operator= (const prefix_cache &)=delete |
| Value * | find_value_for_prefix_of (sequence_view &buffer) |
Find a cached value whose key is the longest prefix of buffer. More... | |
| bool | insert (sequence_view prefix, Value value) |
| Insert a new prefix/value pair as the most recently used entry. More... | |
Private Types | |
| using | Map = std::map< sequence, Entry, std::greater<> > |
| using | It = typename Map::iterator |
| using | Lru_list = std::list< It > |
| using | Lru_it = typename Lru_list::iterator |
Private Member Functions | |
| It | try_find (sequence_view buffer) |
| void | move_to_front (It it) |
| void | link_new_mru (It it) |
| void | enforce_capacity () |
Static Private Member Functions | |
| static bool | is_prefix (sequence_view key, sequence_view buffer) |
Private Attributes | |
| size_t | m_capacity |
| Map | m_map |
| Lru_list | m_lru |
Bounded cache for prefix lookups with MRU/LRU recency tracking.
The main use case is in parsers, which consume a prefix of a buffer, to produce a value, where the length of the prefix is not know up-front without parsing. This solves the case in which callers need to map owned prefixes to values, probe whether a cached prefix matches the beginning of a buffer, and evict old entries when the cache reaches a fixed size. Standard containers alone are not enough, because they do not combine prefix lookup, stable owned keys, and LRU eviction in one reusable abstraction.
Important limitation of this class (met by the parser usecase) is that it can work only with prefix-free languages, i.e. that is it doesn't support adding two prefixes such that one is a proper prefix of another.
|
private |
|
private |
|
private |
|
private |
| using ut::prefix_cache< CharType, Value >::sequence = std::basic_string<CharType> |
| using ut::prefix_cache< CharType, Value >::sequence_view = std::basic_string_view<CharType> |
|
inlineexplicit |
Create a cache with at most capacity stored prefixes.
| [in] | capacity | The maximum number of entries kept after each insertion |
|
delete |
|
inlineprivate |
|
inline |
Find a cached value whose key is the longest prefix of buffer.
If a match is found, the entry becomes most recently used and the buffer is trimmed to the leftover suffix.
| [in,out] | buffer | The input buffer. Trimmed to the leftover suffix on match. Otherwise kept intact. |
|
inline |
Insert a new prefix/value pair as the most recently used entry.
If prefix already exists, the cache is left unchanged. After a successful insertion, least recently used entries are evicted until the cache size is within capacity.
The caller must ensure that the language is prefix-free, i.e. that the prefix is not a proper prefix of any previously inserted one, nor vice-versa.
Invalidates pointers returned by find_value_for_prefix_of(..), as they might become evicted as a result of this call.
| [in] | prefix | key prefix. |
| [in] | value | value to store. |
|
inlinestaticprivate |
|
inlineprivate |
|
inlineprivate |
|
delete |
|
inlineprivate |
|
private |
|
private |
|
private |