-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
215 lines (172 loc) · 4.49 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
Program: chunking based deduplicator/reduplicator
Author: Victor Brunko
Nov 28, 2017
Generic algorithm:
1. Read chunk-by-chunk the input file
2. Calculate the hash sum of each chunk
3. Map realtion: chunkX - {count++:position}
4. Export MAP to output file including DATA
Output format example for the deduped file:
**************** FILE_OUT BEGIN ****************
1
5
ddddd
50
1
10
cccccccccc
30
2
10
aaaaaaaaaa
0
10
2
10
bbbbbbbbbb
20
40
**************** FILE_OUT END ****************
where (last five records from FILE_OUT explained):
2 // How many chunks
10 // Chunk's size
bbbbbbbbbb // binary data
20 // position #1
40 // position #2
*/
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <vector>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
const int CHUNK_SIZE = 10;
// Information about the actual chunk:
struct pos_and_size {
size_t pos;
size_t size;
};
typedef std::map<size_t, list<pos_and_size>> dupmap;
uint32_t hash_block(vector<char> chunk) {
uint32_t hash = 0;
for (const auto c : chunk) {
hash += c;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
// Export file helper function
void export_file(dupmap file_map, ifstream &file_in, ofstream &file_out) {
char data[CHUNK_SIZE];
int l_chunk_pos = 0;
int l_chunk_size = 0;
// Iterate through file map:
for (const auto it : file_map) {
list<pos_and_size> l;
l = it.second;
bool once = false;
for (const auto itl : l) {
l_chunk_pos = itl.pos;
l_chunk_size = itl.size;
if (!once) {
once = true;
file_out << l.size() << endl; // How many chunks
file_out << l_chunk_size << endl; // Current chunk size
file_in.seekg(l_chunk_pos, file_in.beg);
file_in.read(data, l_chunk_size); // Save the actual chunk:
file_out.write(data, l_chunk_size);
}
file_out << l_chunk_pos << endl; // Positions located
}
}
}
void dedup(std::string inpath, std::string outpath) {
ifstream file_in(inpath, ifstream::binary);
ofstream file_out(outpath, ifstream::binary);
if (!file_in) {
cerr << "error while opening file_in: " << inpath << endl;
exit(1);
}
if (!file_out) {
cerr << "error while opening file_out: " << inpath << endl;
exit(1);
}
file_in.seekg(0, file_in.end);
dupmap file_map; // hash:count
size_t total_size = file_in.tellg();
size_t chunk_size = CHUNK_SIZE;
size_t total_chunks = total_size / chunk_size;
size_t last_chunk_size = total_size % chunk_size;
size_t hash = 0;
if (last_chunk_size != 0)
++total_chunks;
else
last_chunk_size = chunk_size;
/* the loop of chunking */
file_in.seekg(0, file_in.beg);
for (size_t chunk = 0; chunk < total_chunks; ++chunk) {
size_t this_chunk_size =
chunk == total_chunks - 1 /* if last chunk */
? last_chunk_size /* then fill chunk with remaining bytes */
: chunk_size; /* else fill entire chunk */
size_t start_of_chunk = chunk * chunk_size;
list<pos_and_size> l;
pos_and_size pands;
vector<char> chunk_data(this_chunk_size);
file_in.read(&chunk_data[0], this_chunk_size);
hash = hash_block(chunk_data);
l = file_map[hash];
pands.pos = start_of_chunk;
pands.size = this_chunk_size;
l.push_back(pands);
file_map[hash] = l;
}
export_file(file_map, file_in, file_out);
}
bool redup(std::string inpath, std::string outpath) {
ifstream file_in(inpath, ifstream::binary);
ofstream file_out(outpath, ifstream::binary);
if (!file_in) {
cerr << "error while opening file_in: " << inpath << endl;
exit(1);
}
if (!file_out) {
cerr << "error while opening file_out: " << inpath << endl;
exit(1);
}
char line[64];
char data[CHUNK_SIZE];
size_t count = 0;
size_t pos = 0;
size_t size = 0;
while (file_in) {
file_in.getline(line, 64);
count = atof(line);
if (count <= 0) {
break;
}
file_in.getline(line, 64);
size = atoi(line);
memset(data, 0, 64);
file_in.read(data, size);
while (count--) {
file_in.getline(line, 64);
pos = atoi(line);
file_out.seekp(pos, file_in.beg);
file_out.write(data, size);
}
}
return true;
}
int main() {
dedup("./file.in", "./file.out");
redup("./file.out", "./file.re");
}