Skip to content

Commit

Permalink
feat: implement IBlockRepositoryApi
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed May 30, 2019
1 parent 7afa69d commit e384b79
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 29 deletions.
84 changes: 84 additions & 0 deletions IpfsCli/Commands/RepoCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Ipfs.Engine;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Threading.Tasks;

namespace Ipfs.Cli
{
[Command(Description = "Manage the IPFS repository")]
[Subcommand("gc", typeof(RepoGCCommand))]
[Subcommand("stat", typeof(RepoStatCommand))]
[Subcommand("verify", typeof(RepoVerifyCommand))]
[Subcommand("version", typeof(RepoVersionCommand))]
class RepoCommand : CommandBase
{
public Program Parent { get; set; }

protected override Task<int> OnExecute(CommandLineApplication app)
{
app.ShowHelp();
return Task.FromResult(0);
}
}

[Command(Description = "Perform a garbage collection sweep on the repo")]
class RepoGCCommand : CommandBase
{
RepoCommand Parent { get; set; }

protected override async Task<int> OnExecute(CommandLineApplication app)
{
var Program = Parent.Parent;

await Program.CoreApi.BlockRepository.RemoveGarbageAsync();
return 0;
}
}

[Command(Description = "Verify all blocks in repo are not corrupted")]
class RepoVerifyCommand : CommandBase
{
RepoCommand Parent { get; set; }

protected override async Task<int> OnExecute(CommandLineApplication app)
{
var Program = Parent.Parent;

await Program.CoreApi.BlockRepository.VerifyAsync();
return 0;
}
}

[Command(Description = "Repository information")]
class RepoStatCommand : CommandBase
{
RepoCommand Parent { get; set; }

protected override async Task<int> OnExecute(CommandLineApplication app)
{
var Program = Parent.Parent;

var stats = await Program.CoreApi.BlockRepository.StatisticsAsync();
return Program.Output(app, stats, null);
}
}

[Command(Description = "Repository version")]
class RepoVersionCommand : CommandBase
{
RepoCommand Parent { get; set; }

protected override async Task<int> OnExecute(CommandLineApplication app)
{
var Program = Parent.Parent;

var stats = await Program.CoreApi.BlockRepository.VersionAsync();
return Program.Output(app, stats, null);
}
}


}
2 changes: 1 addition & 1 deletion IpfsCli/IpfsCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Common.Logging" Version="3.4.1" />
<PackageReference Include="Ipfs.Http.Client" Version="0.28.0" />
<PackageReference Include="Ipfs.Http.Client" Version="0.29.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.9.0" />
Expand Down
1 change: 1 addition & 0 deletions IpfsCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace Ipfs.Cli
[Subcommand("update", typeof(UpdateCommand))]
[Subcommand("bitswap", typeof(BitswapCommand))]
[Subcommand("stats", typeof(StatsCommand))]
[Subcommand("repo", typeof(RepoCommand))]
class Program : CommandBase
{
static bool debugging;
Expand Down
72 changes: 72 additions & 0 deletions IpfsServer/HttpApi/V0/BlockRepositoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Ipfs.CoreApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.IO;

namespace Ipfs.Server.HttpApi.V0
{
/// <summary>
/// A wrapped version number.
/// </summary>
public class VersionBlockRepositoryDto
{
/// <summary>
/// The version number.
/// </summary>
public string Version;
}

/// <summary>
/// Manages all the blocks in teh repository.
/// </summary>
public class BlockRepositoryController : IpfsController
{
/// <summary>
/// Creates a new controller.
/// </summary>
public BlockRepositoryController(ICoreApi ipfs) : base(ipfs) { }

/// <summary>
/// Garbage collection.
/// </summary>
[HttpGet, HttpPost, Route("repo/gc")]
public Task GarbageCollection()
{
return IpfsCore.BlockRepository.RemoveGarbageAsync(Cancel);
}

/// <summary>
/// Get repository information.
/// </summary>
[HttpGet, HttpPost, Route("repo/stat")]
public Task<RepositoryData> Statistics()
{
return IpfsCore.BlockRepository.StatisticsAsync(Cancel);
}

/// <summary>
/// Verify that the blocks are not corrupt.
/// </summary>
[HttpGet, HttpPost, Route("repo/verify")]
public Task Verify()
{
return IpfsCore.BlockRepository.VerifyAsync(Cancel);
}

/// <summary>
/// Get repository information.
/// </summary>
[HttpGet, HttpPost, Route("repo/version")]
public async Task<VersionBlockRepositoryDto> Version()
{
return new VersionBlockRepositoryDto
{
Version = await IpfsCore.BlockRepository.VersionAsync(Cancel)
};
}
}
}
2 changes: 1 addition & 1 deletion IpfsServer/IpfsServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ipfs.Core" Version="0.50.1" />
<PackageReference Include="Ipfs.Core" Version="0.51.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
Expand Down
1 change: 1 addition & 0 deletions doc/articles/core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Each IPFS feature has it's own interface.
| ------- | ------- |
| [Bitswap](xref:Ipfs.CoreApi.IBitswapApi) | Block trading between peers |
| [Block](xref:Ipfs.CoreApi.IBlockApi) | Manages the blocks |
| [BlockRepository](xref:Ipfs.CoreApi.IBlockRepositoryApi) | Manages the repository for [blocks](xref:Ipfs.CoreApi.IBlockApi) |
| [Bootstrap](xref:Ipfs.CoreApi.IBootstrapApi) | Trusted peers |
| [Config](xref:Ipfs.CoreApi.IConfigApi) | Manages the configuration of the local peer |
| [Dag](xref:Ipfs.CoreApi.IDagApi) | Manages the IPLD (linked data) Directed Acrylic Graph |
Expand Down
69 changes: 69 additions & 0 deletions src/CoreApi/BlockRepositoryApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Ipfs.CoreApi;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Collections.Concurrent;
using PeerTalk;

namespace Ipfs.Engine.CoreApi
{
class BlockRepositoryApi : IBlockRepositoryApi
{
IpfsEngine ipfs;

public BlockRepositoryApi(IpfsEngine ipfs)
{
this.ipfs = ipfs;
}

public Task RemoveGarbageAsync(CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
}

public Task<RepositoryData> StatisticsAsync(CancellationToken cancel = default(CancellationToken))
{
var data = new RepositoryData
{
RepoPath = Path.GetFullPath(ipfs.Options.Repository.Folder),
Version = "1",
StorageMax = 10000000000 // TODO: there is no storage max
};

GetDirStats(data.RepoPath, data, cancel);

return Task.FromResult(data);
}

public Task VerifyAsync(CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
}

public async Task<string> VersionAsync(CancellationToken cancel = default(CancellationToken))
{
var stats = await StatisticsAsync(cancel).ConfigureAwait(false);
return stats.Version;
}

void GetDirStats(string path, RepositoryData data, CancellationToken cancel)
{
foreach (var file in Directory.EnumerateFiles(path))
{
cancel.ThrowIfCancellationRequested();
++data.NumObjects;
data.RepoSize += (ulong)(new FileInfo(file).Length);
}

foreach (var dir in Directory.EnumerateDirectories(path))
{
cancel.ThrowIfCancellationRequested();
GetDirStats(dir, data, cancel);
}
}
}
}
27 changes: 1 addition & 26 deletions src/CoreApi/StatsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,7 @@ public StatsApi(IpfsEngine ipfs)

public Task<RepositoryData> RepositoryAsync(CancellationToken cancel = default(CancellationToken))
{
var data = new RepositoryData
{
RepoPath = Path.GetFullPath(ipfs.Options.Repository.Folder),
Version = "1",
StorageMax = 0 // TODO: there is no storage max
};

GetDirStats(data.RepoPath, data, cancel);

return Task.FromResult(data);
}

void GetDirStats(string path, RepositoryData data, CancellationToken cancel)
{
foreach (var file in Directory.EnumerateFiles(path))
{
cancel.ThrowIfCancellationRequested();
++data.NumObjects;
data.RepoSize += (ulong)(new FileInfo(file).Length);
}

foreach (var dir in Directory.EnumerateDirectories(path))
{
cancel.ThrowIfCancellationRequested();
GetDirStats(dir, data, cancel);
}
return ipfs.BlockRepository.StatisticsAsync(cancel);
}
}
}
4 changes: 4 additions & 0 deletions src/IpfsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void Init()
// Init the core api inteface.
Bitswap = new BitswapApi(this);
Block = new BlockApi(this);
BlockRepository = new BlockRepositoryApi(this);
Bootstrap = new BootstrapApi(this);
Config = new ConfigApi(this);
Dag = new DagApi(this);
Expand Down Expand Up @@ -215,6 +216,9 @@ void Init()
/// <inheritdoc />
public IBlockApi Block { get; set; }

/// <inheritdoc />
public IBlockRepositoryApi BlockRepository { get; set; }

/// <inheritdoc />
public IBootstrapApi Bootstrap { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/IpfsEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ipfs.Core" Version="0.50.1" />
<PackageReference Include="Ipfs.Core" Version="0.51.1" />
<PackageReference Include="Makaretu.Dns.Unicast" Version="0.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.0.0" />
Expand Down
32 changes: 32 additions & 0 deletions test/CoreApi/BlockRepositoryApiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ipfs.Engine
{

[TestClass]
public class BlockRepositoryApiTest
{
IpfsEngine ipfs = TestFixture.Ipfs;

[TestMethod]
public void Exists()
{
Assert.IsNotNull(ipfs.BlockRepository);
}

[TestMethod]
public async Task Stats()
{
var stats = await ipfs.BlockRepository.StatisticsAsync();
var version = await ipfs.BlockRepository.VersionAsync();
Assert.AreEqual(stats.Version, version);
}

}
}

0 comments on commit e384b79

Please sign in to comment.