static void CacheResults(ClientSearchRequest request, IEnumerable <SearchItem> items)
 {
     foreach (var searchItem in items)
     {
         request.temporaryCache[searchItem.id] = searchItem;
     }
 }
        static string SearchItemsToJson(IEnumerable <SearchItem> items, ClientSearchRequest request, SearchRequestType requestType, FetchOptions fetchOptions)
        {
            var jsonData = new Dictionary <string, object>
            {
                ["requestId"]   = request.requestId,
                ["items"]       = items.Select(i => SearchItemToDictionary(i, request, fetchOptions)).ToArray(),
                ["requestType"] = (int)requestType
            };

            return(Utils.JsonSerialize(jsonData));
        }
        static Dictionary <string, object> SearchItemToDictionary(SearchItem item, ClientSearchRequest request, FetchOptions fetchOptions)
        {
            var data = new Dictionary <string, object>
            {
                ["id"]               = item.id,
                ["score"]            = item.score,
                ["providerPriority"] = item.provider.priority
            };

            FetchAdditionalItemInfo(item, data, fetchOptions, request.context);

            return(data);
        }
        static void HandleQueryRequests(int connectionId, Dictionary <string, object> queryData)
        {
            var requestId    = (long)queryData["requestId"];
            var query        = queryData["query"] as string;
            var fetchOptions = (FetchOptions)Enum.Parse(typeof(FetchOptions), queryData["fetchOptions"].ToString());
            var maxCount     = (long)queryData["maxCount"];

            if (!s_SearchRequestsById.TryGetValue(connectionId, out var request))
            {
                request = new ClientSearchRequest
                {
                    // Create a new search context
                    context = new SearchContext(SearchService.Providers.Where(p => p.active), "")
                };
                request.context.asyncItemReceived += (context, items) =>
                {
                    var itemsList = items.ToList();
                    if (request.maxCount > -1)
                    {
                        var remainingItems = request.maxCount - request.currentCount;
                        if (remainingItems < 1)
                        {
                            return;
                        }
                        itemsList = itemsList.Take(remainingItems).ToList();
                    }
                    request.currentCount += itemsList.Count;
                    CacheResults(request, itemsList);
                    SendResults(connectionId, request, SearchRequestType.Query, fetchOptions, itemsList);
                };
                s_SearchRequestsById.Add(connectionId, request);
            }

            request.context.sessions.StopAllAsyncSearchSessions();
            request.context.searchText = query;
            request.requestId          = requestId;
            request.maxCount           = (int)maxCount;
            var initialResults = SearchService.GetItems(request.context);

            if (request.maxCount > -1)
            {
                initialResults = initialResults.Take(request.maxCount).ToList();
            }
            request.currentCount   = initialResults.Count;
            request.temporaryCache = new Dictionary <string, SearchItem>();
            CacheResults(request, initialResults);
            SendResults(connectionId, request, SearchRequestType.Query, fetchOptions, initialResults);
        }
        static void SendThumbnailResults(int connectionId, ClientSearchRequest request, IEnumerable <SearchItem> items, ThumbnailOptions options)
        {
            var allItemsData = new List <byte>();

            AppendInt(allItemsData, (int)request.requestId);
            AppendInt(allItemsData, (int)options.compression);
            foreach (var searchItem in items)
            {
                var thumbnail = FetchThumbnail(searchItem, options, request.context);
                if (thumbnail == null)
                {
                    continue;
                }
                EncodeItemThumbnail(searchItem, thumbnail, options, allItemsData);
            }

            ChannelService.Send(connectionId, allItemsData.ToArray());
        }
        static void SendResults(int connectionId, ClientSearchRequest request, SearchRequestType requestType, FetchOptions fetchOptions, IEnumerable <SearchItem> items)
        {
            var json = SearchItemsToJson(items, request, requestType, fetchOptions);

            ChannelService.Send(connectionId, json);
        }