<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hadouken.Configuration;
using Hadouken.Data;
using Hadouken.Data.Models;
using System.Web.Script.Serialization;
using System.Linq.Expressions;
using Hadouken.Messaging;
using Hadouken.Messages;
using Hadouken.Security;

using Microsoft.Win32;

namespace Hadouken.Impl.Config
{
    public class DefaultKeyValueStore : IKeyValueStore
    {
        private readonly JavaScriptSerializer _serializer = new JavaScriptSerializer();
        private readonly IDataRepository _data;
        private readonly IMessageBus _bus;

        public DefaultKeyValueStore(IDataRepository data, IMessageBus bus)
        {
            _data = data;
            _bus = bus;
        }

        public object Get(string key)
        {
            return InternalGet(key, null, false);
        }

        public object Get(string key, object defaultValue)
        {
            return InternalGet(key, defaultValue, true);
        }

        private object InternalGet(string key, object defaultValue, bool userProvidedDefaultValue)
        {
            object v;

            if (TryGet(key, out v))
            {
                return v;
            }

            if (userProvidedDefaultValue)
                return defaultValue;

            throw new Exception("Key does not exist: " + key);
        }

        public bool TryGet(string key, out object value)
        {
            object internalValue = GetFromDatabase(key);;

            if (internalValue != null)
            {
                value = internalValue;
                return true;
            }

            value = null;
            return false;
        }

        private object GetFromDatabase(string key)
        {
            var setting = GetSettingFromDatabase(key);

            if (setting == null)
                return null;

            var type = Type.GetType(setting.Type);

            if (type == null)
                throw new Exception("Cannot deserialize setting with no known type");

            return _serializer.Deserialize(setting.Value, type);
        }

        private T GetFromDatabase<T>(string key)
        {
            var setting = GetSettingFromDatabase(key);

            if (setting == null)
                return default(T);

            return _serializer.Deserialize<T>(setting.Value);
        }

        private Setting GetSettingFromDatabase(string key)
        {
            var setting = _data.Single<Setting>(s => s.Key == key);
<commit_msg>Fixes a NullReferenceException when the Setting does not exist.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hadouken.Configuration;
using Hadouken.Data;
using Hadouken.Data.Models;
using System.Web.Script.Serialization;
using System.Linq.Expressions;
using Hadouken.Messaging;
using Hadouken.Messages;
using Hadouken.Security;

using Microsoft.Win32;

namespace Hadouken.Impl.Config
{
    public class DefaultKeyValueStore : IKeyValueStore
    {
        private readonly JavaScriptSerializer _serializer = new JavaScriptSerializer();
        private readonly IDataRepository _data;
        private readonly IMessageBus _bus;

        public DefaultKeyValueStore(IDataRepository data, IMessageBus bus)
        {
            _data = data;
            _bus = bus;
        }

        public object Get(string key)
        {
            return InternalGet(key, null, false);
        }

        public object Get(string key, object defaultValue)
        {
            return InternalGet(key, defaultValue, true);
        }

        private object InternalGet(string key, object defaultValue, bool userProvidedDefaultValue)
        {
            object v;

            if (TryGet(key, out v))
            {
                return v;
            }

            if (userProvidedDefaultValue)
                return defaultValue;

            throw new Exception("Key does not exist: " + key);
        }

        public bool TryGet(string key, out object value)
        {
            object internalValue = GetFromDatabase(key);;

            if (internalValue != null)
            {
                value = internalValue;
                return true;
            }

            value = null;
            return false;
        }

        private object GetFromDatabase(string key)
        {
            var setting = GetSettingFromDatabase(key);

            if (setting == null)
                return null;

            var type = Type.GetType(setting.Type);

            if (type == null)
                throw new Exception("Cannot deserialize setting with no known type");

            return _serializer.Deserialize(setting.Value, type);
        }

        private T GetFromDatabase<T>(string key)
        {
            var setting = GetSettingFromDatabase(key);

            if (setting == null)
                return default(T);

            return _serializer.Deserialize<T>(setting.Value);
        }

        private Setting GetSettingFromDatabase(string key)
        {
            var setting = _data.Single<Setting>(s => s.Key == key);

            if (setting == null)
                return null;

{'fraction_non_alphanumeric': 0.07516778523489932, 'fraction_numerical': 0.0007670182166826462, 'mean_word_length': 1.858082191780822, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3430603', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1268, 'n_words': 361}