-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
156 lines (140 loc) · 4.32 KB
/
object.cpp
File metadata and controls
156 lines (140 loc) · 4.32 KB
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
/*
* object.cpp
*
* Created on: Nov 15, 2017
* Author: hhx008
*/
# include "object.h"
# include "util.h"
# include <set>
# include <utility>
# include <iostream>
# include "db.h"
// CAUTIOUS, need to grab proper lock before call this function
ObjectOps::CHECK_RET ObjectOps::validCheck(DB * db, metablock * meta, bool deep) {
db->blocklock.lockBlock(TO_NVMP(meta));
if (meta->valid == OBJ_VALID) {
db->blocklock.unlockBlock(TO_NVMP(meta));
return CHECK_RET(OBJ_VALID, false);
} else if (meta->valid == OBJ_INVALID) {
db->blocklock.unlockBlock(TO_NVMP(meta));
return CHECK_RET(OBJ_INVALID, false);
}
if (deep) {
// note that: a combined meta could only be VALID, so we don't have to consider how to check it
// i.e., only single meta could be OBJ_INPROGRESS, combined meta are always OBJ_VALID
datablock * data = TO_DATAP(meta->data);
assert(data->next == NULL);
if (calCRC32(data) == meta->crc32) {
// persist data here before mark it as OBJ_VALID
persist((void *)TO_REGIONP(data->region), data->size);
meta->valid = OBJ_VALID;
persist((void *)meta, sizeof(metablock));
db->blocklock.unlockBlock(TO_NVMP(meta));
return CHECK_RET(OBJ_VALID, true);
} else if (timeoutCheck(meta)) {
meta->valid = OBJ_INVALID;
persist((void *)meta, sizeof(metablock));
db->blocklock.unlockBlock(TO_NVMP(meta));
return CHECK_RET(OBJ_INVALID, true);
}
}
db->blocklock.unlockBlock(TO_NVMP(meta));
return CHECK_RET(OBJ_INPROGRESS, false);
}
bool ObjectOps::timeoutCheck(metablock * meta) {
time_t curr = time(0);
time_t record = meta->timestamp;
if (record + TIMEOUT_TIME < curr)
return true;
else return false;
}
int ObjectOps::intersectExtent(std::vector<OPI> & opiv, std::set<std::pair<int, int>> & readset,
metablock * meta, datablock * curr) {
int glow = curr->offset, ghigh = curr->offset + curr->size;
int added = 0;
char * region = TO_REGIONP(curr->region);
// traverse extent of read
auto begin = readset.lower_bound(std::make_pair(glow, 0));
auto end = readset.lower_bound(std::make_pair(ghigh, 0));
if (begin != readset.begin()) {
begin--;
if (begin->second > glow) {
// CAUTION, filling opi also happens here (as well as reset meta readtime)
if (ghigh < begin->second) {
opiv.push_back(OPI(meta, curr, region, glow, ghigh - glow));
added++;
}
else {
opiv.push_back(OPI(meta, curr, region, glow, begin->second - glow));
added++;
}
}
begin++;
}
for (auto it = begin; it != end; it++) {
// CAUTION, filling opi also happens here (as well as reset meta readtime)
if (ghigh < it->second) {
opiv.push_back(OPI(meta, curr, region + (it->first - glow), it->first, ghigh - it->first));
added++;
}
else {
opiv.push_back(OPI(meta, curr, region + (it->first - glow), it->first, it->second - it->first));
added++;
}
}
std::vector<std::pair<int, int>> tmpv;
begin = readset.lower_bound(std::make_pair(glow, 0));
end = readset.lower_bound(std::make_pair(ghigh, 0));
if (begin != readset.begin()) {
begin--;
if (begin->second > glow) {
if (ghigh < begin->second) {
tmpv.push_back(std::make_pair(begin->first, glow));
tmpv.push_back(std::make_pair(ghigh, begin->second));
} else {
tmpv.push_back(std::make_pair(begin->first, glow));
}
begin = readset.erase(begin);
} else begin++;
}
for (auto it = begin; it != end;) {
if (ghigh < it->second)
tmpv.push_back(std::make_pair(ghigh, it->second));
it = readset.erase(it);
}
for (auto p : tmpv)
readset.insert(p);
return added;
}
std::vector<OPI> ObjectOps::fallGetOPI(DB * db, object * objp, int start, int end) {
std::vector<OPI> opiv;
std::set<std::pair<int, int>> readset;
readset.insert(std::make_pair(start, end));
metablock * curr = TO_METAP(objp->logtail);
metablock * next;
datablock * datacurr;
bool found = false;
while (!found && curr) {
next = TO_METAP(curr->peer);
CHECK_RET cr = ObjectOps::validCheck(db, curr);
switch (cr.vs) {
case OBJ_VALID:
datacurr = TO_DATAP(curr->data);
while (datacurr) {
intersectExtent(opiv, readset, curr, datacurr);
datacurr = TO_DATAP(datacurr->next);
}
if (readset.size() == 0)
found = true;
case OBJ_INVALID:
case OBJ_INPROGRESS:
default:
curr = next;
}
}
return opiv;
}
inline unsigned long long ObjectOps::calCRC32(datablock * data) {
return CRC32(TO_REGIONP(data->region), data->size, 0);
}