<commit_before>﻿namespace Miruken.Callback.Policy
{
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using Bindings;

    public class CachedHandlerDescriptorFactory : IHandlerDescriptorFactory
    {
        private readonly IHandlerDescriptorFactory _factory;
        private readonly ConcurrentDictionary<Tuple<CallbackPolicy, object, bool, bool>,
            IEnumerable<HandlerDescriptor>> _descriptors;
        private readonly ConcurrentDictionary<CallbackPolicy, IEnumerable<PolicyMemberBinding>> _bindings;

        public CachedHandlerDescriptorFactory(IHandlerDescriptorFactory factory)
        {
            _factory     = factory;
            _descriptors = new ConcurrentDictionary<
                Tuple<CallbackPolicy, object, bool, bool>, IEnumerable<HandlerDescriptor>>();
            _bindings    = new ConcurrentDictionary<CallbackPolicy, IEnumerable<PolicyMemberBinding>>();
        }

        public HandlerDescriptor GetDescriptor(Type type)
        {
            return _factory.GetDescriptor(type);
        }

        public HandlerDescriptor RegisterDescriptor(
            Type type, HandlerDescriptorVisitor visitor = null, int? priority = null)
        {
            var descriptor = _factory.RegisterDescriptor(type, visitor, priority);
            _descriptors.Clear();
            _bindings.Clear();
            return descriptor;
        }

        public IEnumerable<PolicyMemberBinding> GetPolicyMembers(CallbackPolicy policy)
        {
            return _bindings.GetOrAdd(policy, _factory.GetPolicyMembers);
        }

        public IEnumerable<HandlerDescriptor> GetInstanceHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                 ? _factory.GetInstanceHandlers(policy, callback)
                 : _descriptors.GetOrAdd(Tuple.Create(policy, key, true, false),
                     k => _factory.GetInstanceHandlers(policy, callback));
        }

        public IEnumerable<HandlerDescriptor> GetStaticHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                ? _factory.GetStaticHandlers(policy, callback)
                : _descriptors.GetOrAdd(Tuple.Create(policy, key, false, true),
                    k => _factory.GetStaticHandlers(policy, callback));
        }

        public IEnumerable<HandlerDescriptor> GetCallbackHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                ? _factory.GetCallbackHandlers(policy, callback)
                : _descriptors.GetOrAdd(Tuple.Create(policy, key, true, true),
                    k => _factory.GetCallbackHandlers(policy, callback));
        }
    }
}
<commit_msg>Revert "Added CallbackPolicy to cache key"<commit_after>﻿namespace Miruken.Callback.Policy
{
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using Bindings;

    public class CachedHandlerDescriptorFactory : IHandlerDescriptorFactory
    {
        private readonly IHandlerDescriptorFactory _factory;
        private readonly ConcurrentDictionary<Tuple<bool, bool, object>, IEnumerable<HandlerDescriptor>> _descriptors;
        private readonly ConcurrentDictionary<CallbackPolicy, IEnumerable<PolicyMemberBinding>> _bindings;

        public CachedHandlerDescriptorFactory(IHandlerDescriptorFactory factory)
        {
            _factory     = factory;
            _descriptors = new ConcurrentDictionary<Tuple<bool, bool, object>, IEnumerable<HandlerDescriptor>>();
            _bindings    = new ConcurrentDictionary<CallbackPolicy, IEnumerable<PolicyMemberBinding>>();
        }

        public HandlerDescriptor GetDescriptor(Type type)
        {
            return _factory.GetDescriptor(type);
        }

        public HandlerDescriptor RegisterDescriptor(
            Type type, HandlerDescriptorVisitor visitor = null, int? priority = null)
        {
            var descriptor = _factory.RegisterDescriptor(type, visitor, priority);
            _descriptors.Clear();
            _bindings.Clear();
            return descriptor;
        }

        public IEnumerable<PolicyMemberBinding> GetPolicyMembers(CallbackPolicy policy)
        {
            return _bindings.GetOrAdd(policy, p => _factory.GetPolicyMembers(p));
        }

        public IEnumerable<HandlerDescriptor> GetInstanceHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                 ? _factory.GetInstanceHandlers(policy, callback)
                 : _descriptors.GetOrAdd(Tuple.Create(true, false, key),
                     k => _factory.GetInstanceHandlers(policy, callback));
        }

        public IEnumerable<HandlerDescriptor> GetStaticHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                ? _factory.GetStaticHandlers(policy, callback)
                : _descriptors.GetOrAdd(Tuple.Create(false, true, key),
                    k => _factory.GetStaticHandlers(policy, callback));
        }

        public IEnumerable<HandlerDescriptor> GetCallbackHandlers(CallbackPolicy policy, object callback)
        {
            var key = policy.GetKey(callback);
            return key == null
                ? _factory.GetCallbackHandlers(policy, callback)
                : _descriptors.GetOrAdd(Tuple.Create(true, true, key),
                    k => _factory.GetCallbackHandlers(policy, callback));
        }
    }
}

{'fraction_non_alphanumeric': 0.07749912922326715, 'fraction_numerical': 0.0, 'mean_word_length': 2.5363300492610836, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 38, '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': '3315021', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1329, 'n_words': 288}