Ejemplo n.º 1
0
        /// <summary>
        /// ライブラリに検索を要求、結果をキャッシュ
        /// </summary>
        /// <returns></returns>
        private async Task <Record[]> RequestSearchAsync
            (ISearchCriteria criteria, long offset, long takes)
        {
            if (criteria == null || takes <= 0)
            {
                throw new ArgumentException();
            }

            Record skipUntil = null;

            /*
             * if (offset > 0)
             * {
             *  lock (this.Cache)
             *  {
             *      if (!this.Cache.TryGetValue(offset - 1, out skipUntil))
             *      {
             *          skipUntil = null;
             *      }
             *  }
             * }*/

            var result = await criteria.SearchAsync(library, offset, takes, skipUntil);

            lock (this.Cache)
            {
                for (int i = 0; i < takes; i++)
                {
                    if (i < result.Length)
                    {
                        this.Cache[offset + i] = result[i];
                    }
                    else
                    {
                        this.Cache[offset + i] = dummyRecord;
                    }
                }
            }

            if (result.Length > 0)
            {
                NotifyCollectionChangedEventArgs eventArgs;

                if (offset + result.Length > this.Length.Value)
                {
                    eventArgs = new NotifyCollectionChangedEventArgs
                                    (NotifyCollectionChangedAction.Add, result.ToList());
                }
                else
                {
                    eventArgs = new NotifyCollectionChangedEventArgs
                                    (NotifyCollectionChangedAction.Replace, result.ToList(), new List <Record>());
                }
                this.CollectionChanged?.Invoke(this, eventArgs);

                if (this.CacheUpdatedSubject.HasObservers)
                {
                    this.CacheUpdatedSubject.OnNext(new CacheUpdatedEventArgs()
                    {
                        Start  = offset,
                        Length = result.Length,
                    });
                }
            }

            return(result);
        }