bable 0.1.0

Badger's high performance sst table implementation
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use super::*;
use crate::{
    bloom::{bloom_bits_per_key, hash, Filter},
    sync::{AtomicU32, Ordering},
};
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::{ops::Deref, ptr::NonNull};
use vpb::{
    checksum::Checksumer,
    compression::Compressor,
    encrypt::{random_iv, Encryptor},
    kvstructs::{Key, KeyExt, Value, ValueExt},
    BlockOffset, Checksum, Compression, Encryption, Marshaller, TableIndex,
};
use zallocator::Buffer;

#[cfg(feature = "std")]
mod standard;

#[cfg(not(feature = "std"))]
mod no_std;

pub const MAX_ALLOCATOR_INITIAL_SIZE: usize = 256 << 20;

/// When a block is encrypted, it's length increases. We add 256 bytes of padding to
/// handle cases when block size increases. This is an approximate number.
const PADDING: usize = 256;

pub struct BuildData {
    alloc: Allocator,
    opts: RefCounter<Options>,
    block_list: Vec<BBlock>,
    index: Vec<u8>,
    checksum: Vec<u8>,
    checksum_size: u32,
    size: u32,
}

/// BBlock represents a block that is being compressed/encrypted in the background.
#[derive(Debug, Clone)]
#[repr(transparent)]
pub(crate) struct BBlock {
    inner: RefCounter<NonNull<BBlockInner>>,
}

/// Safety: we never concurrently read/write data in `BBlock`
unsafe impl Send for BBlock {}
unsafe impl Sync for BBlock {}

struct BBlockInner {
    data: Buffer,
    /// Base key for the current block.
    base_key: Key,
    /// Offsets of entries present in current block.
    entry_offsets: Vec<u32>,
    /// Points to the end offset of the block.
    end: usize,
}

impl BBlockInner {
    #[inline]
    pub(crate) const fn new(buf: Buffer) -> Self {
        Self {
            data: buf,
            base_key: Key::new(),
            entry_offsets: Vec::new(),
            end: 0,
        }
    }
}

impl Drop for BBlock {
    fn drop(&mut self) {
        if RefCounter::count(&self.inner) == 1 && !self.inner.as_ptr().is_null() {
            // Safety: the ptr will not be freed at this time.
            unsafe {
                core::ptr::drop_in_place(self.inner.as_ptr());
            }
        }
    }
}

impl Default for BBlock {
    fn default() -> Self {
        Self::dangling()
    }
}

impl BBlock {
    #[inline(always)]
    pub(crate) fn new(buf: Buffer) -> Self {
        Self {
            inner: RefCounter::new(unsafe {
                NonNull::new_unchecked(Box::into_raw(Box::new(BBlockInner::new(buf))))
            }),
        }
    }

    #[inline(always)]
    pub(crate) fn dangling() -> Self {
        Self {
            inner: RefCounter::new(unsafe { NonNull::new_unchecked(core::ptr::null_mut()) }),
        }
    }

    #[inline(always)]
    pub(crate) fn data(&self) -> &Buffer {
        &self.inner().data
    }

    #[inline(always)]
    pub(crate) fn set_data(&self, data: Buffer) {
        self.inner_mut().data = data;
    }

    #[inline(always)]
    pub(crate) fn increase_end(&self, add: usize) -> usize {
        let end = self.inner_mut().end;
        self.inner_mut().end += add;
        end
    }

    #[inline(always)]
    pub(crate) fn end(&self) -> usize {
        self.inner().end
    }

    #[inline(always)]
    pub(crate) fn set_end(&self, end: usize) {
        self.inner_mut().end = end;
    }

    #[inline(always)]
    pub(crate) fn base_key(&self) -> &Key {
        &self.inner().base_key
    }

    #[inline(always)]
    pub(crate) fn set_base_key(&self, key: Key) {
        self.inner_mut().base_key = key;
    }

    #[inline(always)]
    pub(crate) fn entry_offsets(&self) -> &[u32] {
        &self.inner().entry_offsets
    }

    #[inline(always)]
    pub(crate) fn len(&self) -> usize {
        self.inner().entry_offsets.len()
    }

