<commit_before>﻿using System;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace MaxMind.Db
{
    internal class MemoryMapReader : IByteReader
    {
        private static readonly object FileLocker = new object();
        private readonly MemoryMappedFile _memoryMappedFile;
        private readonly MemoryMappedViewAccessor _view;
        private bool _disposed;

        public MemoryMapReader(string file)
        {
            var fileInfo = new FileInfo(file);

            Length = (int) fileInfo.Length;

            var mmfName = fileInfo.FullName.Replace("\\", "-");
            lock (FileLocker)
            {
                try
                {
                    _memoryMappedFile = MemoryMappedFile.OpenExisting(mmfName, MemoryMappedFileRights.Read);
                }
                catch (Exception ex)
                {
                    if (ex is IOException || ex is NotImplementedException)
                    {
                        _memoryMappedFile = MemoryMappedFile.CreateFromFile(file, FileMode.Open,
                            mmfName, fileInfo.Length, MemoryMappedFileAccess.Read);
                    }
                    else
                        throw;
                }
            }

            _view = _memoryMappedFile.CreateViewAccessor(0, Length, MemoryMappedFileAccess.Read);
        }

        public int Length { get; }

        public byte[] Read(int offset, int count)
        {
            var bytes = new byte[count];
            Copy(offset, bytes);
            return bytes;
        }

        public byte ReadOne(int offset) => _view.ReadByte(offset);

        public void Copy(int offset, byte[] bytes)
        {
            // Doing an unsafe read improves performance by 10%. Probably
            // not worth the increased support overhead from people asking
            // why we use unsafe.
            _view.ReadArray(offset, bytes, 0, bytes.Length);
        }

        /// <summary>
        ///     Release resources back to the system.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);<commit_msg>Update comments<commit_after>﻿using System;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace MaxMind.Db
{
    internal class MemoryMapReader : IByteReader
    {
        private static readonly object FileLocker = new object();
        private readonly MemoryMappedFile _memoryMappedFile;
        private readonly MemoryMappedViewAccessor _view;
        private bool _disposed;

        public MemoryMapReader(string file)
        {
            var fileInfo = new FileInfo(file);

            Length = (int) fileInfo.Length;

            var mmfName = fileInfo.FullName.Replace("\\", "-");
            lock (FileLocker)
            {
                try
                {
                    _memoryMappedFile = MemoryMappedFile.OpenExisting(mmfName, MemoryMappedFileRights.Read);
                }
                catch (Exception ex)
                {
                    if (ex is IOException || ex is NotImplementedException)
                    {
                        _memoryMappedFile = MemoryMappedFile.CreateFromFile(file, FileMode.Open,
                            mmfName, fileInfo.Length, MemoryMappedFileAccess.Read);
                    }
                    else
                        throw;
                }
            }

            _view = _memoryMappedFile.CreateViewAccessor(0, Length, MemoryMappedFileAccess.Read);
        }

        public int Length { get; }

        public byte[] Read(int offset, int count)
        {
            var bytes = new byte[count];
            Copy(offset, bytes);
            return bytes;
        }

        public byte ReadOne(int offset) => _view.ReadByte(offset);

        public void Copy(int offset, byte[] bytes)
        {
            // Although not explicitly marked as thread safe, from
            // reviewing the source code, these operations appear to
            // be thread safe as long as only read operations are
            // being done.
            _view.ReadArray(offset, bytes, 0, bytes.Length);
        }

        /// <summary>
        ///     Release resources back to the system.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
{'fraction_non_alphanumeric': 0.07695895522388059, 'fraction_numerical': 0.0013992537313432835, 'mean_word_length': 1.4965075669383003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2202726', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1017, 'n_words': 282}