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
|
#include "clickhouse_config.h"
#if USE_SNAPPY
#include <cstring>
#include <snappy.h>
#include <Common/ErrorCodes.h>
#include "SnappyWriteBuffer.h"
namespace DB
{
namespace ErrorCodes
{
extern const int SNAPPY_COMPRESS_FAILED;
}
SnappyWriteBuffer::SnappyWriteBuffer(std::unique_ptr<WriteBuffer> out_, size_t buf_size, char * existing_memory, size_t alignment)
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment), out(std::move(out_))
{
}
SnappyWriteBuffer::~SnappyWriteBuffer()
{
finish();
}
void SnappyWriteBuffer::nextImpl()
{
if (!offset())
{
return;
}
const char * in_data = reinterpret_cast<const char *>(working_buffer.begin());
size_t in_available = offset();
uncompress_buffer.append(in_data, in_available);
}
void SnappyWriteBuffer::finish()
{
if (finished)
return;
try
{
finishImpl();
out->finalize();
finished = true;
}
catch (...)
{
/// Do not try to flush next time after exception.
out->position() = out->buffer().begin();
finished = true;
throw;
}
}
void SnappyWriteBuffer::finishImpl()
{
next();
bool success = snappy::Compress(uncompress_buffer.data(), uncompress_buffer.size(), &compress_buffer);
if (!success)
{
throw Exception(ErrorCodes::SNAPPY_COMPRESS_FAILED, "snappy compress failed: ");
}
char * in_data = compress_buffer.data();
size_t in_available = compress_buffer.size();
char * out_data = nullptr;
size_t out_capacity = 0;
size_t len = 0;
while (in_available > 0)
{
out->nextIfAtEnd();
out_data = out->position();
out_capacity = out->buffer().end() - out->position();
len = in_available > out_capacity ? out_capacity : in_available;
memcpy(out_data, in_data, len);
in_data += len;
in_available -= len;
out->position() += len;
}
}
}
#endif
|