* BsonValueHashUint32 generates a uint32 hash value for a given BSON value. */
| 287 | * BsonValueHashUint32 generates a uint32 hash value for a given BSON value. |
| 288 | */ |
| 289 | uint32 |
| 290 | BsonValueHashUint32(const bson_value_t *bsonValue) |
| 291 | { |
| 292 | switch (bsonValue->value_type) |
| 293 | { |
| 294 | case BSON_TYPE_BOOL: |
| 295 | { |
| 296 | return hash_bytes((const unsigned char *) |
| 297 | &(bsonValue->value.v_bool), |
| 298 | sizeof(bool)); |
| 299 | } |
| 300 | |
| 301 | case BSON_TYPE_INT32: |
| 302 | case BSON_TYPE_INT64: |
| 303 | { |
| 304 | int64 value = BsonValueAsInt64(bsonValue); |
| 305 | return hash_bytes((const unsigned char *) &value, |
| 306 | sizeof(int64)); |
| 307 | } |
| 308 | |
| 309 | case BSON_TYPE_DOUBLE: |
| 310 | { |
| 311 | /* NaN and -NaN should create same hash value */ |
| 312 | if (isnan(bsonValue->value.v_double)) |
| 313 | { |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | /* If the value can be converted to int64, then convert it to int64 and generate a hash, |
| 318 | * which ensures that the hash value of both 1.00 and 1 remains the same. */ |
| 319 | bool checkFixedInteger = true; |
| 320 | if (IsBsonValue64BitInteger(bsonValue, checkFixedInteger)) |
| 321 | { |
| 322 | int64 value = BsonValueAsInt64(bsonValue); |
| 323 | return hash_bytes((const unsigned char *) &value, |
| 324 | sizeof(int64)); |
| 325 | } |
| 326 | |
| 327 | /* In set operators aggregation, non-fixed double and Decimal128 values with the same numerical value are not considered equal. |
| 328 | * For example, if we have a double value of "1.1" and a Decimal128 value of "1.1", they will not be considered equal. |
| 329 | * To ensure that these values are not treated as equal, different hashes are generated for these values.*/ |
| 330 | return hash_bytes((const unsigned char *) |
| 331 | &bsonValue->value.v_double, |
| 332 | sizeof(double)); |
| 333 | } |
| 334 | |
| 335 | case BSON_TYPE_DECIMAL128: |
| 336 | { |
| 337 | /* NaN and -NaN should create same hash value */ |
| 338 | if (IsDecimal128NaN(bsonValue)) |
| 339 | { |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* If the value can be converted to int64, then convert it to int64 and generate a hash, |
| 344 | * which ensures that the hash value of both 1.00 and 1 remains the same. */ |
| 345 | bool checkFixedInteger = true; |
| 346 | if (IsBsonValue64BitInteger(bsonValue, checkFixedInteger)) |
no test coverage detected