forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileWriter.h
80 lines (67 loc) · 1.88 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
/**
* 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/WdtConfig.h>
#include "Writer.h"
#include "FileCreator.h"
#include "Protocol.h"
namespace facebook {
namespace wdt {
class FileWriter : public Writer {
public:
FileWriter(int threadIndex, BlockDetails const *blockDetails,
FileCreator *fileCreator)
: threadIndex_(threadIndex),
blockDetails_(blockDetails),
#ifdef HAS_SYNC_FILE_RANGE
nextSyncOffset_(blockDetails->offset),
#endif
fileCreator_(fileCreator) {
}
virtual ~FileWriter() {
close();
}
/// @see Writer.h
virtual ErrorCode open() override;
/// @see Writer.h
virtual ErrorCode write(char *buf, int64_t size) override;
/// @see Writer.h
virtual int64_t getTotalWritten() override {
return totalWritten_;
}
/// @see Writer.h
virtual void 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
*/
void syncFileRange(int64_t written, bool forced);
/// file handler
int fd_{-1};
/// index of the owner receiver thread. This is needed for co-ordination of
/// disk space allocation in fileCreator
int threadIndex_;
/// 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_;
};
}
}