iilyak commented on a change in pull request #471: Use efficient set storage 
for field names
URL: https://github.com/apache/couchdb/pull/471#discussion_r111637686
 
 

 ##########
 File path: src/mango/src/mango_native_proc.erl
 ##########
 @@ -257,15 +257,10 @@ add_default_text_field([_ | Rest], Acc) ->
 
 
 %% index of all field names
-get_field_names([], FAcc) ->
-    FAcc;
-get_field_names([{Name, _Type, _Value} | Rest], FAcc) ->
-    case lists:member([<<"$fieldnames">>, Name, []], FAcc) of
-        true ->
-            get_field_names(Rest, FAcc);
-        false ->
-            get_field_names(Rest, [[<<"$fieldnames">>, Name, []] | FAcc])
-    end.
+get_field_names(Fields) ->
+    GBFieldSet = gb_sets:from_list(Fields),
 
 Review comment:
   I'll try to explain the problem by example.
   ```
   14> Fields = [{foo, type, v1}, {foo, type, v2}].
   [{foo,type,v1},{foo,type,v2}]
   15> GBFieldSet = gb_sets:from_list(Fields).
   {2,{{foo,type,v2},{{foo,type,v1},nil,nil},nil}}
   16> UFields = gb_sets:to_list(GBFieldSet).
   [{foo,type,v1},{foo,type,v2}]
   17> [[<<"$fieldnames">>, Name, []] || {Name, _Type, _Value} <- UFields].
   [[<<"$fieldnames">>,foo,[]],[<<"$fieldnames">>,foo,[]]]
   ```
   The same field is present multiple times ^^^.
   ```
   18> Fields = [{foo, type, v1}, {foo, type, v2}].
   [{foo,type,v1},{foo,type,v2}]
   19> Set = lists:foldl(
   19>     fun({Name, _, _}, Set) ->
   19>          gb_sets:add([<<"$fieldnames">>, Name, []], Set)
   19>     end, gb_sets:new(), Fields),
   19> gb_sets:to_list(Set).
   [[<<"$fieldnames">>,foo,[]]]
   ```
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to