<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Akka.Event;

namespace Akka.Actor
{
    internal class InboxActor : ActorBase, IActorLogging
    {
        private readonly InboxQueue<object> _messages = new InboxQueue<object>();
        private readonly InboxQueue<IQuery> _clients = new InboxQueue<IQuery>();

        private readonly ISet<IQuery> _clientsByTimeout = new SortedSet<IQuery>(DeadlineComparer.Instance);
        private bool _printedWarning;

        private object _currentMessage;
        private Select? _currentSelect;
        private Tuple<DateTime, CancellationTokenSource> _currentDeadline;

        private int size;
        private LoggingAdapter log = Logging.GetLogger(Context);
        public LoggingAdapter Log { get { return log; } }

        public InboxActor(int size)
        {
            this.size = size;
        }

        public void EnqueueQuery(IQuery q)
        {
            var query = q.WithClient(Sender);
            _clients.Enqueue(query);
            _clientsByTimeout.Add(query);
        }

        public void EnqueueMessage(object msg)
        {
            if (_messages.Count < size)
            {
                _messages.Enqueue(msg);
            }
            else
            {
                if (!_printedWarning)
                {
                    Log.Warn("Dropping message: Inbox size has been exceeded, use akka.actor.inbox.inbox-size to increase maximum allowed inbox size. Current is " + size);
                    _printedWarning = true;
                }
            }
        }

        public bool ClientPredicate(IQuery query)
        {
            if (query is Get)
            {
                return true;
            }
            else if (query is Select)
            {
                return ((Select)query).Predicate(_currentMessage);
            }
            else
            {
                return false;
            }
        }

        public bool MessagePredicate(object msg)
        {
            if (!_currentSelect.HasValue)
            {
                return false;
            }

            return _currentSelect.Value.Predicate(msg);
        }

        protected override bool Receive(object message)
        {
            PatternMatch.Match(message)
                .With<Get>(get =>
                {
                    if (_messages.Count == 0)
                    {
                        EnqueueQuery(get);
                    }
                    else
                    {
                        Sender.Tell(_messages.Dequeue());
                    }
                })
                .With<Select>(select =>
                {
                    if (_messages.Count == 0)
                    {
                        EnqueueQuery(select);
                    }
                    else
                    {
                        _currentSelect = select;
                        var firstMatch = _messages.DequeueFirstOrDefault(MessagePredicate);
                        if (firstMatch == null)
                        {
                            EnqueueQuery(select);
                        }
                        else
                        {
                            Sender.Tell(firstMatch);
                        }
                        _currentSelect = null;
                    }
                })
                .With<StartWatch>(sw => Context.Watch(sw.Target))
                .With<Kick>(() =>
                {
                    var now = DateTime.Now;
                    var overdue = _clientsByTimeout.TakeWhile(q => q.Deadline < now);
                    foreach (var query in overdue)
                    {
                        query.Client.Tell(new Status.Failure(new TimeoutException("Deadline passed")));
                    }
                    _clients.RemoveAll(q => q.Deadline < now);
                    _clientsByTimeout.IntersectWith(_clientsByTimeout.Where(q => q.Deadline >= now));
                }).Default(msg =>
                {
                    if (_clients.Count == 0)
                    {
                        EnqueueMessage(msg);
                    }
                    else
                    {
                        _currentMessage = msg;
                        var firstMatch = _clients.DequeueFirstOrDefault(ClientPredicate); //TODO: this should work as DequeueFirstOrDefault
                        if (firstMatch != null)
                        {
                            _clientsByTimeout.ExceptWith(new[] { firstMatch });
                            firstMatch.Client.Tell(msg);
                        }
                        else
                        {
                            EnqueueMessage(msg);
                        }
                        _currentMessage = null;
                    }
                });

            if (_clients.Count == 0)
            {
                if (_currentDeadline != null)
                {
                    _currentDeadline.Item2.Cancel();
                    _currentDeadline = null;
                }
            }
            else
            {
                var next = _clientsByTimeout.Head().Deadline;
                if (_currentDeadline != null)
                {
                    _currentDeadline.Item2.Cancel();
                }
                var cancellationTokenSource = new CancellationTokenSource();
                Context.System.Scheduler.ScheduleOnce(next - DateTime.Now, Self, new Kick(), cancellationTokenSource.Token);

                _currentDeadline = Tuple.Create(next, cancellationTokenSource);
            }

            return true;
        }
    }
<commit_msg>issue-266: fixed bug with null ref and modified enumerable element<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Akka.Event;

namespace Akka.Actor
{
    internal class InboxActor : ActorBase, IActorLogging
    {
        private readonly InboxQueue<object> _messages = new InboxQueue<object>();
        private readonly InboxQueue<IQuery> _clients = new InboxQueue<IQuery>();

