MySQL 8.0.30
Source Code Documentation
lf_hash.cc File Reference

extensible hash More...

#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <atomic>
#include "lf.h"
#include "m_ctype.h"
#include "my_atomic.h"
#include "my_bit.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys/mysys_priv.h"
#include "template_utils.h"

Classes

struct  LF_SLIST
 
struct  CURSOR
 

Macros

#define MAX_LOAD   1.0 /* average number of elements in a bucket */
 

Functions

template<class T >
static T * PTR (T *ptr)
 
template<class T >
static bool DELETED (T *ptr)
 
template<class T >
static T * SET_DELETED (T *ptr)
 
static int my_lfind (std::atomic< LF_SLIST * > *head, lf_cmp_func *cmp_func, CHARSET_INFO *cs, uint32 hashnr, const uchar *key, size_t keylen, CURSOR *cursor, LF_PINS *pins)
 
static int my_lfind_match (std::atomic< LF_SLIST * > *head, uint32 first_hashnr, uint32 last_hashnr, lf_hash_match_func *match, CURSOR *cursor, LF_PINS *pins)
 Search for list element satisfying condition specified by match function and position cursor on it. More...
 
static LF_SLISTlinsert (std::atomic< LF_SLIST * > *head, lf_cmp_func *cmp_func, CHARSET_INFO *cs, LF_SLIST *node, LF_PINS *pins, uint flags)
 
static int ldelete (std::atomic< LF_SLIST * > *head, lf_cmp_func *cmp_func, CHARSET_INFO *cs, uint32 hashnr, const uchar *key, uint keylen, LF_PINS *pins)
 
static LF_SLISTmy_lsearch (std::atomic< LF_SLIST * > *head, lf_cmp_func *cmp_func, CHARSET_INFO *cs, uint32 hashnr, const uchar *key, uint keylen, LF_PINS *pins)
 
static const ucharhash_key (const LF_HASH *hash, const uchar *record, size_t *length)
 
static uint calc_hash (LF_HASH *hash, const uchar *key, size_t keylen)
 
static int initialize_bucket (LF_HASH *, std::atomic< LF_SLIST * > *, uint, LF_PINS *)
 
static uint cset_hash_sort_adapter (const LF_HASH *hash, const uchar *key, size_t length)
 Adaptor function which allows to use hash function from character set with LF_HASH. More...
 
void lf_hash_init_impl (LF_HASH *hash, uint element_size, uint flags, uint key_offset, uint key_length, hash_get_key_function get_key, CHARSET_INFO *charset, lf_hash_func *hash_function, lf_cmp_func *cmp_function, lf_allocator_func *ctor, lf_allocator_func *dtor, lf_hash_init_func *init)
 
void lf_hash_init2 (LF_HASH *hash, uint element_size, uint flags, uint key_offset, uint key_length, hash_get_key_function get_key, CHARSET_INFO *charset, lf_hash_func *hash_function, lf_allocator_func *ctor, lf_allocator_func *dtor, lf_hash_init_func *init)
 
void lf_hash_init3 (LF_HASH *hash, uint element_size, uint flags, hash_get_key_function get_key, lf_hash_func *hash_function, lf_cmp_func *cmp_function, lf_allocator_func *ctor, lf_allocator_func *dtor, lf_hash_init_func *init)
 
void lf_hash_destroy (LF_HASH *hash)
 
int lf_hash_insert (LF_HASH *hash, LF_PINS *pins, const void *data)
 
int lf_hash_delete (LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen)
 
void * lf_hash_search (LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen)
 Find hash element corresponding to the key. More...
 
void * lf_hash_random_match (LF_HASH *hash, LF_PINS *pins, lf_hash_match_func *match, uint rand_val)
 Find random hash element which satisfies condition specified by match function. More...
 

Variables

const int LF_HASH_OVERHEAD = sizeof(LF_SLIST)
 
static const uchardummy_key = pointer_cast<const uchar *>("")
 

Detailed Description

extensible hash

Macro Definition Documentation

◆ MAX_LOAD

#define MAX_LOAD   1.0 /* average number of elements in a bucket */

Function Documentation

◆ calc_hash()

static uint calc_hash ( LF_HASH hash,
const uchar key,
size_t  keylen 
)
inlinestatic

◆ cset_hash_sort_adapter()

static uint cset_hash_sort_adapter ( const LF_HASH hash,
const uchar key,
size_t  length 
)
static

Adaptor function which allows to use hash function from character set with LF_HASH.

◆ DELETED()

template<class T >
static bool DELETED ( T *  ptr)
inlinestatic

◆ hash_key()

static const uchar * hash_key ( const LF_HASH hash,
const uchar record,
size_t *  length 
)
inlinestatic

◆ initialize_bucket()

static int initialize_bucket ( LF_HASH hash,
std::atomic< LF_SLIST * > *  node,
uint  bucket,
LF_PINS pins 
)
static

◆ ldelete()

static int ldelete ( std::atomic< LF_SLIST * > *  head,
lf_cmp_func cmp_func,
CHARSET_INFO cs,
uint32  hashnr,
const uchar key,
uint  keylen,
LF_PINS pins 
)
static

