-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_io.h
99 lines (85 loc) · 1.98 KB
/
disk_io.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
#ifndef _DISK_IO_H_
#define _DISK_IO_H_
#include "batch_dedup_config.h"
#include "trace_types.h"
#include <fstream>
#include <string>
using namespace std;
template<class T>
class RecordReader
{
public:
RecordReader(const string& fname)
: mBuffer(NULL),
mFileName(fname)
{
mBuffer = new char[Env::GetReadBufSize()];
mInput.rdbuf()->pubsetbuf(mBuffer, Env::GetReadBufSize());
mInput.open(mFileName.c_str(), ios::in | ios::binary);
if (mInput.is_open()) {
LOG_DEBUG("open for read success" << fname);
}
else {
LOG_DEBUG("open for read fail" << fname);
}
}
~RecordReader()
{
if (mInput.is_open()) {
mInput.close();
}
if (mBuffer != NULL) {
delete[] mBuffer;
}
}
bool Get(T& record)
{
return record.FromStream(mInput);
}
private:
char* mBuffer;
string mFileName;
ifstream mInput;
};
template<class T>
class RecordWriter
{
public:
RecordWriter(const string& fname, bool append = false)
: mBuffer(NULL),
mFileName(fname)
{
mBuffer = new char[Env::GetWriteBufSize()];
mOutput.rdbuf()->pubsetbuf(mBuffer, Env::GetWriteBufSize());
if (append) {
mOutput.open(mFileName.c_str(), ios::out | ios::binary | ios::app);
}
else {
mOutput.open(mFileName.c_str(), ios::out | ios::binary | ios::trunc);
}
if (mOutput.is_open()) {
LOG_DEBUG("open for write success" << fname);
}
else {
LOG_DEBUG("open for write fail" << fname);
}
}
~RecordWriter()
{
if (mOutput.is_open()) {
mOutput.close();
}
if (mBuffer != NULL) {
delete[] mBuffer;
}
}
void Put(const T& record)
{
record.ToStream(mOutput);
}
private:
char* mBuffer;
string mFileName;
ofstream mOutput;
};
#endif