-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_delta_compression.cpp
163 lines (151 loc) · 5.79 KB
/
pipeline_delta_compression.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
#include "pipeline_delta_compression.h"
#include <glog/logging.h>
#define write_to_queue(TASK_NAME, CTX_NAME) \
do { \
std::unique_lock<std::mutex> lock(TASK_NAME##_lock_); \
TASK_NAME##_cv_.wait( \
lock, [this] { return TASK_NAME##_queue_.size() < max_queue_len_; }); \
TASK_NAME##_queue_.push(CTX_NAME); \
TASK_NAME##_cv_.notify_all(); \
} while (0)
#define get_context_from_queue(TASK_NAME, CTX_NAME) \
do { \
std::unique_lock<std::mutex> lock(TASK_NAME##_lock_); \
TASK_NAME##_cv_.wait(lock, [this] { \
return !TASK_NAME##_queue_.empty() || TASK_NAME##_done_; \
}); \
if (!TASK_NAME##_queue_.empty()) { \
CTX_NAME = TASK_NAME##_queue_.front(); \
TASK_NAME##_queue_.pop(); \
} \
TASK_NAME##_cv_.notify_all(); \
} while (0);
#define task_done(TASK_NAME) \
do { \
TASK_NAME##_done_ = true; \
TASK_NAME##_cv_.notify_all(); \
} while (0)
namespace Delta {
void PipelineDeltaCompression::ChunkingWorker() {
while (true) {
auto chunk = this->chunker_->GetNextChunk();
if (!chunk)
break;
context *context_ = new context();
context_->chunk = chunk;
context_->chunk_type = 0;
write_to_queue(chunk, context_);
}
task_done(chunk);
}
void PipelineDeltaCompression::DedupWorker() {
while (true) {
context *context_ = nullptr;
get_context_from_queue(chunk, context_);
if (nullptr == context_)
break;
uint32_t dedup_base_id = dedup_->ProcessChunk(context_->chunk);
if (dedup_base_id != context_->chunk->id()) {
context_->chunk_type = DuplicateChunk;
context_->base_chunk_id = dedup_base_id;
}
write_to_queue(dedup, context_);
}
task_done(dedup);
}
void PipelineDeltaCompression::FeatureCalcWorker() {
while (true) {
context *context_ = nullptr;
get_context_from_queue(dedup, context_);
if (nullptr == context_)
break;
if (context_->chunk_type != DuplicateChunk) {
context_->feature = (*feature_)(context_->chunk);
}
write_to_queue(feature, context_);
}
task_done(feature);
}
void PipelineDeltaCompression::ChunkMatchWorker() {
while (true) {
context *context_ = nullptr;
get_context_from_queue(feature, context_);
if (nullptr == context_)
break;
if (context_->chunk_type != DuplicateChunk) {
const auto &feature = context_->feature;
auto base_chunk_id = index_->GetBaseChunkID(feature);
if (!base_chunk_id.has_value()) {
context_->chunk_type = BaseChunk;
index_->AddFeature(feature, context_->chunk->id());
} else {
context_->chunk_type = DeltaChunk;
context_->base_chunk_id = base_chunk_id.value();
}
}
write_to_queue(match, context_);
}
task_done(match);
}
void PipelineDeltaCompression::ChunkWriteWorker() {
while (true) {
context *context_ = nullptr;
get_context_from_queue(match, context_);
if (nullptr == context_)
break;
auto chunk_len = context_->chunk->len();
total_size_origin_ += chunk_len;
switch (context_->chunk_type) {
case DuplicateChunk:
duplicate_chunk_count_++;
storage_->WriteDuplicateChunk(context_->chunk, context_->base_chunk_id);
break;
case BaseChunk:
base_chunk_count_++;
total_size_compressed_ += chunk_len;
storage_->WriteBaseChunk(context_->chunk);
break;
case DeltaChunk:
delta_chunk_count_++;
chunk_size_before_delta_ += chunk_len;
{
auto delta_chunk = storage_->GetDeltaEncodedChunk(
context_->chunk, context_->base_chunk_id);
chunk_size_after_delta_ += delta_chunk->len();
total_size_compressed_ += delta_chunk->len();
storage_->WriteDeltaChunk(delta_chunk, context_->base_chunk_id);
}
break;
default:
LOG(FATAL)
<< "PipelineDeltaCompression::ChunkWriteWorker(): unknown chunk type "
<< context_->chunk_type;
}
processed_chunk_++;
delete context_;
}
}
void PipelineDeltaCompression::AddFile(const std::string &file_name) {
FileMeta file_meta;
file_meta.file_name = file_name;
file_meta.start_chunk_id = processed_chunk_;
chunker_->ReinitWithFile(file_name);
chunk_done_ = false;
dedup_done_ = false;
feature_done_ = false;
match_done_ = false;
std::thread chunk_worker(&PipelineDeltaCompression::ChunkingWorker, this);
std::thread dedup_worker(&PipelineDeltaCompression::DedupWorker, this);
std::thread feature_worker(&PipelineDeltaCompression::FeatureCalcWorker,
this);
std::thread match_worker(&PipelineDeltaCompression::ChunkMatchWorker, this);
std::thread write_worker(&PipelineDeltaCompression::ChunkWriteWorker, this);
chunk_worker.join();
dedup_worker.join();
feature_worker.join();
match_worker.join();
write_worker.join();
file_meta.end_chunk_id = (processed_chunk_ - 1);
file_meta_writer_.Write(file_meta);
}
} // namespace Delta