private static async Task <List <Guid> > GetOldGetMsgIds(GetOldMessagesInput input,
                                                                 IReliableDictionary2 <Guid, List <Guid> > indexDict, ITransaction tx)
        {
            if (await indexDict.ContainsKeyAsync(tx, input.Id))
            {
                var lstMessageIndex = (await indexDict.TryGetValueAsync(tx, input.Id)).Value;
                var oldestMsgIndex  = lstMessageIndex.IndexOf(input.OldestMsgId);
                if (oldestMsgIndex == 0)
                {
                    return(null);
                }

                var endIndex   = oldestMsgIndex - 1;
                var startIndex = oldestMsgIndex - MaxMessageAmount;
                var amount     = MaxMessageAmount;
                if (startIndex < 0)
                {
                    startIndex = 0;
                    amount     = endIndex - startIndex + 1;
                }

                return(lstMessageIndex.GetRange(startIndex, amount));
            }

            return(null);
        }
        public async Task <List <ConversationMsg> > GetOldMessagesAsync(GetOldMessagesInput input)
        {
            if (input.Id == Guid.Empty)
            {
                throw new ArgumentException("ConversationId is empty", nameof(input.Id));
            }
            if (input.OldestMsgId == Guid.Empty)
            {
                throw new ArgumentException("OldestMsgId is empty", nameof(input.OldestMsgId));
            }

            _logger.Debug("GetOldMessagesAsync, conversationId is {id}", input.Id);

            var result    = new List <ConversationMsg>();
            var token     = CancellationToken.None;
            var indexDict = await Service.GetMessageIndexDictByIdAsync(StateManager, input.Id);

            var listDicts = await Service.GetAllMessageListDictsAsync(StateManager);

            using (var tx = StateManager.CreateTransaction())
            {
                var willOldGetMsgIds = await GetOldGetMsgIds(input, indexDict, tx);

                if (willOldGetMsgIds == null)
                {
                    return(new List <ConversationMsg>());
                }

                foreach (var listDict in listDicts)
                {
                    result.AddRange(await listDict.GetAllByIdsAsync(tx, willOldGetMsgIds, token));
                }
            }

            _logger.Debug("GetOldMessagesAsync, msg count is {count}", result.Count);
            return(result);
        }