Skip to content

Commit 9731867

Browse files
committed
Merge changes from develop before release 2.0.0
1 parent 4b1220e commit 9731867

File tree

13 files changed

+156
-245
lines changed

13 files changed

+156
-245
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
# ParallelTreeWalker
1+
# Skraalsoft Parallel TreeWalker
22
An extensible and highly scalable **.Net component** for processing elements in a **tree** in an **async** and **parallel** way.
33

4+
[![NuGet](https://img.shields.io/nuget/v/Skraalsoft.ParallelTreeWalker.svg)](https://www.nuget.org/packages/Skraalsoft.ParallelTreeWalker)
5+
46
* it is able to process **huge trees**, like a large directory structure in the file system.
57
* **parent folders** are always processed before children.
68
* **parallel**: you decide how many tree elements can be visited at the same time.
79
* **efficient**: consumed resources depend only on how many parallel operations you allow, not on the size of the tree.
810
* **async**: when a tree element is visited, you can perform an asynchronous operation (e.g. execute a database command or web request).
911

10-
[Latest release](https://github.com/tusmester/ParallelTreeWalker/releases/latest)
11-
1212
## Usage
1313
First implement the *ITreeElement* interface (relax, it is simple) to represent your tree element - or if you want to discover the file system, you can use the built-in *FileSystemElement* class.
1414

15-
Just call the *WalkAsync* method with a root element and it will do the rest. In the options parameter you can define a callback function (*ProcessElementAsync*) that will be executed every time a tree element is reached.
15+
Just call the *WalkAsync* method with a root element and it will do the rest. You can define a callback function that will be executed every time a tree element is reached.
1616

1717
```c#
18-
await TreeWalker.WalkAsync(root, new TreeWalkerOptions
18+
await TreeWalker<FileSystemElement>.WalkAsync(root, async (element) =>
19+
{
20+
var path = element.Path;
21+
var isDirectory = element.IsDirectory;
22+
23+
await DoStuffAsync(element);
24+
},
25+
new TreeWalkerOptions
1926
{
20-
MaxDegreeOfParallelism = 5,
21-
ProcessElementAsync = async (element) =>
22-
{
23-
var el = element as FileSystemElement;
24-
var path = el.Path;
25-
var isDirectory = el.IsDirectory;
26-
27-
await DoStuffAsync(el);
28-
}
27+
MaxDegreeOfParallelism = 10
2928
});
3029
```

img/icon-big.png

10.9 KB
Loading

img/icon.png

2.1 KB
Loading

img/icon.xlsx

17 KB
Binary file not shown.

src/ParallelTreeWalker.Tests/FileSystemTests.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using ParallelTreeWalker.Elements;
2+
using Skraalsoft.ParallelTreewalker.Elements;
33
using System;
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
88
using System.Linq;
9-
using System.Text;
109
using System.Threading.Tasks;
1110

12-
namespace ParallelTreeWalker.Tests
11+
namespace Skraalsoft.ParallelTreewalker.Tests
1312
{
1413
[TestClass]
1514
public class FileSystemTests
@@ -25,29 +24,28 @@ public async Task TwoLevels_Balanced()
2524
var actual = string.Join(Environment.NewLine, allVisitedPath);
2625

2726
// expected results: enumerate manually, add root dir and sort
28-
var expected = string.Join(Environment.NewLine,
29-
Enumerable.Union(new List<string> { rootDir }, Directory.EnumerateFileSystemEntries(rootDir, "*", SearchOption.AllDirectories)).OrderBy(p => p));
27+
var expected = string.Join(Environment.NewLine,
28+
new List<string> {rootDir}
29+
.Union(Directory.EnumerateFileSystemEntries(rootDir, "*", SearchOption.AllDirectories))
30+
.OrderBy(p => p));
3031

3132
Assert.AreEqual(expected, actual);
3233
}
3334

34-
public async Task<string[]> TestWalk(ITreeElement root, int maxParallel)
35+
public async Task<string[]> TestWalk(FileSystemElement root, int maxParallel)
3536
{
3637
var allPaths = new ConcurrentBag<string>();
3738

38-
await TreeWalker.WalkAsync(root, new TreeWalkerOptions
39+
await TreeWalker<FileSystemElement>.WalkAsync(root, el =>
3940
{
40-
MaxDegreeOfParallelism = maxParallel,
41-
ProcessElementAsync = (element) =>
42-
{
43-
var el = element as FileSystemElement;
44-
45-
Trace.WriteLine(string.Format("##PTW> {0} Path: {1}", DateTime.UtcNow.ToLongTimeString(), el.Path));
41+
Trace.WriteLine($"##PTW> {DateTime.UtcNow.ToLongTimeString()} Path: {el.Path}");
4642

47-
allPaths.Add(el.Path);
43+
allPaths.Add(el.Path);
4844

49-
return Task.FromResult<object>(null);
50-
}
45+
return Task.FromResult<object>(null);
46+
}, new TreeWalkerOptions
47+
{
48+
MaxDegreeOfParallelism = maxParallel
5149
});
5250

5351
return allPaths.OrderBy(p => p).ToArray();
@@ -62,14 +60,14 @@ private string CreateTestStructure()
6260
if (Directory.Exists(rootDir))
6361
Directory.Delete(rootDir, true);
6462

65-
var subDirs = new string[]
63+
var subDirs = new[]
6664
{
6765
Path.Combine(rootDir, @"F1\F11"),
6866
Path.Combine(rootDir, @"F1\F12"),
6967
Path.Combine(rootDir, @"F2\F21"),
7068
Path.Combine(rootDir, @"F2\F22"),
7169
Path.Combine(rootDir, @"F3\F31"),
72-
Path.Combine(rootDir, @"F3\F32"),
70+
Path.Combine(rootDir, @"F3\F32")
7371
};
7472

7573
foreach (var subDir in subDirs)

0 commit comments

Comments
 (0)