forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobPartPlanFile.cs
More file actions
99 lines (86 loc) · 3.2 KB
/
JobPartPlanFile.cs
File metadata and controls
99 lines (86 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Storage.Common;
namespace Azure.Storage.DataMovement.JobPlan
{
internal class JobPartPlanFile : IDisposable
{
/// <summary>
/// Save the associated file name within a struct. This will contain
/// our transfer id, job part id, verison etc.
/// </summary>
public JobPartPlanFileName FileName { get; set; }
/// <summary>
/// The associated file on disk. When the last process has finished working
/// with the file, the data is saved to the file on the disk.
/// </summary>
public string FilePath { get => FileName.ToString(); }
/// <summary>
/// Lock for the memory mapped file to allow only one writer.
/// </summary>
public readonly SemaphoreSlim WriteLock;
private const int DefaultBufferSize = 81920;
private JobPartPlanFile()
{
WriteLock = new SemaphoreSlim(1);
}
public static async Task<JobPartPlanFile> CreateJobPartPlanFileAsync(
string checkpointerPath,
string id,
int jobPart,
Stream headerStream,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(checkpointerPath, nameof(checkpointerPath));
Argument.AssertNotNullOrEmpty(id, nameof(id));
Argument.AssertNotNull(jobPart, nameof(jobPart));
Argument.AssertNotNull(headerStream, nameof(headerStream));
JobPartPlanFileName fileName = new JobPartPlanFileName(checkpointerPath: checkpointerPath, id: id, jobPartNumber: jobPart);
return await CreateJobPartPlanFileAsync(fileName, headerStream, cancellationToken).ConfigureAwait(false);
}
public static async Task<JobPartPlanFile> CreateJobPartPlanFileAsync(
JobPartPlanFileName fileName,
Stream headerStream,
CancellationToken cancellationToken = default)
{
JobPartPlanFile result = new JobPartPlanFile()
{
FileName = fileName
};
try
{
using (FileStream fileStream = File.Create(result.FileName.ToString()))
{
await headerStream.CopyToAsync(
fileStream,
DataMovementConstants.DefaultStreamCopyBufferSize,
cancellationToken).ConfigureAwait(false);
}
}
catch (Exception)
{
// will handle if file has not been created yet
File.Delete(result.FileName.ToString());
throw;
}
return result;
}
public static JobPartPlanFile CreateExistingPartPlanFile(
JobPartPlanFileName fileName)
{
return new JobPartPlanFile()
{
FileName = fileName
};
}
public void Dispose()
{
WriteLock.Dispose();
}
}
}