forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileWriter.h
87 lines (71 loc) · 1.97 KB
/
FileWriter.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
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <wdt/Protocol.h>
#include <wdt/WdtConfig.h>
#include <wdt/Writer.h>
#include <wdt/util/FileCreator.h>
namespace facebook {
namespace wdt {
class FileWriter : public Writer {
public:
FileWriter(ThreadCtx &threadCtx, BlockDetails const *blockDetails,
FileCreator *fileCreator)
: threadCtx_(threadCtx),
blockDetails_(blockDetails),
#ifdef HAS_SYNC_FILE_RANGE
nextSyncOffset_(blockDetails->offset),
#endif
fileCreator_(fileCreator) {
}
~FileWriter() override;
/// @see Writer.h
ErrorCode open() override;
/// @see Writer.h
ErrorCode write(char *buf, int64_t size) override;
/// @see Writer.h
int64_t getTotalWritten() override {
return totalWritten_;
}
/// @see Writer.h
/// This method calls fsync() and posix_fadvise, except if options are set
/// to disable it.
ErrorCode sync() override;
/// @see Writer.h
ErrorCode close() override;
private:
/**
* calls sync_file_range at disk_sync_interval_mb intervals.
*
* @param written number of bytes last written
* @param forced whether to force syncing or not
*/
bool syncFileRange(int64_t written, bool forced);
/**
* Return true if the file is already closed.
*/
bool isClosed();
ThreadCtx &threadCtx_;
/// file handler
int fd_{-1};
/// details of the block
BlockDetails const *blockDetails_;
/// number of bytes written
int64_t totalWritten_{0};
#ifdef HAS_SYNC_FILE_RANGE
/// offset to use for next sync
int64_t nextSyncOffset_;
/// number of bytes written since last sync
int64_t writtenSinceLastSync_{0};
#endif
/// reference to file creator
FileCreator *fileCreator_;
};
}
}