-
Notifications
You must be signed in to change notification settings - Fork 13
/
deduper.cpp
168 lines (152 loc) · 5.36 KB
/
deduper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
This file is part of the Fairytale project
Copyright (C) 2018 Márcio Pais
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "deduper.h"
#include <algorithm>
bool Deduper::match(Block* block1, Block* block2, StorageManager* manager) {
// start by checking full hashes and lengths
if (block1->hash != block2->hash || block1->length != block2->length || block1 == block2)
return false;
bool b1wasDormant = false, b2wasDormant = false;
// attempt to revive streams if needed
if (block1->level > 0) {
if (((HybridStream*)block1->data)->wasPurged()) {
if (!block1->attemptRevival(manager))
return false;
}
else
((HybridStream*)block1->data)->setPurgeStatus(false);
}
else {
if ((b1wasDormant = ((FileStream*)block1->data)->dormant()) && !manager->wakeUp((FileStream*)block1->data))
return false;
}
if (block2->level > 0) {
if (((HybridStream*)block2->data)->wasPurged()) {
if (!block2->attemptRevival(manager)) {
if (block1->level > 0)
((HybridStream*)block1->data)->setPurgeStatus(true);
return false;
}
}
else
((HybridStream*)block2->data)->setPurgeStatus(false);
}
else {
if ((b2wasDormant = ((FileStream*)block2->data)->dormant()) && !manager->wakeUp((FileStream*)block2->data)) {
if (block1->level > 0)
((HybridStream*)block1->data)->setPurgeStatus(true);
else if (b1wasDormant)
((FileStream*)block1->data)->goToSleep();
return false;
}
}
// now proceed to compare them
bool ret = true;
int64_t length = block1->length;
off_t off1 = block1->offset, off2 = block2->offset;
try {
while (length > 0 && ret) {
int blsize = (int)std::min<int64_t>(GENERIC_BUFFER_SIZE, length);
block1->data->setPos(off1);
int l = (int)block1->data->blockRead(&buffer[0], blsize);
block2->data->setPos(off2);
ret = (l == (int)block2->data->blockRead(&buffer[GENERIC_BUFFER_SIZE], blsize));
if (ret) {
ret = memcmp(&buffer[0], &buffer[GENERIC_BUFFER_SIZE], blsize) == 0;
off1 += l, off2 += l;
length -= l;
}
}
}
catch (ExhaustedStorageException const&) {
ret = false;
}
// clean up
if (block1->level > 0)
((HybridStream*)block1->data)->setPurgeStatus(true);
else if (b1wasDormant)
((FileStream*)block1->data)->goToSleep();
if (block2->level > 0)
((HybridStream*)block2->data)->setPurgeStatus(true);
else if (b2wasDormant)
((FileStream*)block2->data)->goToSleep();
return ret;
}
Deduper::Deduper(void) : table(DEDUP_HASH_SIZE), buffer(GENERIC_BUFFER_SIZE * 2) {}
Deduper::~Deduper() {
LinkedList *l, *n;
for (size_t i = 0; i < table.size(); i++) {
l = table[i].next;
while (l != nullptr) {
n = l->next;
delete l;
l = n;
}
}
}
void Deduper::process(Block* start, Block* end, StorageManager* manager) {
if (start == nullptr || start->data == nullptr || start->level >= MAX_RECURSION_LEVEL || manager == nullptr)
return;
Block* block = start;
LinkedList* entry;
while (block != nullptr && block != end) {
assert(block->hashed);
entry = &table[block->hash & DEDUP_HASH_MASK];
// first entry?
if (entry->item == nullptr)
entry->item = block;
else {
// loop through list
while (entry != nullptr) {
if (entry->item == block)
break;
// check for match
else if (match(entry->item, block, manager)) {
// free any previously allocated info for this block
block->freeInfo();
// now free any childs
block->freeChilds(manager);
if (block->level > 0) {
// free the stream if possible
if (block->offset == 0 && block->length == block->data->getSize()) {
manager->UpdateStorageBudget((HybridStream*)block->data, false);
((HybridStream*)block->data)->close();
}
// otherwise just decrease its reference count
else
((HybridStream*)block->data)->decRefCount();
}
block->type = BlockType::DEDUP;
// info now points to the block we deduplicated from
block->info = entry->item;
block->done = true;
break;
}
// end of the list and no match found?
else if (entry->next == nullptr) {
// append this block
entry->next = new LinkedList{ block, nullptr };
break;
}
else
entry = entry->next;
}
}
// recurse if possible
if (block->child != nullptr)
process(block->child, nullptr, manager);
block = block->next;
}
}