    #[inline(always)]
    pub(crate) fn push_entry_offset(&self, offset: u32) {
        self.inner_mut().entry_offsets.push(offset);
    }

    #[inline(always)]
    fn inner(&self) -> &BBlockInner {
        // Safety: the ptr will not be freed at this time.
        unsafe { &*(self.inner.deref().as_ptr()) }
    }

    #[inline(always)]
    #[allow(clippy::mut_from_ref)]
    fn inner_mut(&self) -> &mut BBlockInner {
        // Safety: the ptr will not be freed at this time.
        unsafe { &mut *(self.inner.deref().as_ptr()) }
    }
}

/// Builder is used in building a table.
///
/// If you do not want to compress or encrypt data,
/// Please use [`SimpleBuilder`], it is faster than `Builder`
///
/// [`SimpleBuilder`]: struct.SimpleBuilder.html
pub struct Builder {
    /// Typically tens or hundreds of meg. This is for one single file.
    alloc: Allocator,
    cur_block: BBlock,
    #[cfg(feature = "std")]
    compressed_size: RefCounter<AtomicU32>,
    #[cfg(not(feature = "std"))]
    compressed_size: AtomicU32,
    uncompressed_size: AtomicU32,

    len_offsets: u32,
    key_hashes: Vec<u32>,
    opts: RefCounter<super::options::Options>,
    max_version: u64,
    on_disk_size: u32,
    stale_data_size: usize,

    #[cfg(feature = "std")]
    wg: Option<crossbeam_utils::sync::WaitGroup>,
    #[cfg(feature = "std")]
    block_tx: Option<crossbeam_channel::Sender<BBlock>>,
    block_list: Vec<BBlock>,
}

impl TableBuilder for Builder {
    type TableData = BuildData;

    fn new(opts: RefCounter<Options>) -> Result<Self>
    where
        Self: Sized,
    {
        Builder::new_in(opts)
    }

    fn options(&self) -> RefCounter<Options> {
        self.opts.clone()
    }

    /// The same as `insert` function but it also increments the internal
    /// `stale_data_size` counter. This value will be used to prioritize this table for
    /// compaction.
    fn insert_stale(&mut self, key: &Key, val: &Value, value_len: u32) {
        // Rough estimate based on how much space it will occupy in the SST.
        self.stale_data_size += key.len()
            + val.len()
            + 4 // entry offset
            + 4; // header size

        self.insert_in(key, val, value_len, true)
    }

    /// Inserts a key-value pair to the block.
    fn insert(&mut self, key: &Key, val: &Value, value_len: u32) {
        self.insert_in(key, val, value_len, false)
    }

    /// Returns whether builder is empty.
    #[inline]
    fn is_empty(&self) -> bool {
        self.key_hashes.len() == 0
    }

    /// Returns the number of blocks in the builder.
    #[inline]
    fn len(&self) -> usize {
        self.key_hashes.len()
    }

    // TODO: vvv this was the comment on ReachedCapacity.
    // FinalSize returns the *rough* final size of the array, counting the header which is
    // not yet written.
    // TODO: Look into why there is a discrepancy. I suspect it is because of Write(empty, empty)
    // at the end. The diff can vary.
    fn reached_capacity(&self) -> bool {
        // If encryption/compression is enabled then use the compresssed size.
        let mut sum_block_size = self.compressed_size.load(Ordering::SeqCst);
        if self.opts.compression().is_none() && self.opts.encryption().is_none() {
            sum_block_size = self.uncompressed_size.load(Ordering::SeqCst);
        }

        let block_size = sum_block_size
            + (self.cur_block.len() * core::mem::size_of::<u32>()) as u32
            + 4 // count of all entry offsets
            + Checksum::ENCODED_SIZE as u32; // checksum;

        let estimated_size = block_size
            + 4 // index length
            + self.len_offsets;

        (estimated_size as u64) > self.opts.table_capacity()
    }

