1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41#include "ft/serialize/compress.h"
42
43// TODO: Clean this abstraciton up
44static const int max_sub_blocks = 8;
45static const int target_sub_block_size = 512 * 1024;
46static const int max_basement_nodes = 32;
47static const int max_basement_node_uncompressed_size = 256 * 1024;
48static const int max_basement_node_compressed_size = 64 * 1024;
49
50struct sub_block {
51 void *uncompressed_ptr;
52 uint32_t uncompressed_size;
53
54 void *compressed_ptr;
55 uint32_t compressed_size; // real compressed size
56 uint32_t compressed_size_bound; // estimated compressed size
57
58 uint32_t xsum; // sub block checksum
59};
60typedef struct sub_block *SUB_BLOCK;
61
62struct stored_sub_block {
63 uint32_t uncompressed_size;
64 uint32_t compressed_size;
65 uint32_t xsum;
66};
67
68void sub_block_init(SUB_BLOCK);
69SUB_BLOCK sub_block_creat(void);
70
71// get the size of the compression header
72size_t
73sub_block_header_size(int n_sub_blocks);
74
75void
76set_compressed_size_bound(struct sub_block *se, enum toku_compression_method method);
77
78// get the sum of the sub block compressed bound sizes
79size_t
80get_sum_compressed_size_bound(int n_sub_blocks, struct sub_block sub_block[], enum toku_compression_method method);
81
82// get the sum of the sub block uncompressed sizes
83size_t
84get_sum_uncompressed_size(int n_sub_blocks, struct sub_block sub_block[]);
85
86// Choose n_sub_blocks and sub_block_size such that the product is >= total_size and the sub_block_size is at
87// least >= the target_sub_block_size.
88int
89choose_sub_block_size(int total_size, int n_sub_blocks_limit, int *sub_block_size_ret, int *n_sub_blocks_ret);
90
91int
92choose_basement_node_size(int total_size, int *sub_block_size_ret, int *n_sub_blocks_ret);
93
94void
95set_all_sub_block_sizes(int total_size, int sub_block_size, int n_sub_blocks, struct sub_block sub_block[]);
96
97// find the index of the first sub block that contains the offset
98// Returns the index if found, else returns -1
99int
100get_sub_block_index(int n_sub_blocks, struct sub_block sub_block[], size_t offset);
101
102#include "workset.h"
103
104struct compress_work {
105 struct work base;
106 enum toku_compression_method method;
107 struct sub_block *sub_block;
108};
109
110void
111compress_work_init(struct compress_work *w, enum toku_compression_method method, struct sub_block *sub_block);
112
113uint32_t
114compress_nocrc_sub_block(
115 struct sub_block *sub_block,
116 void* sb_compressed_ptr,
117 uint32_t cs_bound,
118 enum toku_compression_method method
119 );
120
121void
122compress_sub_block(struct sub_block *sub_block, enum toku_compression_method method);
123
124void *
125compress_worker(void *arg);
126
127size_t
128compress_all_sub_blocks(int n_sub_blocks, struct sub_block sub_block[], char *uncompressed_ptr, char *compressed_ptr, int num_cores, struct toku_thread_pool *pool, enum toku_compression_method method);
129
130struct decompress_work {
131 struct work base;
132 void *compress_ptr;
133 void *uncompress_ptr;
134 uint32_t compress_size;
135 uint32_t uncompress_size;
136 uint32_t xsum;
137 int error;
138};
139
140// initialize the decompression work
141void
142decompress_work_init(struct decompress_work *dw,
143 void *compress_ptr, uint32_t compress_size,
144 void *uncompress_ptr, uint32_t uncompress_size,
145 uint32_t xsum);
146
147// decompress one block
148int
149decompress_sub_block(void *compress_ptr, uint32_t compress_size, void *uncompress_ptr, uint32_t uncompress_size, uint32_t expected_xsum);
150
151// decompress blocks until there is no more work to do
152void *
153decompress_worker(void *arg);
154
155// decompress all sub blocks from the compressed_data buffer to the uncompressed_data buffer
156// Returns 0 if success, otherwise an error
157int
158decompress_all_sub_blocks(int n_sub_blocks, struct sub_block sub_block[], unsigned char *compressed_data, unsigned char *uncompressed_data, int num_cores, struct toku_thread_pool *pool);
159
160extern int verbose_decompress_sub_block;
161