-
Notifications
You must be signed in to change notification settings - Fork 0
/
logfile.cpp
132 lines (117 loc) · 3.4 KB
/
logfile.cpp
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
#include <string>
#include <format>
#include <filesystem>
#include <fstream>
#include <vector>
#include "skiplist.h"
#include "keyvalue.h"
#include "options.h"
#include "diskio.h"
#include "logfile.h"
LogFile::LogFile(const std::string &dbpath, uint64_t id, const Options &options) :
path(dbpath+"/log."+std::to_string(id)), options(options), file(&filebuf)
{
if(dbpath=="") return;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
filebuf.open(path, O_CREAT | O_WRONLY | O_TRUNC | (options.enableSyncWrite ? O_SYNC : 0));
if(!options.enableSyncWrite && options.disableWriteFlush) {
disableFlush = true;
}
}
void LogFile::startBatch(int len) {
inBatch = true;
writeLEuint32(file,-len);
}
void LogFile::endBatch(int len) {
inBatch=false;
writeLEuint32(file,-len);
file.flush();
}
void LogFile::write(const ByteBuffer& key,const ByteBuffer& value) {
writeLEuint32(file, key.length);
file.write((char*)(uint8_t*)key,key.length);
writeLEuint32(file, value.length);
file.write((char*)(uint8_t*)value,value.length);
if(!inBatch && !disableFlush) {
file.flush();
}
}
void LogFile::close() {
file.flush();
filebuf.close();
}
void LogFile::remove() {
if(path!="") {
std::filesystem::remove(path);
}
}
void readLogFile(SkipList<const KeyValue> &list, std::string path, const Options &options)
{
std::fstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
file.open(path, std::fstream::in | std::fstream::binary);
auto readBatch = [&](int32_t len)
{
std::vector<const KeyValue> entries;
try
{
for (int i = 0; i < (len * -1); i++)
{
int32_t kLen = readLEuint32(file);
ByteBuffer key(kLen);
file.read((char *)(uint8_t *)key, kLen);
int32_t vLen = readLEuint32(file);
ByteBuffer value(vLen);
file.read((char *)(uint8_t *)value, vLen);
entries.push_back(KeyValue(key, value));
}
int32_t len0 = readLEuint32(file);
if (len0 != len)
throw DatabaseCorrupted();
}
catch (exception& ex)
{
if (options.batchReadMode != Options::BatchReadMode::applyPartial)
{
throw ex;
}
}
for (auto &e : entries)
{
list.put(e);
}
};
try
{
while (true)
{
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
int32_t len = readLEuint32(file);
if (file.eof())
return;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
if (len < 0)
{
readBatch(len);
}
else
{
uint32_t kLen = len;
ByteBuffer key(kLen);
file.read((char *)(uint8_t *)key, kLen);
uint32_t vLen = readLEuint32(file);
ByteBuffer value(vLen);
file.read((char *)(uint8_t *)value, vLen);
list.put(KeyValue(key, value));
}
}
}
catch (std::exception& ex)
{
if (options.batchReadMode == Options::BatchReadMode::returnOpenError)
{
throw ex;
}
return;
}
}