    fn build(mut self) -> Result<Option<BuildData>> {
        self.finish_block(false);
        #[cfg(feature = "std")]
        {
            if self.block_tx.is_some() {
                self.block_tx.take();
                // Wait for block handler to finish.
                self.wg.unwrap().wait();
            }
        }

        if self.block_list.is_empty() {
            return Ok(None);
        }

        let key_hashes_len = self.key_hashes.len();
        let mut tbi = TableIndex::new();
        if self.opts.bloom_ratio() > 0.0 {
            let bits = bloom_bits_per_key(key_hashes_len, self.opts.bloom_ratio());
            tbi.bloom_filter = Filter::new(self.key_hashes.as_slice(), bits).into_bytes();
        }

        let mut num_entries = 0;
        let mut data_sz = 0;
        tbi.offsets = self
            .block_list
            .iter()
            .map(|bblk| {
                num_entries += bblk.len();
                let end = bblk.end() as u32;
                let bo = BlockOffset {
                    key: bblk.base_key().deref().clone(),
                    offset: data_sz,
                    len: end,
                };
                data_sz += end;
                bo
            })
            .collect();

        self.on_disk_size += data_sz;
        tbi.uncompressed_size = self.uncompressed_size.load(Ordering::SeqCst);
        tbi.key_count = key_hashes_len as u32;
        tbi.max_version = self.max_version;
        tbi.stale_data_size = self.stale_data_size as u32;

        let data = tbi.marshal();
        let encryption = self.opts.encryption();

        if encryption.is_some() {
            let index = data.as_slice().encrypt_to_vec(
                encryption.secret(),
                &random_iv(),
                encryption.algorithm(),
            )?;
            // Build checksum for the index.
            let cks = index
                .as_slice()
                .checksum(Options::checksum(&self.opts))
                .marshal();
            let cks_size = cks.len();
            let size = data_sz + (index.len() + cks_size + 4 + 4) as u32;

            Ok(Some(BuildData {
                alloc: self.alloc,
                opts: self.opts,
                block_list: self.block_list,
                index,
                checksum: cks,
                checksum_size: cks_size as u32,
                size,
            }))
        } else {
            let cks = data
                .as_slice()
                .checksum(Options::checksum(&self.opts))
                .marshal();

            let cks_size = cks.len();
            let size = data_sz + (data.len() + cks_size + 4 + 4) as u32;
            Ok(Some(BuildData {
                alloc: self.alloc,
                opts: self.opts,
                block_list: self.block_list,
                index: data,
                checksum: cks,
                checksum_size: cks_size as u32,
                size,
            }))
        }
    }
}

impl Builder {
    #[inline]
    fn insert_in(&mut self, key: &Key, val: &Value, value_len: u32, is_stale: bool) {
        let val_encoded_size = val.encoded_size();
        if self.should_finish_block(key.len(), val_encoded_size) {
            if is_stale {
                // This key will be added to tableIndex and it is stale.
                self.stale_data_size += key.len()
                    + 4 // len
                    + 4; // offset
            }

            self.finish_block(true)
        }

        self.insert_helper(key, val, value_len, val_encoded_size)
    }

