-
Notifications
You must be signed in to change notification settings - Fork 99
MONGOCRYPT-762 Generate text search token sets from StrEncode output #946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da19785
MONGOCRYPT-762 Generate text search token sets from StrEncode output
erwee 1450969
assert safe cast of uint32_t length to int
erwee 22a1e32
Fix memleak
erwee 867d146
Fix conversion errors
erwee dacfaa1
Gabe's suggestions
erwee 348ab30
Gabe's feedback 2; Fix empty sets bug and add test
erwee 3f6df0d
revert back to double braces
erwee 1d95bf4
fixes
erwee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -29,6 +29,8 @@ | |||||||
#include "mc-range-edge-generation-private.h" | ||||||||
#include "mc-range-encoding-private.h" | ||||||||
#include "mc-range-mincover-private.h" | ||||||||
#include "mc-str-encode-string-sets-private.h" | ||||||||
#include "mc-text-search-str-encode-private.h" | ||||||||
#include "mc-tokens-private.h" | ||||||||
#include "mongocrypt-buffer-private.h" | ||||||||
#include "mongocrypt-ciphertext-private.h" | ||||||||
|
@@ -1126,26 +1128,22 @@ static bool _fle2_generate_TextSearchTokenSets(_mongocrypt_key_broker_t *kb, | |||||||
mc_FLE2InsertUpdatePayloadV2_t *payload, | ||||||||
const _mongocrypt_buffer_t *indexKeyId, | ||||||||
const mc_FLE2TextSearchInsertSpec_t *spec, | ||||||||
const _mongocrypt_buffer_t *value, | ||||||||
int64_t contentionFactor, | ||||||||
mongocrypt_status_t *status) { | ||||||||
BSON_ASSERT_PARAM(kb); | ||||||||
BSON_ASSERT_PARAM(payload); | ||||||||
BSON_ASSERT_PARAM(indexKeyId); | ||||||||
BSON_ASSERT_PARAM(spec); | ||||||||
BSON_ASSERT_PARAM(value); | ||||||||
|
||||||||
_mongocrypt_crypto_t *crypto = kb->crypt->crypto; | ||||||||
mc_TextSearchTokenSets_t *tsts = &payload->textSearchTokenSets.tsts; | ||||||||
_FLE2EncryptedPayloadCommon_t common = {{0}}; | ||||||||
bool res = false; | ||||||||
|
||||||||
// TODO MONGOCRYPT-759 implement case folding; for now let foldedValue be a copy of value. | ||||||||
_mongocrypt_buffer_t foldedValue = {0}; | ||||||||
_mongocrypt_buffer_init(&foldedValue); | ||||||||
_mongocrypt_buffer_copy_to(value, &foldedValue); | ||||||||
|
||||||||
// TODO MONGOCRYPT-762 do StrEncode here to get substring sets to encode | ||||||||
mc_str_encode_sets_t *encodeSets = mc_text_search_str_encode(spec, status); | ||||||||
if (!encodeSets) { | ||||||||
goto fail; | ||||||||
} | ||||||||
|
||||||||
// Start the token derivations | ||||||||
if (!_get_tokenKey(kb, indexKeyId, &common.tokenKey, status)) { | ||||||||
|
@@ -1164,72 +1162,130 @@ static bool _fle2_generate_TextSearchTokenSets(_mongocrypt_key_broker_t *kb, | |||||||
goto fail; | ||||||||
} | ||||||||
|
||||||||
if (!_fle2_generate_TextExactTokenSet(kb, | ||||||||
&tsts->exact, | ||||||||
&foldedValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
goto fail; | ||||||||
// Generate exact token set singleton | ||||||||
{ | ||||||||
_mongocrypt_buffer_t asBsonValue; | ||||||||
_mongocrypt_buffer_init(&asBsonValue); | ||||||||
BSON_ASSERT(encodeSets->exact.len < INT_MAX); | ||||||||
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, | ||||||||
(const char *)encodeSets->exact.data, | ||||||||
(int)encodeSets->exact.len); | ||||||||
if (!_fle2_generate_TextExactTokenSet(kb, | ||||||||
&tsts->exact, | ||||||||
&asBsonValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
goto fail; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! thanks! |
||||||||
} | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
} | ||||||||
|
||||||||
const char *substring; | ||||||||
uint32_t bytelen; | ||||||||
uint32_t appendCount; | ||||||||
|
||||||||
// Generate array of substring token sets | ||||||||
if (spec->substr.set) { | ||||||||
// TODO MONGOCRYPT-762 iterate on StrEncode substrings set | ||||||||
mc_TextSubstringTokenSet_t substrSet = {{0}}; | ||||||||
mc_TextSubstringTokenSet_init(&substrSet); | ||||||||
|
||||||||
if (!_fle2_generate_TextSubstringTokenSet(kb, | ||||||||
&substrSet, | ||||||||
&foldedValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
mc_TextSubstringTokenSet_cleanup(&substrSet); | ||||||||
goto fail; | ||||||||
mc_substring_set_iter_t set_itr; | ||||||||
mc_substring_set_iter_init(&set_itr, encodeSets->substring_set); | ||||||||
|
||||||||
while (mc_substring_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) { | ||||||||
for (; appendCount > 0; appendCount--) { | ||||||||
erwee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
mc_TextSubstringTokenSet_t tset = {{0}}; | ||||||||
mc_TextSubstringTokenSet_init(&tset); | ||||||||
|
||||||||
_mongocrypt_buffer_t asBsonValue; | ||||||||
_mongocrypt_buffer_init(&asBsonValue); | ||||||||
BSON_ASSERT(bytelen < INT_MAX); | ||||||||
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen); | ||||||||
|
||||||||
if (!_fle2_generate_TextSubstringTokenSet(kb, | ||||||||
&tset, | ||||||||
&asBsonValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
mc_TextSubstringTokenSet_cleanup(&tset); | ||||||||
goto fail; | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->substringArray, tset); // moves ownership of tset | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
} | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->substringArray, substrSet); | ||||||||
} | ||||||||
|
||||||||
// Generate array of suffix token sets | ||||||||
if (spec->suffix.set) { | ||||||||
// TODO MONGOCRYPT-762 iterate on StrEncode suffixes set | ||||||||
mc_TextSuffixTokenSet_t suffixSet = {{0}}; | ||||||||
mc_TextSuffixTokenSet_init(&suffixSet); | ||||||||
|
||||||||
if (!_fle2_generate_TextSuffixTokenSet(kb, | ||||||||
&suffixSet, | ||||||||
&foldedValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
mc_TextSuffixTokenSet_cleanup(&suffixSet); | ||||||||
goto fail; | ||||||||
mc_affix_set_iter_t set_itr; | ||||||||
mc_affix_set_iter_init(&set_itr, encodeSets->suffix_set); | ||||||||
|
||||||||
while (mc_affix_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) { | ||||||||
for (; appendCount > 0; appendCount--) { | ||||||||
mc_TextSuffixTokenSet_t tset = {{0}}; | ||||||||
mc_TextSuffixTokenSet_init(&tset); | ||||||||
|
||||||||
_mongocrypt_buffer_t asBsonValue; | ||||||||
_mongocrypt_buffer_init(&asBsonValue); | ||||||||
BSON_ASSERT(bytelen < INT_MAX); | ||||||||
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen); | ||||||||
|
||||||||
if (!_fle2_generate_TextSuffixTokenSet(kb, | ||||||||
&tset, | ||||||||
&asBsonValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
mc_TextSuffixTokenSet_cleanup(&tset); | ||||||||
goto fail; | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->suffixArray, tset); // moves ownership of tset | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
} | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->suffixArray, suffixSet); | ||||||||
} | ||||||||
|
||||||||
// Generate array of prefix token sets | ||||||||
if (spec->prefix.set) { | ||||||||
// TODO MONGOCRYPT-762 iterate on StrEncode suffixes set | ||||||||
mc_TextPrefixTokenSet_t prefixSet = {{0}}; | ||||||||
mc_TextPrefixTokenSet_init(&prefixSet); | ||||||||
|
||||||||
if (!_fle2_generate_TextPrefixTokenSet(kb, | ||||||||
&prefixSet, | ||||||||
&foldedValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
mc_TextPrefixTokenSet_cleanup(&prefixSet); | ||||||||
goto fail; | ||||||||
mc_affix_set_iter_t set_itr; | ||||||||
mc_affix_set_iter_init(&set_itr, encodeSets->prefix_set); | ||||||||
|
||||||||
while (mc_affix_set_iter_next(&set_itr, &substring, &bytelen, &appendCount)) { | ||||||||
for (; appendCount > 0; appendCount--) { | ||||||||
mc_TextPrefixTokenSet_t tset = {{0}}; | ||||||||
mc_TextPrefixTokenSet_init(&tset); | ||||||||
|
||||||||
_mongocrypt_buffer_t asBsonValue; | ||||||||
_mongocrypt_buffer_init(&asBsonValue); | ||||||||
BSON_ASSERT(bytelen < INT_MAX); | ||||||||
_mongocrypt_buffer_copy_from_string_as_bson_value(&asBsonValue, substring, (int)bytelen); | ||||||||
|
||||||||
if (!_fle2_generate_TextPrefixTokenSet(kb, | ||||||||
&tset, | ||||||||
&asBsonValue, | ||||||||
contentionFactor, | ||||||||
common.collectionsLevel1Token, | ||||||||
common.serverTokenDerivationLevel1Token, | ||||||||
status)) { | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
mc_TextPrefixTokenSet_cleanup(&tset); | ||||||||
goto fail; | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->prefixArray, tset); // moves ownership of tset | ||||||||
_mongocrypt_buffer_cleanup(&asBsonValue); | ||||||||
} | ||||||||
} | ||||||||
_mc_array_append_val(&tsts->prefixArray, prefixSet); | ||||||||
} | ||||||||
payload->textSearchTokenSets.set = true; | ||||||||
res = true; | ||||||||
fail: | ||||||||
_FLE2EncryptedPayloadCommon_cleanup(&common); | ||||||||
_mongocrypt_buffer_cleanup(&foldedValue); | ||||||||
mc_str_encode_sets_destroy(encodeSets); | ||||||||
marksg07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return res; | ||||||||
} | ||||||||
|
||||||||
|
@@ -1350,7 +1406,6 @@ static bool _mongocrypt_fle2_placeholder_to_insert_update_ciphertextForTextSearc | |||||||
&payload, | ||||||||
&placeholder->index_key_id, | ||||||||
&insertSpec, | ||||||||
&value, | ||||||||
payload.contentionFactor, | ||||||||
status)) { | ||||||||
goto fail; | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.