/// <summary>
/// Extract the masterstream to mimic the folder structure the AzureAsyncAppender
/// </summary>
/// <returns></returns>
public async Task ExtractMasterStream(DirectoryInfo output, IAppenderNamingPolicy namingPolicy, long afterVersion, int maxCount, bool writeIndexFile = true)
{
var events = await this.ReadRecords(afterVersion, maxCount);
var versions = new Dictionary <string, int>();
var rootindex = new FileInfo(Path.Combine(output.FullName, namingPolicy.GetIndexPath("master")));
if (!output.Exists)
{
output.Create();
}
using (var masterindexwriter = new StreamWriter(System.IO.File.OpenWrite(rootindex.FullName)))
{
foreach (var @event in events)
{
var streampath = namingPolicy.GetStreamPath(@event.Name);
var filename = new FileInfo(Path.Combine(output.FullName, streampath));
if (!versions.ContainsKey(@event.Name))
{
versions[@event.Name] = 1;
}
else
{
versions[@event.Name]++;
}
var record = new FileRecord(@event.Data, @event.Name, versions[@event.Name]);
if (!filename.Directory.Exists)
{
filename.Directory.Create();
}
using (var fs = System.IO.File.OpenWrite(filename.FullName))
{
record.WriteContentToStream(fs);
}
if (writeIndexFile)
{
var indexfile = Path.Combine(output.FullName, namingPolicy.GetIndexPath(@event.Name));
var hash = string.Join("", record.Hash.Select(x => x.ToString("x2")));
FileStream fileStream = System.IO.File.Exists(indexfile) ? System.IO.File.Open(indexfile, FileMode.Append, FileAccess.Write) : System.IO.File.Create(indexfile);
using (var streamindex = new StreamWriter(fileStream))
{
streamindex.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath));
masterindexwriter.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath));
streamindex.Flush();
}
}
}
masterindexwriter.Flush();
}
}