◆ lf_hash_delete()

int lf_hash_delete ( LF_HASH hash,
LF_PINS pins,
const void *  key,
uint  keylen 
)

◆ lf_hash_destroy()

void lf_hash_destroy ( LF_HASH hash)

◆ lf_hash_init2()

void lf_hash_init2 ( LF_HASH hash,
uint  element_size,
uint  flags,
uint  key_offset,
uint  key_length,
hash_get_key_function  get_key,
CHARSET_INFO charset,
lf_hash_func hash_function,
lf_allocator_func ctor,
lf_allocator_func dtor,
lf_hash_init_func init 
)

◆ lf_hash_init3()

void lf_hash_init3 ( LF_HASH hash,
uint  element_size,
uint  flags,
hash_get_key_function  get_key,
lf_hash_func hash_function,
lf_cmp_func cmp_function,
lf_allocator_func ctor,
lf_allocator_func dtor,
lf_hash_init_func init 
)

◆ lf_hash_init_impl()

void lf_hash_init_impl ( LF_HASH hash,
uint  element_size,
uint  flags,
uint  key_offset,
uint  key_length,
hash_get_key_function  get_key,
CHARSET_INFO charset,
lf_hash_func hash_function,
lf_cmp_func cmp_function,
lf_allocator_func ctor,
lf_allocator_func dtor,
lf_hash_init_func init 
)

◆ lf_hash_insert()

int lf_hash_insert ( LF_HASH hash,
LF_PINS pins,
const void *  data 
)

◆ lf_hash_random_match()

void * lf_hash_random_match ( LF_HASH hash,
LF_PINS pins,
lf_hash_match_func match,
uint  rand_val 
)

Find random hash element which satisfies condition specified by match function.

Parameters
hashHash to search element in.
pinsPins for calling thread to be used during search and for pinning its result.
matchPointer to match function. This function takes pointer to object stored in hash as parameter and returns 0 if object doesn't satisfy its condition (and non-0 value if it does).
rand_valRandom value to be used for selecting hash bucket from which search in sort-ordered list needs to be started.
Return values
Apointer to a random element matching condition.
NULL- if nothing is found
MY_LF_ERRPTR- OOM.
Note
This function follows the same pinning protocol as lf_hash_search(), i.e. uses pins[0..2]. On return pins[0..1] are removed and pins[2] is used to pin object found. It is also not removed in case when object is not found/error occurs but its value is undefined in this case. So calling lf_hash_unpin() is mandatory after call to this function in case of both success and failure.

◆ lf_hash_search()

void * lf_hash_search ( LF_HASH hash,
LF_PINS pins,
const void *  key,
uint  keylen 
)

Find hash element corresponding to the key.

Parameters
hashThe hash to search element in.
pinsPins for the calling thread which were earlier obtained from this hash using lf_hash_get_pins().
keyKey
keylenKey length
Return values
Apointer to an element with the given key (if a hash is not unique and there're many elements with this key - the "first" matching element).
NULL- if nothing is found
MY_LF_ERRPTR- if OOM
Note
Uses pins[0..2]. On return pins[0..1] are removed and pins[2] is used to pin object found. It is also not removed in case when object is not found/error occurs but pin value is undefined in this case. So calling lf_hash_unpin() is mandatory after call to this function in case of both success and failure.
See also
my_lsearch().

◆ linsert()

static LF_SLIST * linsert ( std::atomic< LF_SLIST * > *  head,
lf_cmp_func cmp_func,
CHARSET_INFO cs,
LF_SLIST node,
LF_PINS pins,
uint  flags 
)
static

◆ my_lfind()

static int my_lfind ( std::atomic< LF_SLIST * > *  head,
lf_cmp_func cmp_func,
CHARSET_INFO cs,
uint32  hashnr,
const uchar key,
size_t  keylen,
CURSOR cursor,
LF_PINS pins 
)
static

◆ my_lfind_match()

static int my_lfind_match ( std::atomic< LF_SLIST * > *  head,
uint32  first_hashnr,
uint32  last_hashnr,
lf_hash_match_func match,
CURSOR cursor,
LF_PINS pins 
)
static

Search for list element satisfying condition specified by match function and position cursor on it.

Parameters
headHead of the list to search in.
first_hashnrHash value to start search from.
last_hashnrHash value to stop search after.
matchMatch function.
cursorCursor to be position.
pinsLF_PINS for the calling thread to be used during search for pinning result.
Return values
0- not found
1- found

◆ my_lsearch()

static LF_SLIST * my_lsearch ( std::atomic< LF_SLIST * > *  head,
lf_cmp_func cmp_func,
CHARSET_INFO cs,
uint32  hashnr,
const uchar key,
uint  keylen,
LF_PINS pins 
)
static

◆ PTR()

template<class T >
static T * PTR ( T *  ptr)
inlinestatic

◆ SET_DELETED()

template<class T >
static T * SET_DELETED ( T *  ptr)
inlinestatic

Variable Documentation

◆ dummy_key

const uchar* dummy_key = pointer_cast<const uchar *>("")
static

◆ LF_HASH_OVERHEAD

const int LF_HASH_OVERHEAD = sizeof(LF_SLIST)