    /// # Structure of Block.
    ///
    /// ```text
    /// +-------------------+---------------------+--------------------+--------------+------------------+
    /// | Entry1            | Entry2              | Entry3             | Entry4       | Entry5           |
    /// +-------------------+---------------------+--------------------+--------------+------------------+
    /// | Entry6            | ...                 | ...                | ...          | EntryN           |
    /// +-------------------+---------------------+--------------------+--------------+------------------+
    /// | Block Meta(contains list of offsets used| Block Meta Size    | Block        | Checksum Size    |
    /// | to perform binary search in the block)  | (4 Bytes)          | Checksum     | (4 Bytes)        |
    /// +-----------------------------------------+--------------------+--------------+------------------+
    /// ```
    /// In case the data is encrypted, the "IV" is added to the end of the block.
    fn finish_block(&mut self, start_new: bool) {
        let entries = self.cur_block.entry_offsets();
        let entries_len = entries.len();
        if entries_len == 0 {
            return;
        }

        let cur_block_end = self.cur_block.end();

        self.append(u32_slice_to_bytes(entries));
        self.append(&(entries_len as u32).to_be_bytes());

        // Append the block checksum and its length.
        let checksum = (&self.cur_block.data().as_slice()[..cur_block_end])
            .checksum(Options::checksum(&self.opts))
            .marshal();
        self.append(&checksum);
        self.append(&(checksum.len() as u32).to_be_bytes());

        self.uncompressed_size
            .fetch_add(cur_block_end as u32, Ordering::SeqCst);

        // Add length of baseKey (rounded to next multiple of 4 because of alignment).
        // Add another 40 Bytes, these additional 40 bytes consists of
        // 12 bytes of metadata of flatbuffer
        // 8 bytes for Key in flat buffer
        // 8 bytes for offset
        // 8 bytes for the len
        // 4 bytes for the size of slice while SliceAllocate
        self.len_offsets +=
            (((self.cur_block.base_key().len() as f64) / 4f64) * 4f64).ceil() as u32 + 40;

        self.block_list.push(self.cur_block.clone());

        let old_block = if start_new {
            core::mem::replace(
                &mut self.cur_block,
                BBlock::new(
                    self.alloc
                        .allocate_unchecked((self.opts.block_size() + PADDING) as u64),
                ),
            )
        } else {
            core::mem::take(&mut self.cur_block)
        };
        #[cfg(feature = "std")]
        {
            // If compression/encryption is enabled, we need to send the block to the blockChan.
            if let Some(ref tx) = self.block_tx {
                if let Err(e) = tx.send(old_block) {
                    #[cfg(feature = "tracing")]
                    tracing::error!(target: "table_builder", info = "fail send bblock to the processor", err = ?e);
                    panic!("fail send bblock to the processor: {e}");
                }
            }
        }

        // if no_std, encrypt and compress data in current thread
        #[cfg(not(feature = "std"))]
        {
            if self.opts.compression().is_some() || self.opts.encryption().is_some() {
                Self::process_block(
                    &self.alloc,
                    old_block,
                    &self.compressed_size,
                    self.opts.compression(),
                    self.opts.encryption(),
                )
            }
        }
    }

    fn should_finish_block(&self, key_size: usize, val_encoded_size: u32) -> bool {
        let current_block_end = self.cur_block.end() as u32;
        let len = self.cur_block.len();
        // If there is no entry till now, we will return false.
        if len == 0 {
            return false;
        }

        // Integer overflow check for statements below.
        assert!(((len as u32) + 1) * 4 + 4 + (Checksum::ENCODED_SIZE as u32) < u32::MAX);
        // We should include current entry also in size, that's why +1 to len(b.entryOffsets).
        let entires_offset_size = ((len as u32) + 1)
            * 4
            + 4 // size of entires list
            + (Checksum::ENCODED_SIZE as u32);

        let estimated_size = current_block_end
            + 6 // header size for entry
            + key_size as u32
            + val_encoded_size
            + entires_offset_size
            // IV is added at the end of the block, while encrypting.
		    // So, size of IV is added to estimatedSize.
            + self.opts.encryption().block_size() as u32;

        // Integer overflow check for table size.
        assert!((current_block_end as u64) + (estimated_size as u64) < u32::MAX as u64);

        estimated_size > self.opts.block_size() as u32
    }

