-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Speed up views with ICU sort keys #6050
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less_json_ids/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less_json/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key/1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_icu_version/0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_uca_version/0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_collator_version/0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,7 +26,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -export([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less_nif/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less_erl/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compare_strings_nif/2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compare_strings_nif/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_sort_key_nif/1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -on_load(init/0). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -58,6 +60,35 @@ less_json_ids({JsonA, IdA}, {JsonB, IdB}) -> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less_json(A, B) -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| less(A, B) < 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % Encode ejson to terms with native `less` ordering matching less/2 ICU | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % collation order. The leading integer is the ejson object rank see [1]. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % collection order is: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % null < false < true < num < str < array < object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % Sort keys should not be stored or compared against sort key generated by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % other major libicu version. The intent so to use these only at runtime, in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % memory on the same node (on the coordinator mostly likely when merge-sorting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % incoming view rows). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % [1] https://docs.couchdb.org/en/stable/ddocs/views/collation.html#collation-specification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(null) -> {0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. neat usage of tuple to ensure the type hierarchy.
Contributor
Author
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. I borrowed the idea from the FDB sort key implementation couchdb/src/couch_views/src/couch_views_encoding.erl Lines 44 to 77 in fa5ef27
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(false) -> {1}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(true) -> {2}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(N) when is_number(N) -> {3, N}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(B) when is_binary(B) -> {4, get_sort_key_nif(B)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key(L) when is_list(L) -> {5, [sort_key(E) || E <- L]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key({P}) when is_list(P) -> {6, [{sort_key(K), sort_key(V)} || {K, V} <- P]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % {<<255,255,255,255>>} sentinel (?MAX_JSON_OBJ max key) sorts above all json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % so give it the highest value. This may be a belt-and-suspenders just in case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % In practice we should never emit that object but might see it as part of a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % query. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_key({<<255, 255, 255, 255>>}) -> {7, []}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_sort_key_nif(_A) -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| erlang:nif_error(get_sort_key_nif_load_error). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_icu_version() -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| erlang:nif_error(get_icu_version). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should impose an upper bound in this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Trying to pick a value, and remembered we've had to bump memory limits on couch_js executables due to some large rows / ddocs. The highest I've seen in production was 128Mb of total heap size so maybe we can make that the limit.