forked from Preparation-Publication-BD2K/db_compress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompression.cpp
78 lines (67 loc) · 2.41 KB
/
decompression.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
#include "base.h"
#include "decompression.h"
#include <fstream>
#include <vector>
namespace db_compress {
namespace {
Model* GetModelFromDescription(ByteReader* byte_reader, const Schema& schema, size_t index) {
Model* ret;
unsigned char creator_index = byte_reader->ReadByte();
ret = GetAttrModel(schema.attr_type[index])[creator_index]
->ReadModel(byte_reader, schema, index);
return ret;
}
} // anonymous namespace
Decompressor::Decompressor(const char* compressedFileName, const Schema& schema) :
byte_reader_(compressedFileName),
schema_(schema) {
}
void Decompressor::Init() {
implicit_length_ = byte_reader_.ReadByte();
for (size_t i = 0; i < schema_.attr_type.size(); ++ i) {
attr_order_.push_back(byte_reader_.Read16Bit());
}
for (size_t i = 0; i < schema_.attr_type.size(); ++ i) {
std::unique_ptr<Model> model(GetModelFromDescription(&byte_reader_, schema_, i));
model_.push_back(std::move(model));
}
implicit_prefix_ = 0;
ReadTuplePrefix();
}
void Decompressor::ReadTuplePrefix() {
while (implicit_prefix_ < ((unsigned)1 << implicit_length_)) {
bool bit = byte_reader_.ReadBit();
if (bit)
++ implicit_prefix_;
else
break;
}
}
void Decompressor::ReadNextTuple(Tuple* tuple) {
unsigned int implicit_prefix_count_ = 1;
ProbInterval PIt(GetZeroProb(), GetOneProb());
UnitProbInterval PIb = GetWholeProbInterval();
for (size_t i = 0; i < schema_.attr_type.size(); ++i) {
Decoder* decoder = model_[attr_order_[i]]->GetDecoder(*tuple, PIt, PIb);
while (!decoder->IsEnd()) {
bool bit;
if (implicit_prefix_count_ <= implicit_length_) {
bit = ((implicit_prefix_ >> (implicit_length_ - implicit_prefix_count_)) & 1);
++ implicit_prefix_count_;
} else
bit = byte_reader_.ReadBit();
decoder->FeedBit(bit);
}
PIt = decoder->GetPIt();
PIb = decoder->GetPIb();
const AttrValue* result = decoder->GetResult();
tuple->attr[attr_order_[i]] = result;
}
// We read the prefix for next tuple after finish reading the current tuple,
// this helps us to determine the end of file
ReadTuplePrefix();
}
bool Decompressor::HasNext() const {
return (implicit_prefix_ < ((unsigned)1 << implicit_length_));
}
} // namespace db_compress