* Hashes a bson value for use in hash set. * * If no valid collation string is provided or collation is disabled, we simply * hash the bson value. * * If a collation string is provided, we need to consider the following: * (1) non-collation-aware bson value, just hash the value. * (2) utf8 bson value, create collation sort key and hash that. * (3) arrays and documents, recurse into them, a
| 317 | * The final hash value is the sum of all the hash values. |
| 318 | */ |
| 319 | static void |
| 320 | BsonValueHashFuncCore(const bson_value_t *bsonValue, const |
| 321 | char *collationString, uint32_t *hashValue) |
| 322 | { |
| 323 | /* collation disabled or invalid collation string provided, */ |
| 324 | /* simply hash the bson value and return */ |
| 325 | if (!IsCollationApplicable(collationString)) |
| 326 | { |
| 327 | *hashValue += BsonValueHashUint32(bsonValue); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | /* base cases */ |
| 332 | /* (1) non-collation-aware bson value, just hash value */ |
| 333 | if (!IsBsonTypeCollationAware(bsonValue->value_type) || |
| 334 | !IsCollationApplicable(collationString)) |
| 335 | { |
| 336 | *hashValue += BsonValueHashUint32(bsonValue); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | /* (2) utf8 bson value, create collation sort key and hash that */ |
| 341 | if (bsonValue->value_type == BSON_TYPE_UTF8) |
| 342 | { |
| 343 | char *key = bsonValue->value.v_utf8.str; |
| 344 | char *sortKey = GetCollationSortKey(collationString, key, |
| 345 | bsonValue->value.v_utf8.len); |
| 346 | |
| 347 | *hashValue += hash_bytes((unsigned char *) sortKey, strlen(sortKey)); |
| 348 | pfree(sortKey); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | /* recursive case: arrays and documents */ |
| 353 | /* process each element and recurse into array and document values */ |
| 354 | bson_iter_t arrayValueIterator; |
| 355 | bson_iter_init_from_data(&arrayValueIterator, |
| 356 | bsonValue->value.v_doc.data, |
| 357 | bsonValue->value.v_doc.data_len); |
| 358 | |
| 359 | while (bson_iter_next(&arrayValueIterator)) |
| 360 | { |
| 361 | const bson_value_t *value = bson_iter_value(&arrayValueIterator); |
| 362 | BsonValueHashFuncCore(value, collationString, hashValue); |
| 363 | } |
| 364 | } |
no test coverage detected