]> git.proxmox.com Git - ceph.git/blob - ceph/src/compressor/zstd/ZstdCompressor.h
Import ceph 15.2.8
[ceph.git] / ceph / src / compressor / zstd / ZstdCompressor.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2015 Haomai Wang <haomaiwang@gmail.com>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_ZSTDCOMPRESSOR_H
16 #define CEPH_ZSTDCOMPRESSOR_H
17
18 #define ZSTD_STATIC_LINKING_ONLY
19 #include "zstd/lib/zstd.h"
20
21 #include "include/buffer.h"
22 #include "include/encoding.h"
23 #include "compressor/Compressor.h"
24
25 class ZstdCompressor : public Compressor {
26 public:
27 ZstdCompressor(CephContext *cct) : Compressor(COMP_ALG_ZSTD, "zstd"), cct(cct) {}
28
29 int compress(const bufferlist &src, bufferlist &dst) override {
30 ZSTD_CStream *s = ZSTD_createCStream();
31 ZSTD_initCStream_srcSize(s, cct->_conf->compressor_zstd_level, src.length());
32 auto p = src.begin();
33 size_t left = src.length();
34
35 size_t const out_max = ZSTD_compressBound(left);
36 bufferptr outptr = buffer::create_small_page_aligned(out_max);
37 ZSTD_outBuffer_s outbuf;
38 outbuf.dst = outptr.c_str();
39 outbuf.size = outptr.length();
40 outbuf.pos = 0;
41
42 while (left) {
43 ceph_assert(!p.end());
44 struct ZSTD_inBuffer_s inbuf;
45 inbuf.pos = 0;
46 inbuf.size = p.get_ptr_and_advance(left, (const char**)&inbuf.src);
47 left -= inbuf.size;
48 ZSTD_EndDirective const zed = (left==0) ? ZSTD_e_end : ZSTD_e_continue;
49 size_t r = ZSTD_compressStream2(s, &outbuf, &inbuf, zed);
50 if (ZSTD_isError(r)) {
51 return -EINVAL;
52 }
53 }
54 ceph_assert(p.end());
55
56 ZSTD_freeCStream(s);
57
58 // prefix with decompressed length
59 encode((uint32_t)src.length(), dst);
60 dst.append(outptr, 0, outbuf.pos);
61 return 0;
62 }
63
64 int decompress(const bufferlist &src, bufferlist &dst) override {
65 auto i = std::cbegin(src);
66 return decompress(i, src.length(), dst);
67 }
68
69 int decompress(bufferlist::const_iterator &p,
70 size_t compressed_len,
71 bufferlist &dst) override {
72 if (compressed_len < 4) {
73 return -1;
74 }
75 compressed_len -= 4;
76 uint32_t dst_len;
77 decode(dst_len, p);
78
79 bufferptr dstptr(dst_len);
80 ZSTD_outBuffer_s outbuf;
81 outbuf.dst = dstptr.c_str();
82 outbuf.size = dstptr.length();
83 outbuf.pos = 0;
84 ZSTD_DStream *s = ZSTD_createDStream();
85 ZSTD_initDStream(s);
86 while (compressed_len > 0) {
87 if (p.end()) {
88 return -1;
89 }
90 ZSTD_inBuffer_s inbuf;
91 inbuf.pos = 0;
92 inbuf.size = p.get_ptr_and_advance(compressed_len,
93 (const char**)&inbuf.src);
94 ZSTD_decompressStream(s, &outbuf, &inbuf);
95 compressed_len -= inbuf.size;
96 }
97 ZSTD_freeDStream(s);
98
99 dst.append(dstptr, 0, outbuf.pos);
100 return 0;
101 }
102 private:
103 CephContext *const cct;
104 };
105
106 #endif