        private readonly ISet<IQuery> _clientsByTimeout = new SortedSet<IQuery>(DeadlineComparer.Instance);
        private bool _printedWarning;

        private object _currentMessage;
        private Select? _currentSelect;
        private Tuple<DateTime, CancellationTokenSource> _currentDeadline;

        private int size;
        private LoggingAdapter log = Logging.GetLogger(Context);
        public LoggingAdapter Log { get { return log; } }

        public InboxActor(int size)
        {
            this.size = size;
        }

        public void EnqueueQuery(IQuery q)
        {
            var query = q.WithClient(Sender);
            _clients.Enqueue(query);
            _clientsByTimeout.Add(query);
        }

        public void EnqueueMessage(object msg)
        {
            if (_messages.Count < size)
            {
                _messages.Enqueue(msg);
            }
            else
            {
                if (!_printedWarning)
                {
                    Log.Warn("Dropping message: Inbox size has been exceeded, use akka.actor.inbox.inbox-size to increase maximum allowed inbox size. Current is " + size);
                    _printedWarning = true;
                }
            }
        }

        public bool ClientPredicate(IQuery query)
        {
            if (query is Get)
            {
                return true;
            }
            else if (query is Select)
            {
                return ((Select)query).Predicate(_currentMessage);
            }
            else
            {
                return false;
            }
        }

        public bool MessagePredicate(object msg)
        {
            if (!_currentSelect.HasValue)
            {
                return false;
            }

            return _currentSelect.Value.Predicate(msg);
        }

        protected override bool Receive(object message)
        {
            PatternMatch.Match(message)
                .With<Get>(get =>
                {
                    if (_messages.Count == 0)
                    {
                        EnqueueQuery(get);
                    }
                    else
                    {
                        Sender.Tell(_messages.Dequeue());
                    }
                })
                .With<Select>(select =>
                {
                    if (_messages.Count == 0)
                    {
                        EnqueueQuery(select);
                    }
                    else
                    {
                        _currentSelect = select;
                        var firstMatch = _messages.DequeueFirstOrDefault(MessagePredicate);
                        if (firstMatch == null)
                        {
                            EnqueueQuery(select);
                        }
                        else
                        {
                            Sender.Tell(firstMatch);
                        }
                        _currentSelect = null;
                    }
                })
                .With<StartWatch>(sw => Context.Watch(sw.Target))
                .With<Kick>(() =>
                {
                    var now = DateTime.Now;
                    var overdue = _clientsByTimeout.TakeWhile(q => q.Deadline < now);
                    foreach (var query in overdue)
                    {
                        query.Client.Tell(new Status.Failure(new TimeoutException("Deadline passed")));
                    }
                    _clients.RemoveAll(q => q.Deadline < now);
                    var afterDeadline = _clientsByTimeout.Where(q => q.Deadline >= now).ToList();
                    _clientsByTimeout.IntersectWith(afterDeadline);
                }).Default(msg =>
                {
                    if (_clients.Count == 0)
                    {
                        EnqueueMessage(msg);
                    }
                    else
                    {
                        _currentMessage = msg;
                        var firstMatch = _clients.DequeueFirstOrDefault(ClientPredicate); //TODO: this should work as DequeueFirstOrDefault
                        if (firstMatch != null)
                        {
                            _clientsByTimeout.ExceptWith(new[] { firstMatch });
                            firstMatch.Client.Tell(msg);
                        }
                        else
                        {
                            EnqueueMessage(msg);
                        }
                        _currentMessage = null;
                    }
                });

            if (_clients.Count == 0)
            {
                if (_currentDeadline != null)
                {
                    _currentDeadline.Item2.Cancel();
                    _currentDeadline = null;
                }
            }
            else
            {
                var next = _clientsByTimeout.FirstOrDefault();
                if (next != null)
                {
                    if (_currentDeadline != null)
                    {
                        _currentDeadline.Item2.Cancel();
                    }
                    var cancellationTokenSource = new CancellationTokenSource();
                    Context.System.Scheduler.ScheduleOnce(next.Deadline - DateTime.Now, Self, new Kick(),
                        cancellationTokenSource.Token);

                    _currentDeadline = Tuple.Create(next.Deadline, cancellationTokenSource);
                }
            }

            return true;
        }
    }

{'fraction_non_alphanumeric': 0.07564607222317303, 'fraction_numerical': 0.0012835871983570084, 'mean_word_length': 1.0125710349578096, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 43, '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': '2868555', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2547, 'n_words': 521}