<commit_before>% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_mrview).

-export([validate/2]).
-export([query_all_docs/2, query_all_docs/4]).
-export([query_view/3, query_view/4, query_view/6]).
-export([view_changes_since/5]).
-export([view_changes_since/6, view_changes_since/7]).
-export([count_view_changes_since/4, count_view_changes_since/5]).
-export([get_info/2]).
-export([trigger_update/2, trigger_update/3]).
-export([get_view_info/3]).
-export([refresh/2]).
-export([compact/2, compact/3, cancel_compaction/2]).
-export([cleanup/1]).

-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_mrview/include/couch_mrview.hrl").

-record(mracc, {
    db,
    meta_sent=false,
    total_rows,
    offset,
    limit,
    skip,
    group_level,
    doc_info,
    callback,
    user_acc,
    last_go=ok,
    reduce_fun,
    update_seq,
    args
}).


validate(DbName, DDoc) ->
    {Fields} = DDoc#doc.body,
    case couch_util:get_value(<<"options">>, Fields, {[]}) of
        {_} -> ok;
        _ -> throw({invalid_design_doc, <<"`options` parameter must be an object.">>})
    end,
    case couch_util:get_value(<<"views">>, Fields, {[]}) of
        {_} -> ok;
        _ -> throw({invalid_design_doc, <<"`views` parameter must be an object.">>})
    end,
    GetName = fun
        (#mrview{map_names = [Name | _]}) -> Name;
        (#mrview{reduce_funs = [{Name, _} | _]}) -> Name;
        (_) -> null
    end,
    ValidateView = fun(Proc, #mrview{def=MapSrc, reduce_funs=Reds}=View) ->
        couch_query_servers:try_compile(Proc, map, GetName(View), MapSrc),
        lists:foreach(fun
            ({_RedName, <<"_sum", _/binary>>}) ->
                ok;
            ({_RedName, <<"_count", _/binary>>}) ->
                ok;
            ({_RedName, <<"_stats", _/binary>>}) ->
                ok;
            ({_RedName, <<"_", _/binary>> = Bad}) ->
                Msg = ["`", Bad, "` is not a supported reduce function."],
                throw({invalid_design_doc, Msg});
            ({RedName, RedSrc}) ->
                couch_query_servers:try_compile(Proc, reduce, RedName, RedSrc)
        end, Reds)
    end,
    {ok, #mrst{language=Lang, views=Views}}
            = couch_mrview_util:ddoc_to_mrst(DbName, DDoc),
    try Views =/= [] andalso couch_query_servers:get_os_process(Lang) of
        false ->
            ok;
        Proc ->
            try
                lists:foreach(fun(V) -> ValidateView(Proc, V) end, Views)
            catch Error ->
                Error
            after
                couch_query_servers:ret_os_process(Proc)
            end
    catch {unknown_query_language, _Lang} ->
        %% Allow users to save ddocs written in uknown languages
        ok
    end.


query_all_docs(Db, Args) ->
    query_all_docs(Db, Args, fun default_cb/2, []).


query_all_docs(Db, Args, Callback, Acc) when is_list(Args) ->
    query_all_docs(Db, to_mrargs(Args), Callback, Acc);
query_all_docs(Db, Args0, Callback, Acc) ->
    Sig = couch_util:with_db(Db, fun(WDb) ->
        {ok, Info} = couch_db:get_db_info(WDb),
        couch_index_util:hexsig(couch_util:md5(term_to_binary(Info)))
    end),
    Args1 = Args0#mrargs{view_type=map},<commit_msg>Always throw validation error<commit_after>% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_mrview).

-export([validate/2]).
-export([query_all_docs/2, query_all_docs/4]).
-export([query_view/3, query_view/4, query_view/6]).
-export([view_changes_since/5]).
-export([view_changes_since/6, view_changes_since/7]).
-export([count_view_changes_since/4, count_view_changes_since/5]).
-export([get_info/2]).
-export([trigger_update/2, trigger_update/3]).
-export([get_view_info/3]).
-export([refresh/2]).
-export([compact/2, compact/3, cancel_compaction/2]).
-export([cleanup/1]).

-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_mrview/include/couch_mrview.hrl").

-record(mracc, {
    db,
    meta_sent=false,
    total_rows,
    offset,
    limit,
    skip,
    group_level,
    doc_info,
    callback,
    user_acc,
    last_go=ok,
    reduce_fun,
    update_seq,
    args
}).


validate(DbName, DDoc) ->
    {Fields} = DDoc#doc.body,
    case couch_util:get_value(<<"options">>, Fields, {[]}) of
        {_} -> ok;
        _ -> throw({invalid_design_doc, <<"`options` parameter must be an object.">>})
    end,
    case couch_util:get_value(<<"views">>, Fields, {[]}) of
        {_} -> ok;
        _ -> throw({invalid_design_doc, <<"`views` parameter must be an object.">>})
    end,
    GetName = fun
        (#mrview{map_names = [Name | _]}) -> Name;
        (#mrview{reduce_funs = [{Name, _} | _]}) -> Name;
        (_) -> null
    end,
    ValidateView = fun(Proc, #mrview{def=MapSrc, reduce_funs=Reds}=View) ->
        couch_query_servers:try_compile(Proc, map, GetName(View), MapSrc),
        lists:foreach(fun
            ({_RedName, <<"_sum", _/binary>>}) ->
                ok;
            ({_RedName, <<"_count", _/binary>>}) ->
                ok;
            ({_RedName, <<"_stats", _/binary>>}) ->
                ok;
            ({_RedName, <<"_", _/binary>> = Bad}) ->
                Msg = ["`", Bad, "` is not a supported reduce function."],
                throw({invalid_design_doc, Msg});
            ({RedName, RedSrc}) ->
                couch_query_servers:try_compile(Proc, reduce, RedName, RedSrc)
        end, Reds)
    end,
    {ok, #mrst{language=Lang, views=Views}}
            = couch_mrview_util:ddoc_to_mrst(DbName, DDoc),
    try Views =/= [] andalso couch_query_servers:get_os_process(Lang) of
        false ->
            ok;
        Proc ->
            try
                lists:foreach(fun(V) -> ValidateView(Proc, V) end, Views)
            after
                couch_query_servers:ret_os_process(Proc)
            end
    catch {unknown_query_language, _Lang} ->
        %% Allow users to save ddocs written in uknown languages
        ok
    end.


query_all_docs(Db, Args) ->
    query_all_docs(Db, Args, fun default_cb/2, []).


query_all_docs(Db, Args, Callback, Acc) when is_list(Args) ->
    query_all_docs(Db, to_mrargs(Args), Callback, Acc);
query_all_docs(Db, Args0, Callback, Acc) ->
    Sig = couch_util:with_db(Db, fun(WDb) ->
        {ok, Info} = couch_db:get_db_info(WDb),
        couch_index_util:hexsig(couch_util:md5(term_to_binary(Info)))
    end),
    Args1 = Args0#mrargs{view_type=map},
{'fraction_non_alphanumeric': 0.14752542372881355, 'fraction_numerical': 0.007864406779661017, 'mean_word_length': 2.8118863049095606, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4399834', 'n_tokens_mistral': 2500, 'n_tokens_neox': 2475, 'n_words': 537}