-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathFileOfBlocks.cs
More file actions
222 lines (190 loc) · 5.9 KB
/
FileOfBlocks.cs
File metadata and controls
222 lines (190 loc) · 5.9 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.IO;
using System.Threading.Tasks;
using Waher.Runtime.IO;
using Waher.Runtime.Threading;
namespace Waher.Persistence.Files
{
/// <summary>
/// File of sequential blocks.
/// </summary>
public class FileOfBlocks : IDisposable
{
private readonly MultiReadSingleWriteObject fileAccess;
private readonly FileStream file;
private readonly string collectionName;
private readonly string fileName;
private readonly bool filePreExisting;
private readonly int blockSize;
private readonly bool asyncFileIo;
private long length;
private uint blockLimit;
/// <summary>
/// File of sequential blocks.
/// </summary>
/// <param name="CollectionName">Collection Name</param>
/// <param name="FileName">File name. If the file or folder does not exist
/// prior to the creation of the object, they are created accordingly.</param>
/// <param name="BlockSize">Block Size</param>
public FileOfBlocks(string CollectionName, string FileName, int BlockSize)
{
CheckBlockSize(BlockSize);
this.collectionName = CollectionName;
this.fileName = FileName;
this.filePreExisting = File.Exists(this.fileName);
this.blockSize = BlockSize;
this.fileAccess = new MultiReadSingleWriteObject(this, false);
this.asyncFileIo = FilesProvider.AsyncFileIo;
string Folder = Path.GetDirectoryName(FileName);
if (!string.IsNullOrEmpty(Folder) && !Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
if (this.filePreExisting)
this.file = File.Open(this.fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
else
this.file = File.Open(this.fileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None);
this.length = this.file.Length;
this.blockLimit = (uint)(this.length / this.blockSize);
}
/// <summary>
/// If the file existed before the construction of the object (true),
/// or if the constructor created the file.
/// </summary>
public bool FilePreExisting => this.filePreExisting;
/// <summary>
/// Collection name.
/// </summary>
public string CollectionName => this.collectionName;
/// <summary>
/// File length.
/// </summary>
public long Length => this.length;
/// <summary>
/// Number of blocks in file.
/// </summary>
public uint BlockLimit => this.blockLimit;
internal static void CheckBlockSize(int BlockSize)
{
if (BlockSize < 1024)
throw new ArgumentOutOfRangeException("Block size too small.", nameof(BlockSize));
if (BlockSize > 65536)
throw new ArgumentOutOfRangeException("Block size too large.", nameof(BlockSize));
int i = BlockSize;
while (i != 0 && (i & 1) == 0)
i >>= 1;
if (i != 1)
throw new ArgumentException("The block size must be a power of 2.", nameof(BlockSize));
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
this.file?.Dispose();
this.fileAccess.Dispose();
}
/// <summary>
/// Loads a block from the file.
/// </summary>
/// <param name="BlockIndex">Zero-based block index.</param>
/// <returns>Loaded block.</returns>
public async Task<byte[]> LoadBlock(uint BlockIndex)
{
byte[] Block = new byte[this.blockSize];
await this.LoadBlock(BlockIndex, Block);
return Block;
}
/// <summary>
/// Loads a block from the file.
/// </summary>
/// <param name="BlockIndex">Zero-based block index.</param>
/// <param name="Block"></param>
/// <returns>Loaded block.</returns>
public async Task LoadBlock(uint BlockIndex, byte[] Block)
{
if (Block.Length < this.blockSize)
throw new ArgumentException("Block too small.", nameof(Block));
int NrRead;
await this.fileAccess.BeginWrite(); // Unique access required
try
{
long Pos = ((long)BlockIndex) * this.blockSize;
if (Pos != this.file.Seek(Pos, SeekOrigin.Begin))
throw Database.FlagForRepair(this.collectionName, "Invalid file position.");
if (this.asyncFileIo)
NrRead = await this.file.TryReadAllAsync(Block, 0, this.blockSize);
else
NrRead = this.file.TryReadAll(Block, 0, this.blockSize);
if (this.blockSize != NrRead)
throw Database.FlagForRepair(this.collectionName, "Read past end of file " + this.fileName + ".");
}
finally
{
await this.fileAccess.EndWrite();
}
}
/// <summary>
/// Saves a block to the file.
/// </summary>
/// <param name="BlockIndex">Zero-based block index.</param>
/// <param name="Block">Block to save.</param>
public async Task SaveBlock(uint BlockIndex, byte[] Block)
{
if (Block is null || Block.Length != this.blockSize)
throw Database.FlagForRepair(this.collectionName, "Block not of the correct block size.");
await this.fileAccess.BeginWrite();
try
{
long Pos = ((long)BlockIndex) * this.blockSize;
if (Pos != this.file.Seek(Pos, SeekOrigin.Begin))
throw Database.FlagForRepair(this.collectionName, "Invalid file position.");
if (this.asyncFileIo)
await this.file.WriteAsync(Block, 0, this.blockSize);
else
this.file.Write(Block, 0, this.blockSize);
if (BlockIndex == this.blockLimit)
{
this.blockLimit++;
this.length += this.blockSize;
}
}
finally
{
await this.fileAccess.EndWrite();
}
}
/// <summary>
/// Truncates the file to a given number of blocks.
/// </summary>
/// <param name="BlockLimit">Number of blocks in file.</param>
public async Task Truncate(uint BlockLimit)
{
await this.fileAccess.BeginWrite();
try
{
long Length = ((long)BlockLimit) * this.blockSize;
this.file.SetLength(Length);
this.length = Length;
this.blockLimit = BlockLimit;
}
finally
{
await this.fileAccess.EndWrite();
}
}
/// <summary>
/// Flushes changes to the underlying device.
/// </summary>
public async Task FlushAsync()
{
await this.fileAccess.BeginWrite();
try
{
await this.file.FlushAsync();
}
finally
{
await this.fileAccess.EndWrite();
}
}
}
}