A lock-free hash table that can be used in multi-thread or multi-process programs.
More...
|
| CAtomicHashTable () |
| Prepare an uninitialized object. More...
|
|
| CAtomicHashTable (char *buf, size_t sz) |
| Attach to an existing hash table hosted in a memory buffer. More...
|
|
| CAtomicHashTable (char *buf, size_t sz, size_t capacity, int row, size_t valueLen, bool create=false) |
| Attach to or create a hash table in a memory buffer. More...
|
|
bool | init (char *buf, size_t sz) |
| Attach to an existing hash table hosted in a memory buffer. More...
|
|
bool | init (char *buf, size_t sz, size_t capacity, int row, size_t valueLen, bool create=false) |
| Attach to or create a hash table in a memory buffer. More...
|
|
void | uninit () |
| Reset current object. More...
|
|
bool | valid () const |
| Test if current object is initialized. More...
|
|
|
int | rowSize () const |
| Get number of rows in hash table. More...
|
|
size_t | capacityOfRow (int row) const |
| Get capacity of a row in hash table. More...
|
|
size_t | capacity () const |
| Get capacity of the hash table. More...
|
|
size_t | sizeOfRow (int row) const |
| Get number of key-value pairs in a row. More...
|
|
size_t | size () const |
| Get number of key-value pair in the hash table. More...
|
|
bool | empty () const |
| Test if the hash table is empty. More...
|
|
|
time_t | createTime () const |
| Get creation time of the hash table. More...
|
|
time_t | updateTime () const |
| Get latest updating time of the hash table. More...
|
|
time_t | upgradeTime () const |
| Get latest upgrading time of the hash table. More...
|
|
std::string | toString () const |
| Get a description of the hash table. More...
|
|
|
bool | insert (const key_type &key, const char *value, size_t len) |
| Insert a key-value pair into the hash table. More...
|
|
bool | insert (const key_type &key, const std::string &value) |
| Insert a key-value pair into the hash table. More...
|
|
bool | get (const key_type &key, char *value, size_t &len) const |
| Obtain value of a key. More...
|
|
bool | get (const key_type &key, std::string &value) const |
| Obtain value of a key. More...
|
|
bool | has (const key_type &key) const |
| Test if a key exists in the hash table. More...
|
|
bool | update (const key_type &key, const char *value, size_t len) |
| Update value of a key. More...
|
|
bool | update (const key_type &key, const std::string &value) |
| Update value of a key. More...
|
|
size_t | remove (const key_type &key) |
| Remove a key and its value. More...
|
|
void | clear () |
| Clear all data in the hash table. More...
|
|
template<typename Key, template< typename >class HashKey = CHashFn, template< typename >class EqualKey = std::equal_to>
class CAtomicHashTable< Key, HashKey, EqualKey >
A lock-free hash table that can be used in multi-thread or multi-process programs.
A common usage of CAtomicHashTable is for multiple threads or processes to operate on the same hash table efficiently.
It must reside in continuous memory pre-allocated by the user. Hence, key and value types must be trivially copyable, i.e. can be copied using std::memcpy
.
- Multi-Thread/Process Safety Guide
| Same Key | Different Keys |
Read | Safe | Safe |
Write | Unsafe | Safe |
- Multiple Rows Hash Table
- CAtomicHashTable resolves key hash collisions by adding multiple rows. A row is a one-dimension hash table.
If a key collides with another key in a row, CAtomicHashTable will search in the next row, and so on until it finds a room.
The more number of rows, the less chance of failure when hash table capacity is about to reach. The less number of rows, the better performance it can get.
- Template Parameters
-
Key | Type of keys, must be POD or C struct compatible types |
HashKey | Hash function of Key, should implement: size_t operator ()(const Key & key) const; |
EqualKey | Equal predictor of Key, should implement: bool operator ()(const Key & key1, const Key & key2) const; |
- Value
- Value type is
std::string
or char
array.
- Note
- Size limitation of Key and Value is defined by kValueLenMax.
template<typename Key , template< typename >class HashKey = CHashFn, template< typename >class EqualKey = std::equal_to>
Attach to an existing hash table hosted in a memory buffer.
There must be an initialized hash table in the memory buffer. This function tries to attach to the existing hash table to a local object. If failed, it won't modify the content of the memory buffer.
- Parameters
-
buf | Pointer to the memory buffer |
sz | Byte size of the memory buffer |
- Returns
true
if succeeded; otherwise false
template<typename Key , template< typename >class HashKey = CHashFn, template< typename >class EqualKey = std::equal_to>
bool CAtomicHashTable< Key, HashKey, EqualKey >::insert |
( |
const key_type & |
key, |
|
|
const char * |
value, |
|
|
size_t |
len |
|
) |
| |
|
inline |
Insert a key-value pair into the hash table.
If key
already exists, this function may result in multiple instances of key
in the hash table.
- Parameters
-
key | Key to insert into the hash table |
value | Pointer to bytes of value to insert into the hash table |
len | Byte size of value |
- Returns
true
if succeeded; otherwise false
template<typename Key , template< typename >class HashKey = CHashFn, template< typename >class EqualKey = std::equal_to>
bool CAtomicHashTable< Key, HashKey, EqualKey >::insert |
( |
const key_type & |
key, |
|
|
const std::string & |
value |
|
) |
| |
|
inline |
Insert a key-value pair into the hash table.
If key
already exists, this function may result in multiple instances of key
in the hash table.
- Parameters
-
key | Key to insert into the hash table |
value | Bytes of value to insert into the hash table |
- Returns
true
if succeeded; otherwise false
template<typename Key , template< typename >class HashKey = CHashFn, template< typename >class EqualKey = std::equal_to>
size_t CAtomicHashTable< Key, HashKey, EqualKey >::remove |
( |
const key_type & |
key | ) |
|
|
inline |
Remove a key and its value.
If there are multiple instances of key
, only one will be removed.
- Parameters
-
key | Key to remove from this hash table |
- Returns
- Number of data blocks released; Or 0 if
key
doesn't exist