]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Haomai Wang <haomaiwang@gmail.com>
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.
15 #ifndef CEPH_ZSTDCOMPRESSOR_H
16 #define CEPH_ZSTDCOMPRESSOR_H
18 #define ZSTD_STATIC_LINKING_ONLY
19 #include "zstd/lib/zstd.h"
21 #include "include/buffer.h"
22 #include "include/encoding.h"
23 #include "compressor/Compressor.h"
25 class ZstdCompressor
: public Compressor
{
27 ZstdCompressor(CephContext
*cct
) : Compressor(COMP_ALG_ZSTD
, "zstd"), cct(cct
) {}
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());
33 size_t left
= src
.length();
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();
43 ceph_assert(!p
.end());
44 struct ZSTD_inBuffer_s inbuf
;
46 inbuf
.size
= p
.get_ptr_and_advance(left
, (const char**)&inbuf
.src
);
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
)) {
58 // prefix with decompressed length
59 encode((uint32_t)src
.length(), dst
);
60 dst
.append(outptr
, 0, outbuf
.pos
);
64 int decompress(const bufferlist
&src
, bufferlist
&dst
) override
{
65 auto i
= std::cbegin(src
);
66 return decompress(i
, src
.length(), dst
);
69 int decompress(bufferlist::const_iterator
&p
,
70 size_t compressed_len
,
71 bufferlist
&dst
) override
{
72 if (compressed_len
< 4) {
79 bufferptr
dstptr(dst_len
);
80 ZSTD_outBuffer_s outbuf
;
81 outbuf
.dst
= dstptr
.c_str();
82 outbuf
.size
= dstptr
.length();
84 ZSTD_DStream
*s
= ZSTD_createDStream();
86 while (compressed_len
> 0) {
90 ZSTD_inBuffer_s inbuf
;
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
;
99 dst
.append(dstptr
, 0, outbuf
.pos
);
103 CephContext
*const cct
;