aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/compressor.h
blob: 0c961136d9d2dcc35b74b5a028a119105aef8d30 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "compressionconfig.h"
#include "buffer.h"
#include "alloc.h"

namespace vespalib { class DataBuffer; }

namespace vespalib::compression {

class ICompressor
{
public:
    virtual ~ICompressor() { }
    virtual bool process(CompressionConfig config, const void * input, size_t inputLen, void * output, size_t & outputLen) = 0;
    virtual bool unprocess(const void * input, size_t inputLen, void * output, size_t & outputLen) = 0;
    virtual size_t adjustProcessLen(uint16_t options, size_t len)   const = 0;
};

/**
 * Will try to compress a buffer according to the config. If the criteria can not
 * be met it will return NONE and dest will get the input buffer.
 * @param compression is config for how to compress and what criteria to meet.
 * @param org is the original input buffer.
 * @param dest is the destination buffer. The compressed data will be appended unless allowSwap is true
 *             and it is not compressable. Then it will be swapped in.
 * @param allowSwap will tell it the data must be appended or if it can be swapped in if it is uncompressable or config is NONE.
 */
CompressionConfig::Type compress(CompressionConfig::Type compression, const ConstBufferRef & org, DataBuffer & dest, bool allowSwap);
CompressionConfig::Type compress(CompressionConfig compression, const vespalib::ConstBufferRef & org, vespalib::DataBuffer & dest, bool allowSwap);

/**
 * Will try to decompress a buffer according to the config.
 * be met it will return NONE and dest will get the input buffer.
 * @param compression is the compression type used for the buffer.
 * @param uncompressedLen is the length of the uncompressed data.
 * @param org is the original input buffer.
 * @param dest is the destination buffer. The decompressed data will be
 *             appended unless allowSwap is true and compression is NONE.
 *             Then it will be swapped in.
 * @param allowSwap will tell it the data must be appended or if it can be swapped in if compression type is NONE.
 */
void decompress(CompressionConfig::Type compression, size_t uncompressedLen, const vespalib::ConstBufferRef & org, vespalib::DataBuffer & dest, bool allowSwap);

size_t computeMaxCompressedsize(CompressionConfig::Type type, size_t uncompressedSize);

//-----------------------------------------------------------------------------

/**
 * Simple utility used to compress data according to a compression
 * configuration.
 **/
class Compress {
private:
    alloc::Alloc            _space;
    CompressionConfig::Type _type;
    const char             *_data;
    size_t                  _size;
public:
    Compress(CompressionConfig config, const char *uncompressed_data, size_t uncompressed_size);
    CompressionConfig::Type type() const { return _type; }
    const char *data() const { return _data; }
    size_t size() const { return _size; }
};

/**
 * Simple utility used to decompress data using additional information
 * about compression type and uncompressed size.
 **/
class Decompress {
private:
    alloc::Alloc _space;
    const char  *_data;
    size_t       _size;
public:
    Decompress(CompressionConfig::Type type, size_t uncompressed_size,
               const char *compressed_data, size_t compressed_size);
    const char *data() const { return _data; }
    size_t size() const { return _size; }
};

//-----------------------------------------------------------------------------

}