예제 #1
0
	    public async Task CanBrowseWithSharding()
	    {
            var ms = new MemoryStream();
            var streamWriter = new StreamWriter(ms);
            var expected = new string('a', 1024);
            streamWriter.Write(expected);
            streamWriter.Flush();
            ms.Position = 0;

            await shardedClient.UploadAsync("a.txt", ms);
            await shardedClient.UploadAsync("b.txt", ms);
            await shardedClient.UploadAsync("c.txt", ms);
            await shardedClient.UploadAsync("d.txt", ms);
            await shardedClient.UploadAsync("e.txt", ms);

	        var pagingInfo = new PagingInfo(shardedClient.NumberOfShards);
	        var result = await shardedClient.BrowseAsync(2, pagingInfo);
            Assert.Equal(2, result.Length);

	        pagingInfo.CurrentPage++;
            result = await shardedClient.BrowseAsync(2, pagingInfo);
            Assert.Equal(2, result.Length);

            pagingInfo.CurrentPage++;
            result = await shardedClient.BrowseAsync(2, pagingInfo);
            Assert.Equal(1, result.Length);

            pagingInfo.CurrentPage++;
            result = await shardedClient.BrowseAsync(2, pagingInfo);
            Assert.Equal(0, result.Length);
	    }
		public Task<SearchResults> GetFilesAsync(string folder, FilesSortOptions options = FilesSortOptions.Default, string fileNameSearchPattern = "", int pageSize = 25, PagingInfo pagingInfo = null)
		{
			var folderQueryPart = GetFolderQueryPart(folder);

			if (string.IsNullOrEmpty(fileNameSearchPattern) == false && fileNameSearchPattern.Contains("*") == false &&
				fileNameSearchPattern.Contains("?") == false)
			{
				fileNameSearchPattern = fileNameSearchPattern + "*";
			}
			var fileNameQueryPart = GetFileNameQueryPart(fileNameSearchPattern);

			return SearchAsync(folderQueryPart + fileNameQueryPart, GetSortFields(options), pageSize, pagingInfo);
		}
		public async Task<string[]> GetFoldersAsync(string @from = null, int pageSize = 25, PagingInfo pagingInfo = null)
		{
			if (pagingInfo == null)
				pagingInfo = new PagingInfo(ShardClients.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 GetFoldersAsync(from, pageSize, pagingInfo);
					pagingInfo.CurrentPage++;
				}

				indexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage);
			}

			var results = new List<string>();

			var applyAsync =
			   await
			   ShardStrategy.ShardAccessStrategy.ApplyAsync(ShardClients.Values.ToList(), new ShardRequestData(),
															(client, i) => client.GetFoldersAsync(from, indexes[i], pageSize));

			var originalIndexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage);
			while (results.Count < pageSize)
			{
				var item = GetSmallest(applyAsync, indexes, originalIndexes);
				if (item == null)
					break;

				results.Add(item);
			}

			pagingInfo.SetPagingInfo(indexes);

			return results.ToArray();
		}
		public async Task<SearchResults> SearchAsync(string query, string[] sortFields = null, int pageSize = 25, PagingInfo pagingInfo = null)
		{
			if (pagingInfo == null)
				pagingInfo = new PagingInfo(ShardClients.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);
					pagingInfo.CurrentPage++;
				}

				indexes = pagingInfo.GetPagingInfo(pagingInfo.CurrentPage);
			}

			var result = new SearchResults();

			var applyAsync =
			   await
			   ShardStrategy.ShardAccessStrategy.ApplyAsync(ShardClients.Values.ToList(), new ShardRequestData(),
															(client, i) => client.SearchAsync(query, sortFields, indexes[i], pageSize));

			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<FileInfo>();
				if (result.Files != null)
					files.AddRange(result.Files);
				if (item.Files != null)
					files.AddRange(item.Files);

				result.FileCount++;
				result.Files = files.ToArray();
				result.PageSize = pageSize;
				result.Start = 0;//todo: update start
			}

			pagingInfo.SetPagingInfo(indexes);

			result.Files = result.Files.Where(info => info != null).ToArray();
			result.FileCount = result.Files.Length;
			return result;
		}
예제 #5
0
        public async Task CanNotBrowseToPageFarAway()
        {
            var ms = new MemoryStream();
            var streamWriter = new StreamWriter(ms);
            var expected = new string('a', 1024);
            streamWriter.Write(expected);
            streamWriter.Flush();
            ms.Position = 0;

            await shardedClient.UploadAsync("a.txt", ms);
            await shardedClient.UploadAsync("b.txt", ms);
            await shardedClient.UploadAsync("c.txt", ms);
            await shardedClient.UploadAsync("d.txt", ms);
            await shardedClient.UploadAsync("e.txt", ms);

            var pagingInfo = new PagingInfo(shardedClient.NumberOfShards) { CurrentPage = 20 };
            try
            {
                await shardedClient.BrowseAsync(2, pagingInfo);
                Assert.Equal(true, false);//Should not get here
            }
            catch (Exception exception)
            {
                Assert.IsType<InvalidOperationException>(exception);
            }
        }