<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Nest;

namespace Demeter.FormComponent
{
    public sealed class DemeterFormStore<TForm> : IFormStore<TForm>
        where TForm : DemeterForm, new()
    {
        private readonly IMongoCollection<TForm> _formCollection;
        private readonly ElasticClient _elasticClient;

        public DemeterFormStore(
            IMongoDatabase database,
            string formsCollection,
            ElasticClient elasticClient = null)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }
            if (formsCollection == null)
            {
                throw new ArgumentNullException(nameof(formsCollection));
            }

            this._formCollection = database.GetCollection<TForm>(formsCollection);
            this._elasticClient = elasticClient;
        }

        async Task<FormResult> IFormStore<TForm>.CreateAsync(TForm form,
            CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }
            await Task.WhenAll(
                this._formCollection.InsertOneAsync(
                    form,
                    cancellationToken: cancellationToken
                ),
                Task.Run(async () =>
                {
                    if (this._elasticClient == null)
                    {
                        return;
                    }

                    await this._elasticClient.IndexAsync(
                        form, m => m.Id(form.Id)
                    );
                })
            );

            return FormResult.Success;
        }

        async Task<FormResult> IFormStore<TForm>.DeleteAsync(TForm form,
            CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }

            cancellationToken.ThrowIfCancellationRequested();
            form.Delete();

            var query = Builders<TForm>.Filter.Eq(f => f.Id, form.Id);
            var update = Builders<TForm>
                .Update
                .Set(f => f.DeleteOn, form.DeleteOn);

            var result = (await Task.WhenAll(
                this._formCollection.UpdateOneAsync(
                    query,
                    update,
                    new UpdateOptions { IsUpsert = false },
                    cancellationToken
                ),
                Task.Run(async () =>
                {
                    if (this._elasticClient == null)
                    {
                        return (UpdateResult)UpdateResult.Unacknowledged.Instance;
                    }

                    var response = await this._elasticClient.DeleteAsync<TForm>(
                        DocumentPath<TForm>.Id(form.Id)
                    );

                    return (UpdateResult)UpdateResult.Unacknowledged.Instance;
                })
            ))[0];
            return result.IsModifiedCountAvailable && result.ModifiedCount == 1
                ? FormResult.Success
                : FormResult.Failed();
        }

        async Task<TForm> IFormStore<TForm>.FindByIdAsync(string id, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var query = Builders<TForm>.Filter.And(
                Builders<TForm>.Filter.Eq(f => f.Id, id),
                Builders<TForm>.Filter.Eq(f => f.DeleteOn, null)
            );

            return await this._formCollection.Find(query).FirstOrDefaultAsync(cancellationToken)
                .ConfigureAwait(false);
        }

        async Task<IEnumerable<TForm>> IFormStore<TForm>.QueryAsync(string queryString, int count, CancellationToken cancellationToken)
        {
            if (queryString == null)
            {
                throw new ArgumentNullException(nameof(queryString));
            }
            if (this._elasticClient == null)
            {
                throw new NullReferenceException(nameof(this._elasticClient));
            }

            var response = await this._elasticClient.SearchAsync<TForm>(s => s
                .From(0).Size(count).Query(q =>
                q.QueryString(m => m.Query(queryString))
            ));

            return response.Documents;
        }

        async Task<FormResult> IFormStore<TForm>.UpdateAsync(TForm form, CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }

            var query = Builders<TForm>.Filter.And(
                Builders<TForm>.Filter.Eq(f => f.DeleteOn, null),
                Builders<TForm>.Filter.Eq(f => f.Id, form.Id)
            );
            
            var result = (await Task.WhenAll(
                this._formCollection.ReplaceOneAsync(
                    query,
                    form,
                    new UpdateOptions { IsUpsert = false },
                    cancellationToken
                ),
                Task.Run(async () => 
                {
                    if (this._elasticClient == null)
                    {
                        return (ReplaceOneResult)ReplaceOneResult.Unacknowledged.Instance;
                    }

                    await this._elasticClient.UpdateAsync<TForm>(
                        DocumentPath<TForm>.Id(form.Id),
                        update => update.Doc(form)
                    );

                    return (ReplaceOneResult)ReplaceOneResult.Unacknowledged.Instance;
                })
            ))[0];

            return result.IsModifiedCountAvailable && result.ModifiedCount == 1
                ? FormResult.Success
                : FormResult.Failed();
        }

        Task<IEnumerable<TNewForm>> IFormStore<TForm>.QueryAsync<TNewForm>(<commit_msg>repaire update result error<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Nest;

namespace Demeter.FormComponent
{
    public sealed class DemeterFormStore<TForm> : IFormStore<TForm>
        where TForm : DemeterForm, new()
    {
        private readonly IMongoCollection<TForm> _formCollection;
        private readonly ElasticClient _elasticClient;

        public DemeterFormStore(
            IMongoDatabase database,
            string formsCollection,
            ElasticClient elasticClient = null)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }
            if (formsCollection == null)
            {
                throw new ArgumentNullException(nameof(formsCollection));
            }

            this._formCollection = database.GetCollection<TForm>(formsCollection);
            this._elasticClient = elasticClient;
        }

        async Task<FormResult> IFormStore<TForm>.CreateAsync(TForm form,
            CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }
            await Task.WhenAll(
                this._formCollection.InsertOneAsync(
                    form,
                    cancellationToken: cancellationToken
                ),
                Task.Run(async () =>
                {
                    if (this._elasticClient == null)
                    {
                        return;
                    }

                    await this._elasticClient.IndexAsync(
                        form, m => m.Id(form.Id)
                    );
                })
            );

            return FormResult.Success;
        }

        async Task<FormResult> IFormStore<TForm>.DeleteAsync(TForm form,
            CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }

            cancellationToken.ThrowIfCancellationRequested();
            form.Delete();

            var query = Builders<TForm>.Filter.Eq(f => f.Id, form.Id);
            var update = Builders<TForm>
                .Update
                .Set(f => f.DeleteOn, form.DeleteOn);

            var result = (await Task.WhenAll(
                this._formCollection.UpdateOneAsync(
                    query,
                    update,
                    new UpdateOptions { IsUpsert = false },
                    cancellationToken
                ),
                Task.Run(async () =>
                {
                    if (this._elasticClient == null)
                    {
                        return (UpdateResult)UpdateResult.Unacknowledged.Instance;
                    }

                    var response = await this._elasticClient.DeleteAsync<TForm>(
                        DocumentPath<TForm>.Id(form.Id)
                    );

                    return (UpdateResult)UpdateResult.Unacknowledged.Instance;
                })
            ))[0];
            return result.IsModifiedCountAvailable && result.ModifiedCount == 1
                ? FormResult.Success
                : FormResult.Failed();
        }

        async Task<TForm> IFormStore<TForm>.FindByIdAsync(string id, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var query = Builders<TForm>.Filter.And(
                Builders<TForm>.Filter.Eq(f => f.Id, id),
                Builders<TForm>.Filter.Eq(f => f.DeleteOn, null)
            );

            return await this._formCollection.Find(query).FirstOrDefaultAsync(cancellationToken)
                .ConfigureAwait(false);
        }

        async Task<IEnumerable<TForm>> IFormStore<TForm>.QueryAsync(string queryString, int count, CancellationToken cancellationToken)
        {
            if (queryString == null)
            {
                throw new ArgumentNullException(nameof(queryString));
            }
            if (this._elasticClient == null)
            {
                throw new NullReferenceException(nameof(this._elasticClient));
            }

            var response = await this._elasticClient.SearchAsync<TForm>(s => s
                .From(0).Size(count).Query(q =>
                q.QueryString(m => m.Query(queryString))
            ));

            return response.Documents;
        }

        async Task<FormResult> IFormStore<TForm>.UpdateAsync(TForm form, CancellationToken cancellationToken)
        {
            if (form == null)
            {
                throw new ArgumentNullException(nameof(form));
            }

            var query = Builders<TForm>.Filter.And(
                Builders<TForm>.Filter.Eq(f => f.DeleteOn, null),
                Builders<TForm>.Filter.Eq(f => f.Id, form.Id)
            );
            
            var result = (await Task.WhenAll(
                this._formCollection.ReplaceOneAsync(
                    query,
                    form,
                    new UpdateOptions { IsUpsert = false },
                    cancellationToken
                ),
                Task.Run(async () => 
                {
                    if (this._elasticClient == null)
                    {
                        return (ReplaceOneResult)ReplaceOneResult.Unacknowledged.Instance;
                    }

                    await this._elasticClient.UpdateAsync<TForm>(
                        DocumentPath<TForm>.Id(form.Id),
                        update => update.Doc(form)
                    );

                    return (ReplaceOneResult)ReplaceOneResult.Unacknowledged.Instance;
                })
            ))[0];

            return result.IsModifiedCountAvailable && result.MatchedCount == 1
                ? FormResult.Success
                : FormResult.Failed();
        }

        Task<IEnumerable<TNewForm>> IFormStore<TForm>.QueryAsync<TNewForm>(
{'fraction_non_alphanumeric': 0.08652057896013585, 'fraction_numerical': 0.0008086035416835126, 'mean_word_length': 1.3775470972702806, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2003259', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2869, 'n_words': 518}