<commit_before>﻿using System.Collections.Generic;
using System.Linq;

using Newtonsoft.Json.Linq;

namespace Microsoft.Deployment.Common.ActionModel
{
    /// <summary>
    /// This class stores both public and private details for the application
    /// This class should resemble the typescript to ensure consistency
    /// </summary>
    public class DataStore
    {
        public string CurrentRoutePage { get; set; }
        public string DeploymentIndex { get; set; }

        public string CurrentRoute => CurrentRoutePage + "-" + DeploymentIndex;

        public DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>> PublicDataStore { get; set; } = new DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>>();

        public DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>> PrivateDataStore { get; set; } = new DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>>();

        // Removed for the time being to force usage of getfirst and getall
        //public JToken this[string route, string key]
        //{
        //    get
        //    {
        //        return this.GetValueWithRouteAndKey(DataStoreType.Any, route, key);
        //    }
        //    set
        //    {
        //        this.UpdateValue(DataStoreType.Public, route, key, value);
        //    }
        //}

        //public JToken this[DataStoreType type, string route, string key]
        //{
        //    get
        //    {
        //        return this.GetValueWithRouteAndKey(type, route, key);
        //    }
        //    set
        //    {
        //        this.UpdateValue(type, route, key, value);
        //    }
        //}

        //public IList<DataStoreItem> this[string key, DataStoreType dataStoreType = DataStoreType.Any]
        //{
        //    get
        //    {
        //        return this.GetValueAndRoutesFromDataStore(DataStoreType.Any, key);
        //    }
        //}

        //public IList<DataStoreItem> this[DataStoreType type, string key]
        //{
        //    get
        //    {
        //        return this.GetValueAndRoutesFromDataStore(type, key);
        //    }
        //}

        public bool RouteExists(string route, DataStoreType dataStoreType = DataStoreType.Any)
        {
            bool found = false;

            if (dataStoreType == DataStoreType.Private || dataStoreType == DataStoreType.Any)
            {
                if (PrivateDataStore.ContainsKey(route))
                {
                    found = true;
                }
            }

            if (dataStoreType == DataStoreType.Public || dataStoreType == DataStoreType.Any)
            {
                if (PublicDataStore.ContainsKey(route))
                {
                    found = true;
                }
            }

            return found;
        }

        public bool KeyExists(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetValueAndRoutesFromDataStore(dataStoreType, key).Any();
        }

        public bool RouteAndKeyExists(string route, string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            var listOfKeys = this.GetValueAndRoutesFromDataStore(dataStoreType, key).ToList();
            if (listOfKeys.Any())
            {
                return listOfKeys.Any(p => p.Route.ToLowerInvariant().Equals(route.ToLowerInvariant()));
            }

            return false;
        }

        public void AddToDataStore(string route, string key, JToken value, DataStoreType dataStoreType = DataStoreType.Any)
        {
            this.UpdateValue(dataStoreType, route, key, value);
        }

        public void AddToDataStore(string key, JToken value, DataStoreType dataStoreType = DataStoreType.Any)
        {
            this.UpdateValue(dataStoreType, this.CurrentRoute, key, value);
        }

        public void AddObjectDataStore(string route, JObject value, DataStoreType dataStoreType)
        {
            foreach (var val in value)
            {
                this.UpdateValue(dataStoreType, route, val.Key, val.Value);
            }
        }

        public void AddObjectDataStore(JObject value, DataStoreType dataStoreType)
        {
            foreach (var val in value)
            {
                this.UpdateValue(dataStoreType, this.CurrentRoute, val.Key, val.Value);
            }
        }

        public string GetValue(string route, string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetValueWithRouteAndKey(dataStoreType, route, key)?.ToString();
        }

        public string GetValue(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetLastValue(key, dataStoreType);
            //return this.GetFirstValueFromDataStore(key, dataStoreType)?.ToString();
        }

        public string GetLastValue(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetLastValueFromDataStore(key, dataStoreType)?.ToString();
        }

        public string GetValueAtIndex(string key, string index, DataStoreType dataStoreType = DataStoreType.Any)
        {
            string result = null;
            if (this.KeyExists(index))
            {
                int i = int.Parse(this.GetValue(index));
                IList<string> allValues = this.GetAllValues(key);
                if (allValues.Count > i)
                {
                    result = this.GetAllValues(key)[i];
                }
            }
            return result;
        }

        public JToken GetJson(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetFirstValueFromDataStore(key, dataStoreType);
        }

