<commit_before>:- module(mongo_insert,
    [
        insert/2,
        insert_batch/3
    ]).

/** <module> xxxxxxxxx
 */

:- include(misc(common)).

:- use_module(bson(bson), []).
:- use_module(misc(util), []).
:- use_module(mongo(mongo_bytes), []).
:- use_module(mongo(mongo_collection), []).
:- use_module(mongo(mongo_connection), []).
:- use_module(mongo(mongo_database), []).
:- use_module(mongo(mongo_util), []).

%%  insert(+Collection, +Doc).
%
%   True if Doc is inserted into Collection.

insert(Collection, Doc) :-
    insert_batch(Collection, [], [Doc]).

%%  insert_batch(+Collection, +Options, +Docs).
%
%   True if Docs are inserted into Collection using Options (keep_going).

insert_batch(Collection, Options, Docs) :-
    mongo_collection:collection_namespace(Collection, Namespace),
    options_flags(Options, Flags),
    build_bytes_for_insert_batch(Namespace, Flags, Docs, BytesToSend),
    mongo_collection:collection_connection(Collection, Connection),
    mongo_connection:send_to_server(Connection, BytesToSend).

options_flags([keep_going], 1) :- !.
options_flags([],           0) :- !.

build_bytes_for_insert_batch(Namespace, Flags, Docs, Bytes) :-
    phrase(build_bytes_for_insert_batch(Namespace, Flags, Docs), Bytes),
    mongo_bytes:count_bytes_and_set_length(Bytes).

build_bytes_for_insert_batch(Namespace, Flags, Docs) -->
    mongo_bytes:header(000, 000, 2002), % xxxxx request, response
    mongo_bytes:int32(Flags),
    mongo_bytes:c_string(Namespace),
    mongo_bytes:bson_docs(Docs).<commit_msg>Use general options implementation.<commit_after>:- module(mongo_insert,
    [
        insert/2,
        insert_batch/3
    ]).

/** <module> xxxxxxxxx
 */

:- include(misc(common)).

:- use_module(bson(bson), []).
:- use_module(misc(util), []).
:- use_module(mongo(mongo_bytes), []).
:- use_module(mongo(mongo_collection), []).
:- use_module(mongo(mongo_connection), []).
:- use_module(mongo(mongo_database), []).
:- use_module(mongo(mongo_util), []).

%%  insert(+Collection, +Doc).
%
%   True if Doc is inserted into Collection.

insert(Collection, Doc) :-
    insert_batch(Collection, [], [Doc]).

%%  insert_batch(+Collection, +Options, +Docs).
%
%   True if Docs are inserted into Collection using Options, where
%   possible options are:
%       keep_going

insert_batch(Collection, Options, Docs) :-
    mongo_collection:collection_namespace(Collection, Namespace),
    mongo_util:options_flags(Options, mongo_find:option_value, Flags),
    build_bytes_for_insert_batch(Namespace, Flags, Docs, BytesToSend),
    mongo_collection:collection_connection(Collection, Connection),
    mongo_connection:send_to_server(Connection, BytesToSend).

option_value(keep_going, 1).

build_bytes_for_insert_batch(Namespace, Flags, Docs, Bytes) :-
    phrase(build_bytes_for_insert_batch(Namespace, Flags, Docs), Bytes),
    mongo_bytes:count_bytes_and_set_length(Bytes).

build_bytes_for_insert_batch(Namespace, Flags, Docs) -->
    mongo_bytes:header(000, 000, 2002), % xxxxx request, response
    mongo_bytes:int32(Flags),
    mongo_bytes:c_string(Namespace),
    mongo_bytes:bson_docs(Docs).
{'fraction_non_alphanumeric': 0.1463336535382645, 'fraction_numerical': 0.009926352865834135, 'mean_word_length': 5.198412698412699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '88426', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1054, 'n_words': 165}