SerializedBatchWriter
using Relativity.Transfer.Resources;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.Transfer
{
public class SerializedBatchWriter
{
private readonly ITransferPathReader reader;
private readonly ITransferLog transferLog;
public SerializedBatchWriter(ITransferPathReader reader, ITransferLog log)
{
if (reader == null)
throw new ArgumentNullException("reader");
if (log == null)
throw new ArgumentNullException("log");
this.reader = reader;
transferLog = log;
}
public Task<SerializedPathsResult> WriteAsync(string batchDirectory, bool localPaths, CancellationToken token)
{
return Task.Run(delegate {
PathEnumeratorContext pathEnumeratorContext = new PathEnumeratorContext();
PathSearchStorage pathSearchStorage = new PathSearchStorage(pathEnumeratorContext, new JsonFileSerializer(), transferLog, token) {
BatchDirectory = batchDirectory,
LocalPaths = localPaths
};
while (reader.Read()) {
token.ThrowIfCancellationRequested();
TransferPathRecord record = reader.GetRecord();
if (record != null) {
if (string.IsNullOrEmpty(record.SourcePath))
throw new TransferException(CoreStrings.TransferPathReaderSourcePathExceptionMessage, true);
pathSearchStorage.Add(new TransferPath {
Bytes = record.TotalBytes,
SourcePath = record.SourcePath,
SourcePathId = record.SourcePathId,
Tag = record.Tag,
TargetPath = record.TargetPath,
TargetFileName = record.TargetFileName,
PathAttributes = TransferPathAttributes.File
});
}
}
pathSearchStorage.Save(true);
return new SerializedPathsResult(pathEnumeratorContext.SearchPaths, pathSearchStorage.SerializedPaths, pathSearchStorage.PathErrors, true, DateTime.Now - pathSearchStorage.StartTime);
}, token);
}
}
}