mirror of
https://github.com/ceph/ceph
synced 2025-03-11 02:39:05 +00:00
compressor/snappy: style cleanup
Signed-off-by: Sage Weil <sage@redhat.com>
This commit is contained in:
parent
765a9d0e75
commit
21971b57ad
@ -21,58 +21,69 @@
|
||||
#include "compressor/Compressor.h"
|
||||
|
||||
class CEPH_BUFFER_API BufferlistSource : public snappy::Source {
|
||||
|
||||
bufferlist::iterator pb;
|
||||
size_t remaining;
|
||||
|
||||
public:
|
||||
explicit BufferlistSource(bufferlist::iterator _pb, size_t _input_len): pb(_pb), remaining(_input_len) {
|
||||
remaining = MIN(remaining, pb.get_remaining());
|
||||
explicit BufferlistSource(bufferlist::iterator _pb, size_t _input_len)
|
||||
: pb(_pb),
|
||||
remaining(_input_len) {
|
||||
remaining = std::min(remaining, (size_t)pb.get_remaining());
|
||||
}
|
||||
virtual ~BufferlistSource() {}
|
||||
virtual size_t Available() const { return remaining; }
|
||||
virtual const char* Peek(size_t* len) {
|
||||
const char* data = NULL;
|
||||
size_t Available() const override {
|
||||
return remaining;
|
||||
}
|
||||
const char *Peek(size_t *len) override {
|
||||
const char *data = NULL;
|
||||
*len = 0;
|
||||
size_t avail = Available();
|
||||
if(avail) {
|
||||
if (avail) {
|
||||
auto ptmp = pb;
|
||||
*len = ptmp.get_ptr_and_advance(avail, &data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
virtual void Skip(size_t n) {
|
||||
void Skip(size_t n) override {
|
||||
assert(n <= remaining);
|
||||
pb.advance(n);
|
||||
remaining -= n;
|
||||
}
|
||||
bufferlist::iterator get_pos() const { return pb; }
|
||||
|
||||
bufferlist::iterator get_pos() const {
|
||||
return pb;
|
||||
}
|
||||
};
|
||||
|
||||
class SnappyCompressor : public Compressor {
|
||||
public:
|
||||
SnappyCompressor() : Compressor("snappy") {}
|
||||
|
||||
int compress(const bufferlist &src, bufferlist &dst) override {
|
||||
BufferlistSource source(const_cast<bufferlist&>(src).begin(), src.length());
|
||||
bufferptr ptr = buffer::create_page_aligned(
|
||||
snappy::MaxCompressedLength(src.length()));
|
||||
snappy::UncheckedByteArraySink sink(ptr.c_str());
|
||||
snappy::Compress(&source, &sink);
|
||||
dst.append(ptr, 0, sink.CurrentDestination()-ptr.c_str());
|
||||
dst.append(ptr, 0, sink.CurrentDestination() - ptr.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int decompress(const bufferlist &src, bufferlist &dst) override {
|
||||
bufferlist::iterator i = const_cast<bufferlist&>(src).begin();
|
||||
return decompress(i, src.length(), dst);
|
||||
}
|
||||
int decompress(bufferlist::iterator &p, size_t compressed_len, bufferlist &dst) override {
|
||||
|
||||
int decompress(bufferlist::iterator &p,
|
||||
size_t compressed_len,
|
||||
bufferlist &dst) override {
|
||||
size_t res_len = 0;
|
||||
// Trick, decompress only need first 32bits buffer
|
||||
bufferlist::const_iterator ptmp = p;
|
||||
bufferlist tmp;
|
||||
ptmp.copy(4, tmp);
|
||||
if (!snappy::GetUncompressedLength(tmp.c_str(), tmp.length(), &res_len))
|
||||
if (!snappy::GetUncompressedLength(tmp.c_str(), tmp.length(), &res_len)) {
|
||||
return -1;
|
||||
}
|
||||
BufferlistSource source(p, compressed_len);
|
||||
bufferptr ptr(res_len);
|
||||
if (snappy::RawUncompress(&source, ptr.c_str())) {
|
||||
|
Loading…
Reference in New Issue
Block a user