]> git.proxmox.com Git - ceph.git/blame - ceph/src/compressor/snappy/SnappyCompressor.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / compressor / snappy / SnappyCompressor.h
CommitLineData
7c673cae
FG
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_SNAPPYCOMPRESSOR_H
16#define CEPH_SNAPPYCOMPRESSOR_H
17
18#include <snappy.h>
19#include <snappy-sinksource.h>
11fdf7f2 20#include "common/config.h"
7c673cae 21#include "compressor/Compressor.h"
31f18b77 22#include "include/buffer.h"
7c673cae
FG
23
24class CEPH_BUFFER_API BufferlistSource : public snappy::Source {
11fdf7f2 25 bufferlist::const_iterator pb;
7c673cae
FG
26 size_t remaining;
27
28 public:
11fdf7f2 29 explicit BufferlistSource(bufferlist::const_iterator _pb, size_t _input_len)
7c673cae
FG
30 : pb(_pb),
31 remaining(_input_len) {
32 remaining = std::min(remaining, (size_t)pb.get_remaining());
33 }
34 size_t Available() const override {
35 return remaining;
36 }
37 const char *Peek(size_t *len) override {
38 const char *data = NULL;
39 *len = 0;
40 size_t avail = Available();
41 if (avail) {
42 auto ptmp = pb;
43 *len = ptmp.get_ptr_and_advance(avail, &data);
44 }
45 return data;
46 }
47 void Skip(size_t n) override {
11fdf7f2 48 ceph_assert(n <= remaining);
9f95a23c 49 pb += n;
7c673cae
FG
50 remaining -= n;
51 }
52
11fdf7f2 53 bufferlist::const_iterator get_pos() const {
7c673cae
FG
54 return pb;
55 }
56};
57
58class SnappyCompressor : public Compressor {
59 public:
11fdf7f2
TL
60 SnappyCompressor(CephContext* cct) : Compressor(COMP_ALG_SNAPPY, "snappy") {
61#ifdef HAVE_QATZIP
62 if (cct->_conf->qat_compressor_enabled && qat_accel.init("snappy"))
63 qat_enabled = true;
64 else
65 qat_enabled = false;
66#endif
67 }
7c673cae
FG
68
69 int compress(const bufferlist &src, bufferlist &dst) override {
11fdf7f2
TL
70#ifdef HAVE_QATZIP
71 if (qat_enabled)
72 return qat_accel.compress(src, dst);
73#endif
7c673cae 74 BufferlistSource source(const_cast<bufferlist&>(src).begin(), src.length());
11fdf7f2 75 bufferptr ptr = buffer::create_small_page_aligned(
7c673cae
FG
76 snappy::MaxCompressedLength(src.length()));
77 snappy::UncheckedByteArraySink sink(ptr.c_str());
78 snappy::Compress(&source, &sink);
79 dst.append(ptr, 0, sink.CurrentDestination() - ptr.c_str());
80 return 0;
81 }
82
83 int decompress(const bufferlist &src, bufferlist &dst) override {
11fdf7f2
TL
84#ifdef HAVE_QATZIP
85 if (qat_enabled)
86 return qat_accel.decompress(src, dst);
87#endif
88 auto i = src.begin();
7c673cae
FG
89 return decompress(i, src.length(), dst);
90 }
91
11fdf7f2 92 int decompress(bufferlist::const_iterator &p,
7c673cae
FG
93 size_t compressed_len,
94 bufferlist &dst) override {
11fdf7f2
TL
95#ifdef HAVE_QATZIP
96 if (qat_enabled)
97 return qat_accel.decompress(p, compressed_len, dst);
98#endif
7c673cae
FG
99 snappy::uint32 res_len = 0;
100 BufferlistSource source_1(p, compressed_len);
101 if (!snappy::GetUncompressedLength(&source_1, &res_len)) {
102 return -1;
103 }
104 BufferlistSource source_2(p, compressed_len);
105 bufferptr ptr(res_len);
106 if (snappy::RawUncompress(&source_2, ptr.c_str())) {
107 p = source_2.get_pos();
108 dst.append(ptr);
109 return 0;
110 }
111 return -2;
112 }
113};
114
115#endif