We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Using ConcurrentHashMap to Query and Maintain Key-Value Information in a Multithreaded Environment

ConcurrentHashMap provided by FAST Kit is specifically designed for key-value data management in high-concurrency scenarios. It is primarily designed for secure storage, fast access, and efficient updates in a multithreaded environment. It is suitable for create, read, update, and delete (CRUD) operations that require high concurrent throughput and data consistency. Typical scenarios include single-point insertion, deletion, query, and concurrent modifications.

API Description

For details about the APIs, please refer to the API Reference.

Expand
API Description
FAST_ErrorCode HMS_FAST_ConcurrentHashmap_Create (FAST_ConcurrentHashmapHandle* handle, HMS_FAST_ConcurrentHashmap_HashFunc hasher, HMS_FAST_ConcurrentHashmap_KeyEqualFunc equaler, float maxLoadFac, size_t numShards) Creates a ConcurrentHashMap with the given configuration.
void HMS_FAST_ConcurrentHashmap_Destroy (FAST_ConcurrentHashmapHandle* handle) Destroys a specified ConcurrentHashMap.
FAST_ErrorCode HMS_FAST_ConcurrentHashmap_Insert (FAST_ConcurrentHashmapHandle* handle, const FAST_ConcurrentHashmapKeyPtr key, const FAST_ConcurrentHashmapValuePtr value, FAST_ConcurrentHashmapValuePtr* originValue) Inserts a given key-value pair into a ConcurrentHashMap. If the key already exists, the existing value is overwritten with value, and the address of the original value is stored in originValue.
FAST_ErrorCode HMS_FAST_ConcurrentHashmap_Find (FAST_ConcurrentHashmapHandle* handle, const FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapValuePtr* value) Searches for the input key in a given ConcurrentHashMap and saves the corresponding value into value.
FAST_ErrorCode HMS_FAST_ConcurrentHashmap_Erase (FAST_ConcurrentHashmapHandle* handle, const FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapKeyPtr* originKey, FAST_ConcurrentHashmapValuePtr* originValue) Deletes the input key from the given HashMap and saves the addresses of the corresponding key and value into originKey and originValue, respectively.
FAST_ErrorCode HMS_FAST_ConcurrentHashmap_TryInsert (FAST_ConcurrentHashmapHandle* handle, const FAST_ConcurrentHashmapKeyPtr key, const FAST_ConcurrentHashmapValuePtr value) Inserts a given key-value pair into a ConcurrentHashMap. If the key already exists, no operation is performed.
size_t HMS_FAST_ConcurrentHashmap_Size (FAST_ConcurrentHashmapHandle* handle) Returns the number of elements in a given HashMap.
void HMS_FAST_ConcurrentHashmap_Clear (FAST_ConcurrentHashmapHandle* handle) Clears all elements maintained in a given HashMap.
size_t HMS_FAST_ConcurrentHashmap_EraseIf (FAST_ConcurrentHashmapHandle* handle, HMS_FAST_ConcurrentHashmap_HookFunc condFunc, void* condCtx, HMS_FAST_ConcurrentHashmap_HookFunc freeFunc, void* freeCtx) Deletes all elements that meet the developer-defined conditions from the HashMap and releases the memory in the developer-defined manner.
void HMS_FAST_ConcurrentHashmap_Traverse (FAST_ConcurrentHashmapHandle* handle, HMS_FAST_ConcurrentHashmap_HookFunc condFunc, void* condCtx, HMS_FAST_ConcurrentHashmap_HookFunc workFunc, void* workCtx) Traverses the HashMap and modifies all key-value pairs that meet the developer-defined conditions in the developer-defined manner.

