<commit_before>﻿namespace BlobCache.ConcurrencyModes
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Security.AccessControl;
    using System.Security.Principal;
    using System.Threading;
    using JetBrains.Annotations;

    public class SessionConcurrencyHandler : ConcurrencyHandler
    {
        private readonly object _locker = new object();
        private MemoryMappedFile _mmf;

        private GlobalLockData _rwl;

        public SessionConcurrencyHandler()
        {
            Timeout = 15000;
        }

        protected bool IsGlobal { get; set; } = false;

        private GlobalLockData LockData
        {
            get
            {
                if (_rwl == null)
                    lock (_locker)
                    {
                        if (_rwl != null)
                            return _rwl;

                        _rwl = new GlobalLockData(Id, IsGlobal);
                    }

                return _rwl;
            }
        }

        private MemoryMappedFile Memory
        {
            get
            {
                if (_mmf == null)
                    lock (_locker)
                    {
                        if (_mmf != null)
                            return _mmf;

                        _mmf = CreateMemoryMappedFile();
                    }

                return _mmf;
            }
        }

        public override IDisposable Lock(int timeout, CancellationToken token)
        {
            return LockData.WriteLock(timeout, token);
        }

        public override StorageInfo ReadInfo()
        {
            using (var s = Memory.CreateViewStream())
            {
                return StorageInfo.ReadFromStream(s);
            }
        }

        public override void SignalReadFinish()
        {
            LockData.ReadEvent.Set();
        }

        public override void SignalWaitRequired()
        {
            LockData.ReadEvent.Reset();
        }

        public override void WaitForReadFinish(CancellationToken token)
        {
            while (true)
            {
                if (LockData.ReadEvent.WaitOne(500))
                    break;
                token.ThrowIfCancellationRequested();
            }
        }

        public override void WriteInfo(StorageInfo info)
        {
            using (var s = Memory.CreateViewStream())
            {
                info.WriteToStream(s);
            }
        }

        protected virtual MemoryMappedFile CreateMemoryMappedFile()
        {
            var security = new MemoryMappedFileSecurity();
            var rule = new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow);
            security.AddAccessRule(rule);

            return MemoryMappedFile.CreateOrOpen($"{(IsGlobal ? "Global\\" : "")}BlobStorage-{Id}-Info", 1024 * 1024, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, security, HandleInheritability.None);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
<commit_msg>Flush stream after write info<commit_after>﻿namespace BlobCache.ConcurrencyModes
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.IO.MemoryMappedFiles;
    using System.Security.AccessControl;
    using System.Security.Principal;
    using System.Threading;
    using JetBrains.Annotations;

    public class SessionConcurrencyHandler : ConcurrencyHandler
    {
        private readonly object _locker = new object();
        private MemoryMappedFile _mmf;

        private GlobalLockData _rwl;

        public SessionConcurrencyHandler()
        {
            Timeout = 15000;
        }

        protected bool IsGlobal { get; set; } = false;

        private GlobalLockData LockData
        {
            get
            {
                if (_rwl == null)
                    lock (_locker)
                    {
                        if (_rwl != null)
                            return _rwl;

                        _rwl = new GlobalLockData(Id, IsGlobal);
                    }

                return _rwl;
            }
        }

        private MemoryMappedFile Memory
        {
            get
            {
                if (_mmf == null)
                    lock (_locker)
                    {
                        if (_mmf != null)
                            return _mmf;

                        _mmf = CreateMemoryMappedFile();
                    }

                return _mmf;
            }
        }

        public override IDisposable Lock(int timeout, CancellationToken token)
        {
            return LockData.WriteLock(timeout, token);
        }

        public override StorageInfo ReadInfo()
        {
            using (var s = Memory.CreateViewStream())
            {
                return StorageInfo.ReadFromStream(s);
            }
        }

        public override void SignalReadFinish()
        {
            LockData.ReadEvent.Set();
        }

        public override void SignalWaitRequired()
        {
            LockData.ReadEvent.Reset();
        }

        public override void WaitForReadFinish(CancellationToken token)
        {
            while (true)
            {
                if (LockData.ReadEvent.WaitOne(500))
                    break;
                token.ThrowIfCancellationRequested();
            }
        }

        public override void WriteInfo(StorageInfo info)
        {
            using (var s = Memory.CreateViewStream())
            {
                info.WriteToStream(s);
                s.Flush();
            }
        }

        protected virtual MemoryMappedFile CreateMemoryMappedFile()
        {
            var security = new MemoryMappedFileSecurity();
            var rule = new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow);
            security.AddAccessRule(rule);

            return MemoryMappedFile.CreateOrOpen($"{(IsGlobal ? "Global\\" : "")}BlobStorage-{Id}-Info", 1024 * 1024, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, security, HandleInheritability.None);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

{'fraction_non_alphanumeric': 0.0728343406167674, 'fraction_numerical': 0.004958933829226716, 'mean_word_length': 1.4363910909777275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1115337', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1536, 'n_words': 337}