forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_file_mmap.h
600 lines (524 loc) · 19.3 KB
/
tkrzw_file_mmap.h
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*************************************************************************************************
* File implementations by memory mapping
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#ifndef _TKRZW_FILE_MMAP_H
#define _TKRZW_FILE_MMAP_H
#include <memory>
#include <string>
#include <cinttypes>
#include "tkrzw_file.h"
#include "tkrzw_lib_common.h"
namespace tkrzw {
/**
* Interface for memory mapping file implementations.
*/
class MemoryMapFile : public File {
public:
/**
* Destructor.
*/
virtual ~MemoryMapFile() = default;
/**
* Locks the memory of the beginning region of the file, not to be swapped out.
* @param size The size of the beginning region to lock.
* @return The result status.
* @details This method must be called after the file is opened.
*/
virtual Status LockMemory(size_t size) = 0;
};
class MemoryMapParallelFileImpl;
/**
* File implementation by memory mapping and locking for parallel operations.
* @details Reading and writing operations are thread-safe; Multiple threads can access the same
* file concurrently. Other operations including Open, Close, Truncate, and Synchronize are not
* thread-safe. Moreover, locking doesn't assure atomicity of reading and writing operations.
*/
class MemoryMapParallelFile final : public MemoryMapFile {
public:
/**
* Structure to make a shared section where a region can be accessed.
* @details The zone object contains a mutex which is locked by the constructor and unlocked by
* the destructor. The user can access the region freely while the zone object is alive.
*/
class Zone {
friend class MemoryMapParallelFile;
public:
/**
* Destuctor.
*/
~Zone();
/**
* Copy and assignment are disabled.
*/
explicit Zone(const Zone& rhs) = delete;
Zone& operator =(const Zone& rhs) = delete;
/**
* Gets the offset of the region to access.
* @return The offset of the region to access.
*/
int64_t Offset() const;
/**
* Gets the pointer to the region to access.
* @return The pointer to the region to access.
*/
char* Pointer() const;
/**
* Gets the size of the region to access.
* @return The size of the region to access.
*/
size_t Size() const;
private:
/**
* Constructor.
* @param file The file implementation object.
* @param writable Whether the zone is writable.
* @param off The offset of a region.
* @param size The size of the data to be read.
*/
explicit Zone(
MemoryMapParallelFileImpl* file, bool writable,
int64_t off, size_t size, Status* status);
/** The file object. */
MemoryMapParallelFileImpl* file_;
/** The offset of the region. */
int64_t off_;
/** The size of the region. */
size_t size_;
/** Whether the zone is writable. */
bool writable_;
};
/**
* Default constructor
*/
MemoryMapParallelFile();
/**
* Destructor.
*/
virtual ~MemoryMapParallelFile();
/**
* Copy and assignment are disabled.
*/
explicit MemoryMapParallelFile(const MemoryMapParallelFile& rhs) = delete;
MemoryMapParallelFile& operator =(const MemoryMapParallelFile& rhs) = delete;
/**
* Opens a file.
* @param path A path of the file.
* @param writable If true, the file is writable. If false, it is read-only.
* @param options Bit-sum options of File::OpenOption enums.
* @return The result status.
* @details By default, exclusive locking against other processes is done for a writer and
* shared locking against other processes is done for a reader.
*/
Status Open(const std::string& path, bool writable, int32_t options = OPEN_DEFAULT) override;
/**
* Closes the file.
* @return The result status.
*/
Status Close() override;
/**
* Makes an accessible zone.
* @param writable If true, the region is for reading and writing. If false, the region is
* only for reading.
* @param off The offset of the region to access.
* @param size The size of the region to access.
* @param zone A unique pointer to own the writable zone.
* @return The result status.
* @details If the writable flag is true and the region is out of the current file size, the file
* is expanded. If the writable flag is true and the offset is negative, the offset is set at
* the end of the file. If the writable flag is false and the region is out of the current file
* size, the size of the region is fitted to the file size or the operation fails.
*/
Status MakeZone(bool writable, int64_t off, size_t size, std::unique_ptr<Zone>* zone);
/**
* Reads data.
* @param off The offset of a source region.
* @param buf The pointer to the destination buffer.
* @param size The size of the data to be read.
* @return The result status.
*/
Status Read(int64_t off, void* buf, size_t size) override;
/**
* Reads data, in a simple way.
* @param off The offset of a source region.
* @param size The size of the data to be read.
* @return A string of the read data. It is empty on failure.
*/
std::string ReadSimple(int64_t off, size_t size) override;
/**
* Writes data.
* @param off The offset of the destination region.
* @param buf The pointer to the source buffer.
* @param size The size of the data to be written.
* @return The result status.
*/
Status Write(int64_t off, const void* buf, size_t size) override;
/**
* Appends data at the end of the file.
* @param buf The pointer to the source buffer.
* @param size The size of the data to be written.
* @param off The pointer to an integer object to contain the offset at which the data has been
* put. If it is nullptr, it is ignored.
* @return The result status.
*/
Status Append(const void* buf, size_t size, int64_t* off = nullptr) override;
/**
* Expands the file size without writing data.
* @param inc_size The size to increment the file size by.
* @param old_size The pointer to an integer object to contain the old size of the file.
* If it is nullptr, it is ignored.
* @return The result status.
*/
Status Expand(size_t inc_size, int64_t* old_size = nullptr) override;
/**
* Truncates the file.
* @param size The new size of the file.
* @return The result status.
*/
Status Truncate(int64_t size) override;
/**
* Truncate the file fakely.
* @param size The new size of the file.
* @return The result status.
* @details This doesn't modify the actual file but modifies the internal length parameter,
* which affects behavior of Close, Synchronize, Append, Expand, and GetSize. If the
* specified size is more than the actual file size, the operation fails.
*/
Status TruncateFakely(int64_t size) override;
/**
* Synchronizes the content of the file to the file system.
* @param hard True to do physical synchronization with the hardware or false to do only
* logical synchronization with the file system.
* @param off The offset of the region to be synchronized.
* @param size The size of the region to be synchronized. If it is zero, the length to the
* end of file is specified.
* @return The result status.
* @details The pysical file size can be larger than the logical size in order to improve
* performance by reducing frequency of allocation. Thus, you should call this function before
* accessing the file with external tools.
*/
Status Synchronize(bool hard, int64_t off = 0, int64_t size = 0) override;
/**
* Gets the size of the file.
* @param size The pointer to an integer object to contain the result size.
* @return The result status.
*/
Status GetSize(int64_t* size) override;
/**
* Sets allocation strategy.
* @param init_size An initial size of allocation.
* @param inc_factor A factor to increase the size of allocation.
* @return The result status.
* @details By default, the initial size is 1MB and the increasing factor is 2. This method
* must be called before the file is opened.
*/
Status SetAllocationStrategy(int64_t init_size, double inc_factor) override;
/**
* Copies internal properties to another file object.
* @param file The other file object.
* @return The result status.
*/
Status CopyProperties(File* file) override;
/**
* Locks the memory of the beginning region of the file, not to be swapped out.
* @param size The size of the beginning region to lock.
* @return The result status.
* @details This method must be called after the file is opened.
*/
Status LockMemory(size_t size) override;
/**
* Gets the path of the file.
* @param path The pointer to a string object to store the path.
* @return The result status.
*/
Status GetPath(std::string* path) override;
/**
* Renames the file.
* @param new_path A new path of the file.
* @return The result status.
*/
Status Rename(const std::string& new_path) override;
/**
* Disables operations related to the path.
* @return The result status.
* @details This should be called if the file is overwritten by external operations.
*/
Status DisablePathOperations() override;
/**
* Checks whether operations are done by memory mapping.
* @return Always true. This is fast, but the file size cannot exceed the virtual memory.
*/
bool IsMemoryMapping() const override {
return true;
}
/**
* Checks whether updating operations are atomic and thread-safe.
* @return Always false. Atomicity is not assured. Some operations are not thread-safe.
* are not.
*/
bool IsAtomic() const override {
return false;
}
/**
* Makes a new file object of the same concrete class.
* @return The new file object.
*/
std::unique_ptr<File> MakeFile() const override {
return std::make_unique<MemoryMapParallelFile>();
}
private:
/** Pointer to the actual implementation. */
MemoryMapParallelFileImpl* impl_;
};
class MemoryMapAtomicFileImpl;
/**
* File implementation by memory mapping and locking for atomic operations.
* @details All operations are thread-safe; Multiple threads can access the same file concurrently.
* Also, locking assures that every operation is observed in an atomic manner.
*/
class MemoryMapAtomicFile final : public MemoryMapFile {
public:
/**
* Structure to make a critical section where a region can be accessed.
* @details The zone object contains a mutex which is locked by the constructor and unlocked by
* the destructor. The user can access the region freely while the zone object is alive.
*/
class Zone {
friend class MemoryMapAtomicFile;
public:
/**
* Destuctor.
*/
~Zone();
/**
* Copy and assignment are disabled.
*/
explicit Zone(const Zone& rhs) = delete;
Zone& operator =(const Zone& rhs) = delete;
/**
* Gets the offset of the region to access.
* @return The offset of the region to access.
*/
int64_t Offset() const;
/**
* Gets the pointer to the region to access.
* @return The pointer to the region to access.
*/
char* Pointer() const;
/**
* Gets the size of the region to access.
* @return The size of the region to access.
*/
size_t Size() const;
private:
/**
* Constructor.
* @param file The file implementation object.
* @param writable Whether the zone is writable.
* @param off The offset of a region.
* @param size The size of the data to be read.
*/
explicit Zone(
MemoryMapAtomicFileImpl* file, bool writable,
int64_t off, size_t size, Status* status);
/** The file object. */
MemoryMapAtomicFileImpl* file_;
/** The offset of the region. */
int64_t off_;
/** The size of the region. */
size_t size_;
/** Whether the zone is writable. */
bool writable_;
};
/**
* Default constructor
*/
MemoryMapAtomicFile();
/**
* Destructor.
*/
virtual ~MemoryMapAtomicFile();
/**
* Copy and assignment are disabled.
*/
explicit MemoryMapAtomicFile(const MemoryMapAtomicFile& rhs) = delete;
MemoryMapAtomicFile& operator =(const MemoryMapAtomicFile& rhs) = delete;
/**
* Opens a file.
* @param path A path of the file.
* @param writable If true, the file is writable. If false, it is read-only.
* @param options Bit-sum options of File::OpenOption enums.
* @return The result status.
* @details By default, exclusive locking against other processes is done for a writer and
* shared locking against other processes is done for a reader.
*/
Status Open(const std::string& path, bool writable, int32_t options = OPEN_DEFAULT) override;
/**
* Closes the file.
* @return The result status.
*/
Status Close() override;
/**
* Makes an accessible zone.
* @param writable If true, the region is for reading and writing. If false, the region is
* only for reading.
* @param off The offset of the region to access.
* @param size The size of the region to access.
* @param zone A unique pointer to own the writable zone.
* @return The result status.
* @details If the writable flag is true and the region is out of the current file size, the file
* is expanded. If the writable flag is true and the offset is negative, the offset is set at
* the end of the file. If the writable flag is false and the region is out of the current file
* size, the size of the region is fitted to the file size or the operation fails.
*/
Status MakeZone(bool writable, int64_t off, size_t size, std::unique_ptr<Zone>* zone);
/**
* Reads data.
* @param off The offset of a source region.
* @param buf The pointer to the destination buffer.
* @param size The size of the data to be read.
* @return The result status.
*/
Status Read(int64_t off, void* buf, size_t size) override;
/**
* Reads data, in a simple way.
* @param off The offset of a source region.
* @param size The size of the data to be read.
* @return A string of the read data. It is empty on failure.
*/
std::string ReadSimple(int64_t off, size_t size) override;
/**
* Writes data.
* @param off The offset of the destination region.
* @param buf The pointer to the source buffer.
* @param size The size of the data to be written.
* @return The result status.
*/
Status Write(int64_t off, const void* buf, size_t size) override;
/**
* Appends data at the end of the file.
* @param buf The pointer to the source buffer.
* @param size The size of the data to be written.
* @param off The pointer to an integer object to contain the offset at which the data has been
* put. If it is nullptr, it is ignored.
* @return The result status.
*/
Status Append(const void* buf, size_t size, int64_t* off = nullptr) override;
/**
* Expands the file size without writing data.
* @param inc_size The size to increment the file size by.
* @param old_size The pointer to an integer object to contain the old size of the file.
* If it is nullptr, it is ignored.
* @return The result status.
*/
Status Expand(size_t inc_size, int64_t* old_size = nullptr) override;
/**
* Truncates the file.
* @param size The new size of the file.
* @return The result status.
*/
Status Truncate(int64_t size) override;
/**
* Truncate the file fakely.
* @param size The new size of the file.
* @return The result status.
* @details This doesn't modify the actual file but modifies the internal length parameter,
* which affects behavior of Close, Synchronize, Append, Expand, and GetSize. If the
* specified size is more than the actual file size, the operation fails.
*/
Status TruncateFakely(int64_t size) override;
/**
* Synchronizes the content of the file to the file system.
* @param hard True to do physical synchronization with the hardware or false to do only
* logical synchronization with the file system.
* @param off The offset of the region to be synchronized.
* @param size The size of the region to be synchronized. If it is zero, the length to the
* end of file is specified.
* @return The result status.
* @details The pysical file size can be larger than the logical size in order to improve
* performance by reducing frequency of allocation. Thus, you should call this function before
* accessing the file with external tools.
*/
Status Synchronize(bool hard, int64_t off = 0, int64_t size = 0) override;
/**
* Gets the size of the file.
* @param size The pointer to an integer object to contain the result size.
* @return The result status.
*/
Status GetSize(int64_t* size) override;
/**
* Sets allocation strategy.
* @param init_size An initial size of allocation.
* @param inc_factor A factor to increase the size of allocation.
* @return The result status.
* @details By default, the initial size is 1MB and the increasing factor is 2. This method
* must be called before the file is opened.
*/
Status SetAllocationStrategy(int64_t init_size, double inc_factor) override;
/**
* Copies internal properties to another file object.
* @param file The other file object.
* @return The result status.
*/
Status CopyProperties(File* file) override;
/**
* Locks the memory of the beginning region of the file, not to be swapped out.
* @param size The size of the beginning region to lock.
* @return The result status.
* @details This method must be called after the file is opened.
*/
Status LockMemory(size_t size) override;
/**
* Gets the path of the file.
* @param path The pointer to a string object to store the path.
* @return The result status.
*/
Status GetPath(std::string* path) override;
/**
* Renames the file.
* @param new_path A new path of the file.
* @return The result status.
*/
Status Rename(const std::string& new_path) override;
/**
* Disables operations related to the path.
* @return The result status.
* @details This should be called if the file is overwritten by external operations.
*/
Status DisablePathOperations() override;
/**
* Checks whether operations are done by memory mapping.
* @return Always true. This is fast, but the file size cannot exceed the virtual memory.
*/
bool IsMemoryMapping() const override {
return true;
}
/**
* Checks whether updating operations are atomic and thread-safe.
* @return Always true. Atomicity is assured. All operations are thread-safe.
*/
bool IsAtomic() const override {
return true;
}
/**
* Makes a new file object of the same concrete class.
* @return The new file object.
*/
std::unique_ptr<File> MakeFile() const override {
return std::make_unique<MemoryMapAtomicFile>();
}
private:
/** Pointer to the actual implementation. */
MemoryMapAtomicFileImpl* impl_;
};
} // namespace tkrzw
#endif // _TKRZW_FILE_MMAP_H
// END OF FILE