File: zstd_test.cpp

package info (click to toggle)
clickhouse 18.16.1%2Bds-7.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 40,292 kB
  • sloc: cpp: 223,075; sql: 21,608; python: 6,596; sh: 4,299; ansic: 3,889; xml: 3,312; perl: 155; makefile: 57; asm: 34
file content (68 lines) | stat: -rw-r--r-- 1,453 bytes parent folder | download | duplicates (3)
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
#include <port/unistd.h>
#include <zstd.h>
#include <vector>
#include <stdexcept>
#include <sys/types.h>


int main(int argc, char ** argv)
{
    bool compress = argc == 1;

    const size_t size = 1048576;
    std::vector<char> src_buf(size);
    std::vector<char> dst_buf;

    size_t pos = 0;
    while (true)
    {
        ssize_t read_res = read(STDIN_FILENO, &src_buf[pos], size - pos);
        if (read_res < 0)
            throw std::runtime_error("Cannot read from stdin");
        if (read_res == 0)
            break;
        pos += read_res;
    }

    src_buf.resize(pos);

    size_t zstd_res;

    if (compress)
    {
        dst_buf.resize(ZSTD_compressBound(src_buf.size()));

        zstd_res = ZSTD_compress(
            &dst_buf[0],
            dst_buf.size(),
            &src_buf[0],
            src_buf.size(),
            1);
    }
    else
    {
        dst_buf.resize(size);

        zstd_res = ZSTD_decompress(
            &dst_buf[0],
            dst_buf.size(),
            &src_buf[0],
            src_buf.size());
    }

    if (ZSTD_isError(zstd_res))
        throw std::runtime_error(ZSTD_getErrorName(zstd_res));

    dst_buf.resize(zstd_res);

    pos = 0;
    while (pos < dst_buf.size())
    {
        ssize_t write_res = write(STDOUT_FILENO, &dst_buf[pos], dst_buf.size());
        if (write_res <= 0)
            throw std::runtime_error("Cannot write to stdout");
        pos += write_res;
    }

    return 0;
}