        public string GetJson(string key, string property, DataStoreType dataStoreType = DataStoreType.Any)
        {
            string result = null;
            JToken tokenKey = this.GetFirstValueFromDataStore(key, dataStoreType);
            if (tokenKey != null)
            {
                JToken tokenProperty = tokenKey[property];
                if (tokenProperty != null)
                {
                    result = tokenProperty.ToString();
                }
            }<commit_msg>default sqlserverindex to 0<commit_after>﻿using System.Collections.Generic;
using System.Linq;

using Newtonsoft.Json.Linq;

namespace Microsoft.Deployment.Common.ActionModel
{
    /// <summary>
    /// This class stores both public and private details for the application
    /// This class should resemble the typescript to ensure consistency
    /// </summary>
    public class DataStore
    {
        public string CurrentRoutePage { get; set; }
        public string DeploymentIndex { get; set; }

        public string CurrentRoute => CurrentRoutePage + "-" + DeploymentIndex;

        public DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>> PublicDataStore { get; set; } = new DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>>();

        public DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>> PrivateDataStore { get; set; } = new DictionaryIgnoreCase<DictionaryIgnoreCase<JToken>>();

        // Removed for the time being to force usage of getfirst and getall
        //public JToken this[string route, string key]
        //{
        //    get
        //    {
        //        return this.GetValueWithRouteAndKey(DataStoreType.Any, route, key);
        //    }
        //    set
        //    {
        //        this.UpdateValue(DataStoreType.Public, route, key, value);
        //    }
        //}

        //public JToken this[DataStoreType type, string route, string key]
        //{
        //    get
        //    {
        //        return this.GetValueWithRouteAndKey(type, route, key);
        //    }
        //    set
        //    {
        //        this.UpdateValue(type, route, key, value);
        //    }
        //}

        //public IList<DataStoreItem> this[string key, DataStoreType dataStoreType = DataStoreType.Any]
        //{
        //    get
        //    {
        //        return this.GetValueAndRoutesFromDataStore(DataStoreType.Any, key);
        //    }
        //}

        //public IList<DataStoreItem> this[DataStoreType type, string key]
        //{
        //    get
        //    {
        //        return this.GetValueAndRoutesFromDataStore(type, key);
        //    }
        //}

        public bool RouteExists(string route, DataStoreType dataStoreType = DataStoreType.Any)
        {
            bool found = false;

            if (dataStoreType == DataStoreType.Private || dataStoreType == DataStoreType.Any)
            {
                if (PrivateDataStore.ContainsKey(route))
                {
                    found = true;
                }
            }

            if (dataStoreType == DataStoreType.Public || dataStoreType == DataStoreType.Any)
            {
                if (PublicDataStore.ContainsKey(route))
                {
                    found = true;
                }
            }

            return found;
        }

        public bool KeyExists(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetValueAndRoutesFromDataStore(dataStoreType, key).Any();
        }

        public bool RouteAndKeyExists(string route, string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            var listOfKeys = this.GetValueAndRoutesFromDataStore(dataStoreType, key).ToList();
            if (listOfKeys.Any())
            {
                return listOfKeys.Any(p => p.Route.ToLowerInvariant().Equals(route.ToLowerInvariant()));
            }

            return false;
        }

        public void AddToDataStore(string route, string key, JToken value, DataStoreType dataStoreType = DataStoreType.Any)
        {
            this.UpdateValue(dataStoreType, route, key, value);
        }

        public void AddToDataStore(string key, JToken value, DataStoreType dataStoreType = DataStoreType.Any)
        {
            this.UpdateValue(dataStoreType, this.CurrentRoute, key, value);
        }

        public void AddObjectDataStore(string route, JObject value, DataStoreType dataStoreType)
        {
            foreach (var val in value)
            {
                this.UpdateValue(dataStoreType, route, val.Key, val.Value);
            }
        }

        public void AddObjectDataStore(JObject value, DataStoreType dataStoreType)
        {
            foreach (var val in value)
            {
                this.UpdateValue(dataStoreType, this.CurrentRoute, val.Key, val.Value);
            }
        }

        public string GetValue(string route, string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetValueWithRouteAndKey(dataStoreType, route, key)?.ToString();
        }

        public string GetValue(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetLastValue(key, dataStoreType);
            //return this.GetFirstValueFromDataStore(key, dataStoreType)?.ToString();
        }

        public string GetLastValue(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetLastValueFromDataStore(key, dataStoreType)?.ToString();
        }

        public string GetValueAtIndex(string key, string index, DataStoreType dataStoreType = DataStoreType.Any)
        {
            string result = null;

            int i = this.KeyExists(index) ? int.Parse(this.GetValue(index)) : 0;
            IList<string> allValues = this.GetAllValues(key);
            if (allValues.Count > i)
            {
                result = this.GetAllValues(key)[i];
            }

            return result;
        }

        public JToken GetJson(string key, DataStoreType dataStoreType = DataStoreType.Any)
        {
            return this.GetFirstValueFromDataStore(key, dataStoreType);
        }

        public string GetJson(string key, string property, DataStoreType dataStoreType = DataStoreType.Any)
        {
            string result = null;
            JToken tokenKey = this.GetFirstValueFromDataStore(key, dataStoreType);
            if (tokenKey != null)
            {
                JToken tokenProperty = tokenKey[property];
                if (tokenProperty != null)
                {
                    result = tokenProperty.ToString();
                }
            }
{'fraction_non_alphanumeric': 0.0907696034886538, 'fraction_numerical': 0.00016151174997981103, 'mean_word_length': 1.7816711590296497, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, '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': '2497183', 'n_tokens_mistral': 3235, 'n_tokens_neox': 3142, 'n_words': 701}