Skip to content

Commit

Permalink
feat(AddDirectory): improve performance
Browse files Browse the repository at this point in the history
Cache the empty directory.
  • Loading branch information
richardschneider committed Jan 14, 2017
1 parent 75a1e83 commit 368ff50
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Ipfs.Api
{
public partial class IpfsClient
{

/// <summary>
/// Add a file to the interplanetary file system.
/// </summary>
Expand All @@ -30,8 +31,8 @@ public Task<MerkleNode> AddFileAsync(string path)
/// <param name="recursive"></param>
public async Task<MerkleNode> AddDirectoryAsync(string path, bool recursive = true)
{
// Add the files and sub-directories.
path = Path.GetFullPath(path);
var folder = await Object.NewDirectoryAsync();
var files = Directory
.EnumerateFiles(path)
.Select(AddFileAsync);
Expand All @@ -43,12 +44,12 @@ public async Task<MerkleNode> AddDirectoryAsync(string path, bool recursive = tr
files = files.Union(folders);
}
var nodes = await Task.WhenAll(files);
var links = nodes.Select(node => node.ToLink());

// TODO: Use DagNode.AddLinks
folder = new DagNode(folder.Data, links);

// Create the directory with links to the created files and sub-directories
var links = nodes.Select(node => node.ToLink());
var folder = emptyFolder.Value.AddLinks(links);
var directory = await Object.PutAsync(folder);

return new MerkleNode(directory.Hash, Path.GetFileName(path));
}

Expand Down
4 changes: 2 additions & 2 deletions src/IpfsApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<HintPath>..\packages\Google.Protobuf.3.1.0\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ipfs.Core, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Ipfs.Core.0.5.1\lib\net45\Ipfs.Core.dll</HintPath>
<Reference Include="Ipfs.Core, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Ipfs.Core.0.6.0\lib\net45\Ipfs.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand Down
4 changes: 4 additions & 0 deletions src/IpfsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public partial class IpfsClient
/// </summary>
public static Uri DefaultApiUri = new Uri("http://localhost:5001");

Lazy<DagNode> emptyFolder;

/// <summary>
/// Creates a new instance of the <see cref="IpfsClient"/> class and sets the
/// default values.
Expand All @@ -58,6 +60,8 @@ public IpfsClient()
Swarm = new SwarmApi(this);
Dag = new DagApi(this);
Object = new ObjectApi(this);

emptyFolder = new Lazy<DagNode>(() => Object.NewDirectoryAsync().Result);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Common.Logging" version="3.3.1" targetFramework="net45" />
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net45" />
<package id="Google.Protobuf" version="3.1.0" targetFramework="net45" />
<package id="Ipfs.Core" version="0.5.1" targetFramework="net452" />
<package id="Ipfs.Core" version="0.6.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="SHA3" version="0.9.2" targetFramework="net452" />
</packages>
4 changes: 2 additions & 2 deletions test/IpfsApiTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<HintPath>..\packages\Google.Protobuf.3.1.0\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ipfs.Core, Version=0.5.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Ipfs.Core.0.5.1\lib\net45\Ipfs.Core.dll</HintPath>
<Reference Include="Ipfs.Core, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Ipfs.Core.0.6.0\lib\net45\Ipfs.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
Expand Down
2 changes: 1 addition & 1 deletion test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Common.Logging" version="3.3.1" targetFramework="net45" />
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net45" />
<package id="Google.Protobuf" version="3.1.0" targetFramework="net45" />
<package id="Ipfs.Core" version="0.5.1" targetFramework="net452" />
<package id="Ipfs.Core" version="0.6.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="SHA3" version="0.9.2" targetFramework="net452" />
</packages>

0 comments on commit 368ff50

Please sign in to comment.