core/bus_center/lnn/net_ledger/common/src/lnn_map.c
/**
 * Add map element
 *
 * @param : map Map see details in type Map
 *          key Map key
 *          value Map value
 *          valueSize Map value size
 * @return : SOFTBUS_OK or other error
 */
Add map element
@param : map Map see details in type Map
key Map key
value Map value
valueSize Map value size
@return : SOFTBUS_OK or other error
Add map element @param : map Map see details in type Map key Map key value Map value valueSize Map value size @return : SOFTBUS_OK or other error
int32_t LnnMapSet(Map *map, const char *key, const void *value, uint32_t valueSize)
{
    MapNode *node = NULL;
    if (map == NULL || key == NULL || value == NULL || valueSize == 0) {
        return SOFTBUS_INVALID_PARAM;
    }
    if (valueSize > HDF_MAP_KEY_MAX_SIZE || strlen(key) > HDF_MAP_VALUE_MAX_SIZE) {
        return SOFTBUS_INVALID_PARAM;
    }
    uint32_t hash = MapHash(key);
    if (map->nodeSize > 0 && map->nodes != NULL) {
        uint32_t idx = MapHashIdx(map, hash);
        node = map->nodes[idx];
        while (node != NULL) {
            if (node->hash != hash || node->key == NULL || strcmp(node->key, key) != 0) {
                node = node->next;
                continue;
            }
            if (node->value == NULL || node->valueSize != valueSize) {
                return SOFTBUS_INVALID_PARAM;
            }
            if (memcpy_s(node->value, node->valueSize, value, valueSize) != EOK) {
                return SOFTBUS_ERR;
            }
            return SOFTBUS_OK;
        }
    }
    if (map->nodeSize >= map->bucketSize) {
        uint32_t size = (map->bucketSize < HDF_MIN_MAP_SIZE) ? HDF_MIN_MAP_SIZE : \
            (map->bucketSize << HDF_ENLARGE_FACTOR);
        MapResize(map, size);
    }
    node = MapCreateNode(key, hash, value, valueSize);
    if (node == NULL) {
        return SOFTBUS_INVALID_PARAM;
    }
    MapAddNode(map, node);
    map->nodeSize++;
    return SOFTBUS_OK;
}
int32_t LnnMapSet ( Map * map , const char * key , const void * value , uint32_t valueSize ) { MapNode * node = NULL ; if ( map == NULL || key == NULL || value == NULL || valueSize == 0 ) { return SOFTBUS_INVALID_PARAM ; } if ( valueSize > HDF_MAP_KEY_MAX_SIZE || strlen ( key ) > HDF_MAP_VALUE_MAX_SIZE ) { return SOFTBUS_INVALID_PARAM ; } uint32_t hash = MapHash ( key ) ; if ( map -> nodeSize > 0 && map -> nodes != NULL ) { uint32_t idx = MapHashIdx ( map , hash ) ; node = map -> nodes [ idx ] ; while ( node != NULL ) { if ( node -> hash != hash || node -> key == NULL || strcmp ( node -> key , key ) != 0 ) { node = node -> next ; continue ; } if ( node -> value == NULL || node -> valueSize != valueSize ) { return SOFTBUS_INVALID_PARAM ; } if ( memcpy_s ( node -> value , node -> valueSize , value , valueSize ) != EOK ) { return SOFTBUS_ERR ; } return SOFTBUS_OK ; } } if ( map -> nodeSize >= map -> bucketSize ) { uint32_t size = ( map -> bucketSize < HDF_MIN_MAP_SIZE ) ? HDF_MIN_MAP_SIZE : ( map -> bucketSize << HDF_ENLARGE_FACTOR ) ; MapResize ( map , size ) ; } node = MapCreateNode ( key , hash , value , valueSize ) ; if ( node == NULL ) { return SOFTBUS_INVALID_PARAM ; } MapAddNode ( map , node ) ; map -> nodeSize ++ ; return SOFTBUS_OK ; }
Add map element
 @param : map Map see details in type Map
          key Map key
          value Map value
          valueSize Map value size
 @return : SOFTBUS_OK or other error
Add map element @param : map Map see details in type Map key Map key value Map value valueSize Map value size @return : SOFTBUS_OK or other error
// size unmatch // update k-v node // for decreasing map search conflict, enlarge bucket Size