<commit_before>﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Text;

namespace SharedMemory
{
    public class VarsMemory
    {
        private const string VARS_MAP_MEMORY = "VARS_MAP_MEMORY";
        private const string VARS_LENGTH = "VarsLength";
        private MemoryMappedViewAccessor _memoryVarsViewAccesor;
        private int _kbSize=1;

        private Dictionary<string, object> Vars { get; set; }

        public VarsMemory()
        {
            _memoryVarsViewAccesor = SharedMemory.CreateMapFileAndAccesorByKb(VARS_MAP_MEMORY, _kbSize);
            Vars = new Dictionary<string, object>() { { VARS_LENGTH, 0 } };
            WriteVars();
        }

        public void SetVar(string varName, object value)
        {
            Vars = GetVars();
            Vars[varName] = value;
            Vars[VARS_LENGTH] = Vars.Keys.Count-1;
            WriteVars();
        }

        public object GetVar(string varName)
        {
            Vars = GetVars();
            object result = Vars.TryGetValue(varName, out result);
            return result;
        }

        public Dictionary<string, object> GetVars()
        {
            int bytesSize = (int)(_kbSize * 1024);
            byte[] buffer = new byte[bytesSize];
            _memoryVarsViewAccesor.ReadArray(0, buffer, 0, bytesSize);
            string json = Encoding.UTF8.GetString(buffer);
            Vars = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
            return Vars;
        }

        private void WriteVars()
        {
            int bytesSize = (int)(_kbSize * 1024);
            string input = JsonConvert.SerializeObject(Vars);
            byte[] data = Encoding.UTF8.GetBytes(input);            
            _memoryVarsViewAccesor.WriteArray(0, new byte[bytesSize], 0, bytesSize);//clear data
            if (data.Length <= (_kbSize * 1024))
            {
                _memoryVarsViewAccesor.WriteArray(0, data, 0, data.Length);
            }
            else
            {
                Vars = new Dictionary<string, object>() { { "error", $"Data too long, maximium allowed {(_kbSize * 1024)} bytes but was {data.Length}"   } };
                WriteVars();
            }
        }
    }
}<commit_msg>fix VarsMemory<commit_after>﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Text;

namespace SharedMemory
{
    public class VarsMemory
    {
        private const string VARS_MAP_MEMORY = "VARS_MAP_MEMORY";
        private const string VARS_LENGTH = "VarsLength";
        private MemoryMappedViewAccessor _memoryVarsViewAccesor;
        private int _kbSize=1;

        private Dictionary<string, object> Vars { get; set; }

        public VarsMemory()
        {
            _memoryVarsViewAccesor = SharedMemory.CreateMapFileAndAccesorByKb(VARS_MAP_MEMORY, _kbSize);
            Vars = new Dictionary<string, object>() { { VARS_LENGTH, 0 } };
            WriteVars();
        }

        public void SetVar(string varName, object value)
        {
            Vars = GetVars();
            Vars[varName] = value;
            Vars[VARS_LENGTH] = Vars.Keys.Count-1;
            WriteVars();
        }

        public object GetVar(string varName)
        {
            Vars = GetVars();
            object result;
            Vars.TryGetValue(varName, out result);
            return result;
        }

        public Dictionary<string, object> GetVars()
        {
            int bytesSize = _kbSize * 1024;
            byte[] buffer = new byte[bytesSize];
            _memoryVarsViewAccesor.ReadArray(0, buffer, 0, bytesSize);
            string json = Encoding.UTF8.GetString(buffer);
            Vars = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
            return Vars;
        }

        private void WriteVars()
        {
            int bytesSize = _kbSize * 1024;
            string input = JsonConvert.SerializeObject(Vars);
            byte[] data = Encoding.UTF8.GetBytes(input);            
            _memoryVarsViewAccesor.WriteArray(0, new byte[bytesSize], 0, bytesSize);//clear data
            if (data.Length <= (_kbSize * 1024))
            {
                _memoryVarsViewAccesor.WriteArray(0, data, 0, data.Length);
            }
            else
            {
                Vars = new Dictionary<string, object>() { { "error", $"Data too long, maximium allowed {(_kbSize * 1024)} bytes but was {data.Length}"   } };
                WriteVars();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09776413853572995, 'fraction_numerical': 0.01183691363437089, 'mean_word_length': 1.941972920696325, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1871278', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1284, 'n_words': 285}