Skip to content

Commit 368ff50

Browse files
feat(AddDirectory): improve performance
Cache the empty directory.
1 parent 75a1e83 commit 368ff50

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

src/Add.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Ipfs.Api
1212
{
1313
public partial class IpfsClient
1414
{
15+
1516
/// <summary>
1617
/// Add a file to the interplanetary file system.
1718
/// </summary>
@@ -30,8 +31,8 @@ public Task<MerkleNode> AddFileAsync(string path)
3031
/// <param name="recursive"></param>
3132
public async Task<MerkleNode> AddDirectoryAsync(string path, bool recursive = true)
3233
{
34+
// Add the files and sub-directories.
3335
path = Path.GetFullPath(path);
34-
var folder = await Object.NewDirectoryAsync();
3536
var files = Directory
3637
.EnumerateFiles(path)
3738
.Select(AddFileAsync);
@@ -43,12 +44,12 @@ public async Task<MerkleNode> AddDirectoryAsync(string path, bool recursive = tr
4344
files = files.Union(folders);
4445
}
4546
var nodes = await Task.WhenAll(files);
46-
var links = nodes.Select(node => node.ToLink());
47-
48-
// TODO: Use DagNode.AddLinks
49-
folder = new DagNode(folder.Data, links);
5047

48+
// Create the directory with links to the created files and sub-directories
49+
var links = nodes.Select(node => node.ToLink());
50+
var folder = emptyFolder.Value.AddLinks(links);
5151
var directory = await Object.PutAsync(folder);
52+
5253
return new MerkleNode(directory.Hash, Path.GetFileName(path));
5354
}
5455

src/IpfsApi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
<HintPath>..\packages\Google.Protobuf.3.1.0\lib\net45\Google.Protobuf.dll</HintPath>
5959
<Private>True</Private>
6060
</Reference>
61-
<Reference Include="Ipfs.Core, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL">
62-
<HintPath>..\packages\Ipfs.Core.0.5.1\lib\net45\Ipfs.Core.dll</HintPath>
61+
<Reference Include="Ipfs.Core, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
62+
<HintPath>..\packages\Ipfs.Core.0.6.0\lib\net45\Ipfs.Core.dll</HintPath>
6363
<Private>True</Private>
6464
</Reference>
6565
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

src/IpfsClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public partial class IpfsClient
3636
/// </summary>
3737
public static Uri DefaultApiUri = new Uri("http://localhost:5001");
3838

39+
Lazy<DagNode> emptyFolder;
40+
3941
/// <summary>
4042
/// Creates a new instance of the <see cref="IpfsClient"/> class and sets the
4143
/// default values.
@@ -58,6 +60,8 @@ public IpfsClient()
5860
Swarm = new SwarmApi(this);
5961
Dag = new DagApi(this);
6062
Object = new ObjectApi(this);
63+
64+
emptyFolder = new Lazy<DagNode>(() => Object.NewDirectoryAsync().Result);
6165
}
6266

6367
/// <summary>

src/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="Common.Logging" version="3.3.1" targetFramework="net45" />
66
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net45" />
77
<package id="Google.Protobuf" version="3.1.0" targetFramework="net45" />
8-
<package id="Ipfs.Core" version="0.5.1" targetFramework="net452" />
8+
<package id="Ipfs.Core" version="0.6.0" targetFramework="net452" />
99
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
1010
<package id="SHA3" version="0.9.2" targetFramework="net452" />
1111
</packages>

test/IpfsApiTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
<HintPath>..\packages\Google.Protobuf.3.1.0\lib\net45\Google.Protobuf.dll</HintPath>
5757
<Private>True</Private>
5858
</Reference>
59-
<Reference Include="Ipfs.Core, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL">
60-
<HintPath>..\packages\Ipfs.Core.0.5.1\lib\net45\Ipfs.Core.dll</HintPath>
59+
<Reference Include="Ipfs.Core, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
60+
<HintPath>..\packages\Ipfs.Core.0.6.0\lib\net45\Ipfs.Core.dll</HintPath>
6161
<Private>True</Private>
6262
</Reference>
6363
<Reference Include="Microsoft.CSharp" />

test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<package id="Common.Logging" version="3.3.1" targetFramework="net45" />
66
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net45" />
77
<package id="Google.Protobuf" version="3.1.0" targetFramework="net45" />
8-
<package id="Ipfs.Core" version="0.5.1" targetFramework="net452" />
8+
<package id="Ipfs.Core" version="0.6.0" targetFramework="net452" />
99
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
1010
<package id="SHA3" version="0.9.2" targetFramework="net452" />
1111
</packages>

0 commit comments

Comments
 (0)