Development Procedure

  1. Link the related dynamic library in the CMake script.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. find_library(
    2. lib_fast_ads
    3. NAMES fast_ads
    4. )
    5. target_link_libraries(entry PRIVATE ${lib_fast_ads})
  2. Call related APIs to manage key-value information.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. #include "FASTKit/fast_ads_concurrent_hashmap.h"
    2. // Define how to calculate hash values and compare keys.
    3. uint64_t custom_hash_int(const FAST_ConcurrentHashmapKeyPtr key) {
    4. static std::hash<int> hasher;
    5. int* intKey = (int*)key;
    6. return hasher(*intKey);
    7. }
    8. int32_t custom_equal_int(const FAST_ConcurrentHashmapKeyPtr key1, const FAST_ConcurrentHashmapKeyPtr key2) {
    9. int* intKey1 = (int*)key1;
    10. int* intKey2 = (int*)key2;
    11. return (*intKey1) == (*intKey2);
    12. }
    13. // Customize the deletion condition.
    14. int32_t custom_erase_cond(FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapValuePtr val, void* context) {
    15. return 1;
    16. }
    17. // Free the memory held by the key and value pointers.
    18. int32_t custom_free(FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapValuePtr val, void* context) {
    19. int* intKey = (int*)key;
    20. int* intVal = (int*)val;
    21. delete intKey;
    22. delete intVal;
    23. return 0;
    24. }
    25. // Customize the modification condition. You can also pass nullptr to modify all elements.
    26. int32_t custom_modify_cond(FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapValuePtr val, void* context) {
    27. return 1;
    28. }
    29. int32_t custom_work(FAST_ConcurrentHashmapKeyPtr key, FAST_ConcurrentHashmapValuePtr val, void* context) {
    30. int* intVal = (int*)val;
    31. int* intCtx = (int*)context;
    32. *intVal += (*intCtx);
    33. return 1;
    34. }
    35. static napi_value RunConcurrentHashmap(napi_env env, napi_callback_info info)
    36. {
    37. // Create a ConcurrentHashMap with proper configurations. The typical values of the load factor (loadfac) and the number of segments (numShards) are 0.8 and 64, respectively.
    38. // The load factor mainly affects operations within each segment. A larger load factor usually results in lower memory consumption but higher operational overhead.
    39. // The number of segments mainly affects the ConcurrentHashMap as a whole. A larger number of segments generally means better concurrency performance but greater memory consumption.
    40. FAST_ConcurrentHashmapHandle handle;
    41. HMS_FAST_ConcurrentHashmap_HashFunc hasher = &custom_hash_int;
    42. HMS_FAST_ConcurrentHashmap_KeyEqualFunc equaler = &custom_equal_int;
    43. float loadfac = 0.8;
    44. size_t numShards = 64;
    45. int ret = HMS_FAST_ConcurrentHashmap_Create(&handle, hasher, equaler, loadfac, numShards);
    46. // Initialize an empty HashMap and insert elements into it.
    47. const int size = 10;
    48. int keys[size] = {1,2,3,4,5,6,7,8,9,10};
    49. int vals[size] = {1,2,3,4,5,6,7,8,9,10};
    50. for (int i = 0; i < size; ++i) {
    51. ret = HMS_FAST_ConcurrentHashmap_Insert(
    52. handle,
    53. (FAST_ConcurrentHashmapKeyPtr)&(keys[i]),
    54. (FAST_ConcurrentHashmapValuePtr)&(vals[i]),
    55. nullptr
    56. );
    57. } // After the insertion, the HashMap should contain {1: 1, 2: 2, ..., 10: 10}.
    58. // Use insert to overwrite the existing key. If tryInsert is used, the existing key will not be overwritten.
    59. int key2 = 1;
    60. int val2 = 2;
    61. int* originVal0;
    62. ret = HMS_FAST_ConcurrentHashmap_Insert(
    63. handle,
    64. (FAST_ConcurrentHashmapKeyPtr)&key2,
    65. (FAST_ConcurrentHashmapValuePtr)&val2,
    66. (FAST_ConcurrentHashmapValuePtr*)&originVal0
    67. ); // {1: 2, ...}, and originVal0 == &vals[0]
    68. // Use the key to search for the corresponding value and save the result in the input pointer. When using this API, you need to verify the return value ret.
    69. // If the value of ret is not FAST_ERROR_CODE_SUCCESS, the value obtained by res is invalid.
    70. int targetKey = 1;
    71. int* res;
    72. ret = HMS_FAST_ConcurrentHashmap_Find(
    73. handle,
    74. (FAST_ConcurrentHashmapKeyPtr)&targetKey,
    75. (FAST_ConcurrentHashmapValuePtr*)&res
    76. ); // (*res) == 2
    77. // Remove the key-value pair associated with the key from the ConcurrentHashMap and retrieve the associated memory addresses.
    78. // You can use originKey/Val to obtain the address of the pre-inserted element for memory management. You can also use nullptr as the input parameter.
    79. int* originKey1;
    80. int* originVal1;
    81. int deleteKey = 1;
    82. ret = HMS_FAST_ConcurrentHashmap_Erase(
    83. handle,
    84. (FAST_ConcurrentHashmapKeyPtr)&deleteKey,
    85. (FAST_ConcurrentHashmapKeyPtr*)&originKey1,
    86. (FAST_ConcurrentHashmapValuePtr*)&originVal1
    87. ); // originKey1 == &keys[0] && originVal1 == &val2
    88. // Query the number of elements in the ConcurrentHashMap.
    89. size_t curSize = HMS_FAST_ConcurrentHashmap_Size(handle); // curSize == 9
    90. // Clear all elements in the ConcurrentHashMap.
    91. HMS_FAST_ConcurrentHashmap_Clear(handle);
    92. curSize = HMS_FAST_ConcurrentHashmap_Size(handle); // curSize == 0
    93. for (int i = 0; i < 6; i++) {
    94. int* key = new int{i};
    95. int* val = new int{i};
    96. ret = HMS_FAST_ConcurrentHashmap_Insert(
    97. handle,
    98. (FAST_ConcurrentHashmapKeyPtr)key,
    99. (FAST_ConcurrentHashmapValuePtr)val,
    100. nullptr
    101. );
    102. } // {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
    103. // Use the Traverse API to perform the custom_work operation on the elements that meet the custom_modify_cond condition.
    104. int context = 10;
    105. HMS_FAST_ConcurrentHashmap_Traverse(
    106. handle,
    107. (HMS_FAST_ConcurrentHashmap_HookFunc)&custom_modify_cond,
    108. nullptr,
    109. (HMS_FAST_ConcurrentHashmap_HookFunc)&custom_work,
    110. (void*)&context
    111. ); // {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15}
    112. // Use the EraseIf API to delete all key-value pairs that meet the custom_erase_cond condition and use custom_free to clear the corresponding memory.
    113. ret = HMS_FAST_ConcurrentHashmap_EraseIf(
    114. handle,
    115. (HMS_FAST_ConcurrentHashmap_HookFunc)&custom_erase_cond,
    116. nullptr,
    117. (HMS_FAST_ConcurrentHashmap_HookFunc)&custom_free,
    118. nullptr
    119. ); // size == 0 && ret == 6
    120. // Destroy the ConcurrentHashMap.
    121. HMS_FAST_ConcurrentHashmap_Destroy(handle);
    122. return 0;
    123. }
Search in Guides
Enter a keyword.