1 | /* |
2 | * flat.h |
3 | * |
4 | * Copyright (C) 2019 Aerospike, Inc. |
5 | * |
6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
7 | * license agreements. |
8 | * |
9 | * This program is free software: you can redistribute it and/or modify it under |
10 | * the terms of the GNU Affero General Public License as published by the Free |
11 | * Software Foundation, either version 3 of the License, or (at your option) any |
12 | * later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
17 | * details. |
18 | * |
19 | * You should have received a copy of the GNU Affero General Public License |
20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
21 | */ |
22 | |
23 | #pragma once |
24 | |
25 | //========================================================== |
26 | // Includes. |
27 | // |
28 | |
29 | #include <stdint.h> |
30 | |
31 | #include "aerospike/as_atomic.h" |
32 | #include "citrusleaf/cf_digest.h" |
33 | |
34 | |
35 | //========================================================== |
36 | // Forward declarations. |
37 | // |
38 | |
39 | struct as_bin_s; |
40 | struct as_namespace_s; |
41 | struct as_remote_record_s; |
42 | struct as_storage_rd_s; |
43 | |
44 | |
45 | //========================================================== |
46 | // Typedefs & constants. |
47 | // |
48 | |
49 | #define AS_FLAT_MAGIC 0x037AF201 // changed for storage version 3 |
50 | |
51 | // Per-record mandatory metadata on device. |
52 | typedef struct as_flat_record_s { |
53 | uint32_t magic; |
54 | |
55 | // offset: 4 |
56 | uint32_t n_rblocks: 19; // unused if storage-engine memory |
57 | uint32_t has_void_time: 1; |
58 | uint32_t has_set: 1; |
59 | uint32_t has_key: 1; |
60 | uint32_t has_bins: 1; // i.e. is live |
61 | uint32_t is_compressed: 1; |
62 | uint32_t unused: 2; |
63 | uint32_t tree_id: 6; // for local storage only |
64 | |
65 | // offset: 8 |
66 | cf_digest keyd; |
67 | |
68 | // offset: 28 |
69 | uint64_t last_update_time: 40; |
70 | uint64_t generation: 16; |
71 | |
72 | // final size: 35 |
73 | uint8_t data[]; |
74 | } __attribute__ ((__packed__)) as_flat_record; |
75 | |
76 | typedef enum { |
77 | AS_COMPRESSION_NONE, |
78 | AS_COMPRESSION_LZ4, |
79 | AS_COMPRESSION_SNAPPY, |
80 | AS_COMPRESSION_ZSTD, |
81 | |
82 | AS_COMPRESSION_LAST_PLUS_1 |
83 | } as_compression_method; |
84 | |
85 | #define NS_COMPRESSION() ({ \ |
86 | as_compression_method meth = as_load_int32(&ns->storage_compression); \ |
87 | (meth == AS_COMPRESSION_NONE ? "none" : \ |
88 | (meth == AS_COMPRESSION_LZ4 ? "lz4" : \ |
89 | (meth == AS_COMPRESSION_SNAPPY ? "snappy" : \ |
90 | (meth == AS_COMPRESSION_ZSTD ? "zstd" : \ |
91 | "illegal")))); \ |
92 | }) |
93 | |
94 | #define NS_COMPRESSION_LEVEL() ({ \ |
95 | uint32_t level = as_load_uint32(&ns->storage_compression_level); \ |
96 | (ns->storage_compression == AS_COMPRESSION_ZSTD && level == 0 ? \ |
97 | 9 : level); \ |
98 | }) |
99 | |
100 | // Compression metadata - relevant for enterprise only. |
101 | typedef struct ssd_comp_meta_s { |
102 | as_compression_method method; |
103 | uint32_t orig_sz; |
104 | uint32_t comp_sz; |
105 | } as_flat_comp_meta; |
106 | |
107 | // Per-record optional metadata container. |
108 | typedef struct as_flat_opt_meta_s { |
109 | uint32_t void_time; |
110 | uint32_t set_name_len; |
111 | const char* set_name; |
112 | uint32_t key_size; |
113 | const uint8_t* key; |
114 | uint32_t n_bins; |
115 | as_flat_comp_meta cm; |
116 | } as_flat_opt_meta; |
117 | |
118 | #define RBLOCK_SIZE 16 // 2^4 |
119 | #define LOG_2_RBLOCK_SIZE 4 // must be in sync with RBLOCK_SIZE |
120 | |
121 | |
122 | //========================================================== |
123 | // Public API. |
124 | // |
125 | |
126 | void as_flat_pickle_record(struct as_storage_rd_s* rd); |
127 | uint32_t as_flat_record_size(const struct as_storage_rd_s* rd); |
128 | void as_flat_pack_record(const struct as_storage_rd_s* rd, uint32_t n_rblocks, as_flat_record* flat); |
129 | |
130 | as_flat_record* as_flat_compress_bins_and_pack_record(const struct as_storage_rd_s* rd, uint32_t max_orig_sz, uint32_t* flat_sz); |
131 | |
132 | bool as_flat_unpack_remote_record_meta(struct as_namespace_s* ns, struct as_remote_record_s* rr); |
133 | const uint8_t* as_flat_unpack_record_meta(const as_flat_record* flat, const uint8_t* end, struct as_flat_opt_meta_s* opt_meta, bool single_bin); |
134 | int as_flat_unpack_remote_bins(struct as_remote_record_s* rr, struct as_bin_s* bins); |
135 | int as_flat_unpack_bins(struct as_namespace_s* ns, const uint8_t* at, const uint8_t* end, uint16_t n_bins, struct as_bin_s* bins); |
136 | bool as_flat_check_packed_bins(const uint8_t* at, const uint8_t* end, uint32_t n_bins, bool single_bin); |
137 | |
138 | uint32_t as_flat_orig_pickle_size(const struct as_remote_record_s* rr, uint32_t pickle_sz); |
139 | bool as_flat_decompress_bins(const as_flat_comp_meta* cm, struct as_storage_rd_s* rd); |
140 | bool as_flat_decompress_buffer(const as_flat_comp_meta* cm, uint32_t max_orig_sz, const uint8_t** at, const uint8_t** end); |
141 | |
142 | // Round size in bytes up to a multiple of rblock size. |
143 | static inline uint32_t |
144 | SIZE_UP_TO_RBLOCK_SIZE(uint32_t size) { |
145 | return (size + (RBLOCK_SIZE - 1)) & -RBLOCK_SIZE; |
146 | } |
147 | |
148 | // Convert size in bytes to n_rblocks. |
149 | static inline uint32_t |
150 | SIZE_TO_N_RBLOCKS(uint32_t size) { |
151 | return ((size + (RBLOCK_SIZE - 1)) >> LOG_2_RBLOCK_SIZE) - 1; |
152 | } |
153 | |
154 | // Convert size in bytes to n_rblocks - size must be a multiple of rblock size. |
155 | static inline uint32_t |
156 | ROUNDED_SIZE_TO_N_RBLOCKS(uint32_t size) { |
157 | return (size >> LOG_2_RBLOCK_SIZE) - 1; |
158 | } |
159 | |
160 | // Convert n_rblocks to size in bytes. |
161 | static inline uint32_t |
162 | N_RBLOCKS_TO_SIZE(uint32_t n_rblocks) { |
163 | return (n_rblocks + 1) << LOG_2_RBLOCK_SIZE; |
164 | } |
165 | |
166 | |
167 | //========================================================== |
168 | // Private API - for enterprise separation only. |
169 | // |
170 | |
171 | uint8_t* flatten_record_meta(const struct as_storage_rd_s* rd, uint32_t n_rblocks, const as_flat_comp_meta* cm, as_flat_record* flat); |
172 | uint16_t flatten_bins(const struct as_storage_rd_s* rd, uint8_t* buf, uint32_t* sz); |
173 | |
174 | uint8_t* flatten_compression_meta(const as_flat_comp_meta* cm, as_flat_record* flat, uint8_t* buf); |
175 | const uint8_t* unflatten_compression_meta(const as_flat_record* flat, const uint8_t* at, const uint8_t* end, as_flat_comp_meta* cm); |
176 | |