public HttpResponseMessage Get(string query, [FromUri] string[] sort) { int results; var keys = Search.Query(query, sort, Paging.Start, Paging.PageSize, out results); var list = new List<FileHeader>(); Storage.Batch(accessor => list.AddRange(keys.Select(accessor.ReadFile).Where(x => x != null))); var result = new SearchResults { Start = Paging.Start, PageSize = Paging.PageSize, Files = list, FileCount = results }; return this.GetMessageWithObject(result); }
private SearchResults GetSmallest(SearchResults[] searchResults, int[] indexes, int[] originalIndexes, string[] sortFields) { FileHeader smallest = null; var smallestIndex = -1; for (var i = 0; i < searchResults.Length; i++) { var pos = indexes[i] - originalIndexes[i]; if (pos >= searchResults[i].FileCount) continue; var current = searchResults[i].Files[pos]; if (smallest != null && CompareFileInfos(current, smallest, sortFields) >= 0) continue; smallest = current; smallestIndex = i; } if (smallestIndex != -1) indexes[smallestIndex]++; return new SearchResults { FileCount = 1, Files = new List<FileHeader> { smallest } }; }
public async Task<SearchResults> SearchAsync(string query, string[] sortFields = null, int pageSize = 25, ShardPagingInfo pagingInfo = null) { if (pagingInfo == null) pagingInfo = new ShardPagingInfo(Clients.Count); var indexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage); if (indexes == null) { var lastPage = pagingInfo.GetLastPageNumber(); if (pagingInfo.CurrentPage - lastPage > 10) throw new InvalidOperationException("Not Enough info in order to calculate requested page in a timely fation, last page info is for page #" + lastPage + ", please go to a closer page"); var originalPage = pagingInfo.CurrentPage; pagingInfo.CurrentPage = lastPage; while (pagingInfo.CurrentPage < originalPage) { await SearchAsync(query, sortFields, pageSize, pagingInfo).ConfigureAwait(false); pagingInfo.CurrentPage++; } indexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage); } var result = new SearchResults(); var applyAsync = await Strategy.ShardAccessStrategy.ApplyAsync( Clients.Values.ToList(), new ShardRequestData(), (client, i) => client.SearchAsync(query, sortFields, indexes[i], pageSize)) .ConfigureAwait(false); var originalIndexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage); while (result.FileCount < pageSize) { var item = GetSmallest(applyAsync, indexes, originalIndexes, sortFields); if (item == null) break; var files = new List<FileHeader>(); if (result.Files != null) files.AddRange(result.Files); if (item.Files != null) files.AddRange(item.Files); result.FileCount++; result.Files = files; result.PageSize = pageSize; result.Start = 0; //TODO: update start } pagingInfo.SetPagingInfo(indexes); result.Files = result.Files.Where(info => info != null).ToList(); result.FileCount = result.Files.Count; return result; }