<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Persistence.Journal;
using Akka.Serialization;
using MongoDB.Driver;

namespace Akka.Persistence.MongoDb.Journal
{
    /// <summary>
    /// An Akka.NET journal implementation that writes events asynchronously to MongoDB.
    /// </summary>
    public class MongoDbJournal : AsyncWriteJournal
    {
        private static readonly Type PersistentRepresentationType = typeof(IPersistentRepresentation);
        private readonly Serializer _serializer;

        private readonly IMongoCollection<JournalEntry> _collection;

        public MongoDbJournal()
        {
            var mongoDbExtension = MongoDbPersistence.Instance.Apply(Context.System);

            _collection = mongoDbExtension.JournalCollection;
            _serializer = Context.System.Serialization.FindSerializerForType(PersistentRepresentationType);
        }

        public override Task ReplayMessagesAsync(string persistenceId, long fromSequenceNr, long toSequenceNr, long max, Action<IPersistentRepresentation> replayCallback)
        {
            // Limit(0) doesn't work...
            if (max == 0)
                return Task.Run(() => {});

            // Limit allows only integer
            var maxValue = max >= int.MaxValue ? int.MaxValue : (int)max;
            var sender = Context.Sender;
            var builder = Builders<JournalEntry>.Filter;
            var filter = builder.Eq(x => x.PersistenceId, persistenceId);

            if (fromSequenceNr > 0)
                filter &= builder.Gte(x => x.SequenceNr, fromSequenceNr);

            if (toSequenceNr != long.MaxValue)
                filter &= builder.Lte(x => x.SequenceNr, toSequenceNr);

            return
                _collection
                    .Find(filter)<commit_msg>Added ascending sort of sequenceNr for the replay messages because the index is descending.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Persistence.Journal;
using Akka.Serialization;
using MongoDB.Driver;

namespace Akka.Persistence.MongoDb.Journal
{
    /// <summary>
    /// An Akka.NET journal implementation that writes events asynchronously to MongoDB.
    /// </summary>
    public class MongoDbJournal : AsyncWriteJournal
    {
        private static readonly Type PersistentRepresentationType = typeof(IPersistentRepresentation);
        private readonly Serializer _serializer;

        private readonly IMongoCollection<JournalEntry> _collection;

        public MongoDbJournal()
        {
            var mongoDbExtension = MongoDbPersistence.Instance.Apply(Context.System);

            _collection = mongoDbExtension.JournalCollection;
            _serializer = Context.System.Serialization.FindSerializerForType(PersistentRepresentationType);
        }

        public override Task ReplayMessagesAsync(string persistenceId, long fromSequenceNr, long toSequenceNr, long max, Action<IPersistentRepresentation> replayCallback)
        {
            // Limit(0) doesn't work...
            if (max == 0)
                return Task.Run(() => {});

            // Limit allows only integer
            var maxValue = max >= int.MaxValue ? int.MaxValue : (int)max;
            var sender = Context.Sender;
            var builder = Builders<JournalEntry>.Filter;
            var filter = builder.Eq(x => x.PersistenceId, persistenceId);
            var sort = Builders<JournalEntry>.Sort.Ascending(x => x.SequenceNr);

            if (fromSequenceNr > 0)
                filter &= builder.Gte(x => x.SequenceNr, fromSequenceNr);

            if (toSequenceNr != long.MaxValue)
                filter &= builder.Lte(x => x.SequenceNr, toSequenceNr);

            return
                _collection
                    .Find(filter)
                    .Sort(sort)
{'fraction_non_alphanumeric': 0.0869896018260208, 'fraction_numerical': 0.001521683996956632, 'mean_word_length': 2.515151515151515, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 27, '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': '2491701', 'n_tokens_mistral': 1040, 'n_tokens_neox': 987, 'n_words': 249}