-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompressMap.cpp
324 lines (279 loc) · 6.86 KB
/
CompressMap.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "CompressMap.h"
#include "common.h"
#include "zlib.h"
const int kCompressBlockBufSize = 16*1024;
CompressStorage::CompressStorage( int size /*= 0*/ )
{
default_bufsize = size > 0 ? size : kCompressBlockBufSize;
}
CompressStorage::~CompressStorage()
{
Clear();
}
void CompressStorage::_BlockInit( _Block* block )
{
memset(block, 0, sizeof(_Block));
}
void CompressStorage::_BlockClear( _Block* block )
{
_BlockClearBuf(block);
_BlockClearCBuf(block);
memset(block, 0, sizeof(_Block));
}
void CompressStorage::_BlockClearBuf( _Block* block )
{
if (block->buf)
delete [] block->buf;
block->buf = NULL;
block->buf_len = 0;
block->w_off = 0;
}
void CompressStorage::_BlockClearCBuf( _Block* block )
{
if (block->cbuf)
delete [] block->cbuf;
block->cbuf = NULL;
block->cbuf_len = 0;
}
void CompressStorage::_BlockNew( _Block* block, int size )
{
memset(block, 0, sizeof(_Block));
_BlockNewBuf(block, default_bufsize > size ? default_bufsize : size);
}
void CompressStorage::_BlockNewBuf( _Block* block, int size )
{
if (size > block->buf_len)
{
_BlockClearBuf(block);
block->buf_len = size;
block->buf = new char [block->buf_len];
}
block->w_off = 0;
}
void CompressStorage::_BlockNewCBuf( _Block* block, int size )
{
if (size > block->cbuf_len)
{
_BlockClearCBuf(block);
block->cbuf_len = size;
block->cbuf = new char [block->cbuf_len];
}
}
bool CompressStorage::_BlockIsSufficient( _Block* block, int len )
{
if (block->cbuf)
return false;
if (block->w_off + len > block->buf_len)
return false;
return true;
}
bool CompressStorage::_BlockWrite( _Block* block, Pointer* ptr, char* src, int src_len, int w_off /*= -1*/ )
{
if (w_off == -1)
w_off = block->w_off;
memcpy(block->buf + w_off, src, src_len);
ptr->off = w_off;
ptr->len = src_len;
if (w_off == -1)
block->w_off += src_len;
else
block->w_off = max(block->w_off, w_off + src_len);
return true;
}
bool CompressStorage::_BlockCompress( _Block* block )
{
block->compress_len = compressBound(block->buf_len);
std::string buf;
buf.resize(block->compress_len);
int ret = compress((Bytef *)buf.c_str(), (uLongf *)&block->compress_len, (const Bytef *)block->buf, block->w_off);
if (ret != Z_OK)
{
assert(false);
return false;
}
_BlockNewCBuf(block, block->compress_len);
memcpy(block->cbuf, (char*)buf.c_str(), block->compress_len);
block->old_len = block->w_off;
_BlockClearBuf(block);
return true;
}
bool CompressStorage::_BlockUnCompress( _Block* block )
{
if (block->cbuf == NULL || block->old_len <= 0 || block->compress_len <= 0)
return false;
if (block->buf != NULL)
return true;
_BlockNewBuf(block, block->old_len);
int uncompress_len = block->old_len;
int ret = uncompress((Bytef *)block->buf, (uLongf *)&uncompress_len, (const Bytef *)block->cbuf, block->compress_len);
if (ret != Z_OK || uncompress_len != block->old_len)
{
assert(false);
return false;
}
return true;
}
bool CompressStorage::_BlockCopy( _Block* block, char* dst, int off, int len )
{
if (off + len > block->cbuf_len)
return false;
memcpy(dst, block->cbuf + off, len);
return true;
}
bool CompressStorage::_BlockMove( _Block* block, _Block* block_src, Pointer* ptr )
{
int len = ptr->len;
if (block->w_off + len > block->cbuf_len)
return false;
memmove(block->buf + block->w_off, block_src->buf + ptr->off, len);
ptr->idx = block->idx;
ptr->off = block->w_off;
block->w_off += len;
return true;
}
int CompressStorage::Insert( char* src, int src_len )
{
// find block
_Block* block = NULL;
if (!blocks.empty())
{
if (_BlockIsSufficient(&blocks.back(), src_len))
block = &blocks.back();
}
// new block
if (block == NULL)
{
blocks.resize(blocks.size() + 1);
block = &blocks.back();
_BlockNew(block, src_len);
block->idx = (int)blocks.size() - 1;
}
// write
// lazy compress
Pointer ret;
ret.idx = block->idx;
_BlockWrite(block, &ret, src, src_len);
// write index
if (keymap.empty())
ret.key = 1;
else
ret.key = keymap.rbegin()->first + 1;
keymap[ret.key] = ret;
return ret.key;
}
void CompressStorage::Remove( int key )
{
//TODO: recycle buf, cbuf
_PointMap::iterator it = keymap.find(key);
if (it == keymap.end())
return;
keymap.erase(it);
}
CompressStorage::Pointer CompressStorage::Query( int key )
{
_PointMap::iterator it = keymap.find(key);
if (it == keymap.end())
{
static Pointer null_ptr = {0};
return null_ptr;
}
return it->second;
}
char* CompressStorage::GetData( int key )
{
Pointer pos = Query(key);
if (pos.key != key)
return NULL;
static int pre_idx = -1;
if (pre_idx != -1 && pre_idx != pos.idx)
_BlockClearBuf(&blocks[pre_idx]);
pre_idx = pos.idx;
return GetData(pos);
}
char* CompressStorage::GetData( Pointer pos )
{
if (pos.key <= 0 || pos.idx < 0 || pos.idx >= (int)blocks.size())
return NULL;
_Block* block = &blocks[pos.idx];
_BlockUnCompress(block);
return block->buf + pos.off;
}
struct pointer_cmp_by_off
{
typedef CompressStorage::Pointer pointer;
bool operator() (const pointer* left, const pointer* right) const
{
return left->off < right->off;
}
bool operator() (const pointer& left, const pointer& right) const
{
return left.off < right.off;
}
};
void CompressStorage::Compress()
{
// ѹËõ
for (_BlockVec::iterator it = blocks.begin(); it != blocks.end(); ++ it)
{
_BlockCompress(&(*it));
}
// ÄÚ´æÕûÀí
// typedef std::vector <Pointer*> PointerVec;
// typedef std::vector <PointerVec> BlockVec;
//
// BlockVec block_info;
// block_info.resize(blocks.size());
// for (_PointMap::iterator it = keymap.begin(); it != keymap.end(); ++ it)
// {
// Pointer& pos = it->second;
// block_info[pos.idx].push_back(&pos);
// }
//
// int new_idx = 0;
// blocks[0].w_off = 0; // re-assign
// for (int i = 0; i < (int)block_info.size(); ++ i)
// {
// std::sort(block_info[i].begin(), block_info[i].end(), pointer_cmp_by_off());
// _Block* block = &blocks[new_idx];
// _Block* block_old = &blocks[i];
//
// for (PointerVec::iterator it = block_info[i].begin(); it != block_info[i].end(); ++ it)
// {
// if (!_BlockMove(block, block_old, *it))
// {
// block = &blocks[++ new_idx];
// block->w_off = 0; // re-assign
// if (!_BlockMove(block, block_old, *it))
// assert(false);
// }
// }
// }
//
// if (blocks[new_idx].w_off == 0)
// -- new_idx;
//
// for (int i = new_idx + 1; i < (int)blocks.size(); ++ i)
// _BlockClear(&blocks[i]);
// blocks.resize(new_idx + 1);
}
int CompressStorage::QueryBytes()
{
int ret = 0;
for (_BlockVec::iterator it = blocks.begin(); it != blocks.end(); ++ it)
ret += it->w_off + it->old_len;
return ret;
}
int CompressStorage::QueryCBytes()
{
int ret = 0;
for (_BlockVec::iterator it = blocks.begin(); it != blocks.end(); ++ it)
ret += it->compress_len;
return ret;
}
void CompressStorage::Clear()
{
keymap.clear();
for (_BlockVec::iterator it = blocks.begin(); it != blocks.end(); ++ it)
_BlockClear(&(*it));
blocks.clear();
}