<commit_before>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Mocoding.EasyDocDb.Core
{
    internal class Document<T> : IDocument<T> where T : class, new()
    {
        private const int TIMEOUT = 1000 * 2; // 2 seconds timeout to save a document.

        internal readonly string _ref;

        private readonly IDocumentSerializer _serializer;
        private readonly IDocumentStorage _storage;

        private Action<IDocument<T>> _onSave;
        private Action<IDocument<T>> _onDelete;
        
        private readonly object _dataAccess = new object();
        private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);

        public Document(string @ref, IDocumentStorage storage, IDocumentSerializer serializer, Action<IDocument<T>> onDelete = null, Action<IDocument<T>> onSave = null)
        {
            _storage = storage;
            _ref = @ref;
            _serializer = serializer;
            _onDelete = onDelete;
            _onSave = onSave;
            Data = new T();
        }

        internal async Task Init()
        {
            var content = await _storage.Read(_ref);
            Data = _serializer.Deserialize<T>(content);
        }

        public T Data { get; private set; }

        public async Task SyncUpdate(Action<T> updateAction)
        {
            if (!await _semaphore.WaitAsync(TIMEOUT))
                throw new EasyDocDbException($"Timeout! Can't get exclusive access to document.");

            try
            {
                updateAction(Data);
                await SaveInternal();
            }
            finally
            {
                _semaphore.Release();
            }
        }

        public async Task Save()
        {
            if(!await _semaphore.WaitAsync(TIMEOUT))
                throw new EasyDocDbException($"Timeout! Can't get exclusive access to document.");

            try
            {<commit_msg>Added missed document initialization.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Mocoding.EasyDocDb.Core
{
    internal class Document<T> : IDocument<T> where T : class, new()
    {
        private const int TIMEOUT = 1000 * 2; // 2 seconds timeout to save a document.

        internal readonly string _ref;

        private readonly IDocumentSerializer _serializer;
        private readonly IDocumentStorage _storage;

        private Action<IDocument<T>> _onSave;
        private Action<IDocument<T>> _onDelete;
        
        private readonly object _dataAccess = new object();
        private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);

        public Document(string @ref, IDocumentStorage storage, IDocumentSerializer serializer, Action<IDocument<T>> onDelete = null, Action<IDocument<T>> onSave = null)
        {
            _storage = storage;
            _ref = @ref;
            _serializer = serializer;
            _onDelete = onDelete;
            _onSave = onSave;
            Data = new T();
        }

        internal async Task Init()
        {
            var content = await _storage.Read(_ref);
            Data = _serializer.Deserialize<T>(content) ?? new T();
        }

        public T Data { get; private set; }

        public async Task SyncUpdate(Action<T> updateAction)
        {
            if (!await _semaphore.WaitAsync(TIMEOUT))
                throw new EasyDocDbException($"Timeout! Can't get exclusive access to document.");

            try
            {
                updateAction(Data);
                await SaveInternal();
            }
            finally
            {
                _semaphore.Release();
            }
        }

        public async Task Save()
        {
            if(!await _semaphore.WaitAsync(TIMEOUT))
                throw new EasyDocDbException($"Timeout! Can't get exclusive access to document.");

            try
            {
{'fraction_non_alphanumeric': 0.0822227809906965, 'fraction_numerical': 0.00352024138798089, 'mean_word_length': 1.9293078055964654, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '2220056', 'n_tokens_mistral': 1038, 'n_tokens_neox': 1005, 'n_words': 289}