-
Notifications
You must be signed in to change notification settings - Fork 8
/
compress.h
111 lines (90 loc) · 2.68 KB
/
compress.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <vector>
#include <bitset>
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define BLOCK_SIZE 4096
// offset: 40bit (~1TB)
// size: 12bit (~4KB)
// ref: 28bit (1TB / 4KB)
// flag: 2bit (00: not-compressed, 01: self-compressed, 10: deduped, 11: delta-compressed)
// total: 40 + 12 + 28 + 2 = 82bit
typedef std::bitset<82> RECIPE;
static inline void set_offset(RECIPE& r, unsigned long long t) { r |= (RECIPE(t) << 42); }
static inline void set_size(RECIPE& r, unsigned long t) { r |= (RECIPE(t) << 30); }
static inline void set_ref(RECIPE& r, unsigned long t) { r |= (RECIPE(t) << 2); }
static inline void set_flag(RECIPE& r, unsigned long t) { r |= (RECIPE(t) << 0); }
static inline unsigned long long get_offset(RECIPE& r) { return ((r << 0) >> 42).to_ullong(); }
static inline unsigned long get_size(RECIPE& r) { return ((r << 40) >> 70).to_ulong(); }
static inline unsigned long get_ref(RECIPE& r) { return ((r << 52) >> 54).to_ulong(); }
static inline unsigned long get_flag(RECIPE& r) { return ((r << 80) >> 80).to_ulong(); }
char compressed[2 * BLOCK_SIZE];
char delta_compressed[2 * BLOCK_SIZE];
struct DATA_IO {
int N;
std::vector<char*> trace;
std::vector<RECIPE> recipe;
char fileName[100];
char outputFileName[100];
char recipeName[100];
FILE* out;
struct timeval start_time, end_time;
DATA_IO(char* name);
~DATA_IO();
void read_file();
inline void write_file(char* data, int size);
inline void recipe_insert(RECIPE r);
void recipe_write();
void time_check_start();
long long time_check_end();
};
DATA_IO::DATA_IO(char* name) {
sprintf(fileName, "%s", name);
sprintf(outputFileName, "%s_output", name);
sprintf(recipeName, "%s_recipe", name);
out = NULL;
}
DATA_IO::~DATA_IO() {
if (out) fclose(out);
for (int i = 0; i < N; ++i) {
free(trace[i]);
}
}
void DATA_IO::read_file() {
N = 0;
trace.clear();
FILE* f = fopen(fileName, "rb");
while (1) {
char* ptr = new char[BLOCK_SIZE];
trace.push_back(ptr);
int now = fread(trace[N++], 1, BLOCK_SIZE, f);
if (!now) {
free(trace.back());
trace.pop_back();
N--;
break;
}
}
fclose(f);
}
inline void DATA_IO::write_file(char* data, int size) {
if (out == NULL) out = fopen(outputFileName, "wb");
fwrite(data, size, 1, out);
}
inline void DATA_IO::recipe_insert(RECIPE r) {
recipe.push_back(r);
}
void DATA_IO::recipe_write() {
FILE* f = fopen(recipeName, "wb");
for (int i = 0; i < N; ++i) {
fwrite(&recipe[i], 1, sizeof(RECIPE), f);
}
fclose(f);
}
void DATA_IO::time_check_start() {
gettimeofday(&start_time, NULL);
}
long long DATA_IO::time_check_end() {
gettimeofday(&end_time, NULL);
return (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_usec - start_time.tv_usec);
}