    /// Returns a suffix of newKey that is different from `self.base_key`.
    #[inline]
    fn key_diff<'a>(&self, new_key: &'a [u8]) -> &'a [u8] {
        let base_key = self.cur_block.base_key();
        let (new_key_len, base_key_len) = (new_key.len(), base_key.len());
        let mut idx = 0;
        while idx < new_key_len && idx < base_key_len {
            if new_key[idx] != base_key[idx] {
                break;
            }
            idx += 1;
        }
        &new_key[idx..]
    }

    fn insert_helper(&mut self, key: &Key, val: &Value, vplen: u32, val_encoded_size: u32) {
        self.key_hashes.push(hash(key.parse_key()));

        let version = key.parse_timestamp();
        if version > self.max_version {
            self.max_version = version;
        }

        let base_key = self.cur_block.base_key();
        // diffKey stores the difference of key with base key.
        let diff_key = if base_key.is_empty() {
            self.cur_block.set_base_key(key.clone());
            key
        } else {
            self.key_diff(key)
        };

        let key_len = key.len();
        let diff_key_len = diff_key.len();
        assert!(key_len - diff_key_len <= u16::MAX as usize);
        assert!(diff_key_len <= u16::MAX as usize);

        let header = Header {
            overlap: (key_len - diff_key_len) as u16,
            diff: diff_key_len as u16,
        };

        // store current entry's offset
        self.cur_block
            .push_entry_offset(self.cur_block.end() as u32);

        // Layout: header, diff_key, value.
        self.append(&header.encode());
        self.append(diff_key);

        let dst = self.allocate(val_encoded_size as usize);
        val.encode(dst.as_mut_slice());

        // Add the vpLen to the onDisk size. We'll add the size of the block to
        // onDisk size in Finish() function.
        self.on_disk_size += vplen;
    }

    fn allocate(&self, need: usize) -> Buffer {
        let data = self.cur_block.data();
        let prev_end = self.cur_block.end();
        if data.as_ref()[prev_end..].len() < need {
            // We need to reallocate. 1GB is the max size that the allocator can allocate.
            // While reallocating, if doubling exceeds that limit, then put the upper bound on it.
            let sz = (2 * data.len() as u64)
                .min(zallocator::Zallocator::MAX_ALLOC)
                .max((prev_end + need) as u64);
            let tmp = self.alloc.allocate_unchecked(sz);
            unsafe {
                core::ptr::copy_nonoverlapping(
                    data.as_ref()[..prev_end].as_ptr(),
                    tmp.as_mut_ptr(),
                    prev_end,
                );
            }
            self.cur_block.set_data(tmp);
        }

        self.cur_block.increase_end(need);
        self.cur_block.data().slice(prev_end..prev_end + need)
    }

    /// Appends to `cur_block.data`
    fn append(&self, data: &[u8]) {
        let dst = self.allocate(data.len());
        Buffer::copy_from_slice(&dst, data)
    }

    #[inline(always)]
    fn process_block(
        alloc: &Allocator,
        item: BBlock,
        compressed_size: &AtomicU32,
        compression: Compression,
        encryption: &Encryption,
    ) {
        let end = item.end();
        // Extract the block.
        let mut block_buf = item.data().slice(..end);
        // Compress the block.
        block_buf = Self::compress_data(alloc, block_buf, compression);
        // Encrypt the block.
        block_buf = Self::encrypt_data(alloc, block_buf, encryption);

        // BlockBuf should always less than or equal to allocated space. If the blockBuf is greater
        // than allocated space that means the data from this block cannot be stored in its
        // existing location.
        let allocated_space = vpb::compression::max_encoded_len(compression, end) + PADDING + 1;
        let cap = block_buf.capacity();
        assert!(cap <= allocated_space);
        // blockBuf was allocated on allocator. So, we don't need to copy it over.
        item.set_data(block_buf);
        item.set_end(cap);
        compressed_size.fetch_add(cap as u32, Ordering::SeqCst);
    }

    #[inline(always)]
    fn compress_data(alloc: &Allocator, data: Buffer, compression_algo: Compression) -> Buffer {
        if compression_algo.is_none() {
            return data;
        }

        let buffer = alloc.allocate_unchecked(data.max_encoded_len(compression_algo) as u64);
        let end = data
            .compress_to(buffer.as_mut_slice(), compression_algo)
            .map_err(|e| {
                #[cfg(feature = "tracing")]
                {
                    tracing::error!(target: "table_builder", err=%e, info = "error while compressing block in table builder.");
                }
                e
            })
            .unwrap();
        buffer.slice(..end)
    }

    #[inline(always)]
    fn encrypt_data(_alloc: &Allocator, data: Buffer, encryption: &Encryption) -> Buffer {
        let algo = encryption.algorithm();
        match algo {
            #[cfg(any(feature = "aes", feature = "aes-std"))]
            vpb::EncryptionAlgorithm::Aes => {
                let iv = vpb::encrypt::random_iv();
                let key = encryption.secret();

                let buffer = _alloc.allocate_unchecked((data.capacity() + iv.len()) as u64);
                let slice = buffer.as_mut_slice();
                data.encrypt_to(&mut slice[..data.capacity()], key, &iv, algo)
                    .map_err(|e| {
                        #[cfg(feature = "tracing")]
                        {
                            tracing::error!(target: "table_builder", err=%e, info = "error while encrypting block in table builder.");
                        }
                        e
                    })
                    .unwrap();
                slice[data.capacity()..].copy_from_slice(&iv);
                buffer
            }
            _ => data,
        }
    }
}