canopydb 0.2.5

Transactional key-value storage engine
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
#[cfg(unix)]
use std::os::unix::prelude::AsRawFd;
use std::{
    fs::{self, File},
    io::{self, IoSlice, Write},
    mem::MaybeUninit,
    path::{Path, PathBuf},
};

use crate::{
    shim::{
        sync::{self, atomic::AtomicBool, mpsc},
        thread,
    },
    Error,
};

pub fn common_prefix_len(a: &[u8], b: &[u8]) -> usize {
    a.iter().zip(b.iter()).take_while(|(&x, &y)| x == y).count()
}

pub fn fallocate(file: &File, len: u64) -> Result<(), io::Error> {
    fail::fail_point!("ftruncate", |s| Err(io::Error::new(
        io::ErrorKind::Other,
        format!("failpoint ftruncate {:?}", s)
    )));
    #[cfg(not(miri))]
    #[cfg(any(target_os = "linux", target_os = "android"))]
    {
        // Use fallocate instead of posix_fallocate to avoid writing zeroes on COW filesystems
        let res = unsafe { libc::fallocate64(file.as_raw_fd(), 0, 0, len.try_into().unwrap()) };
        match nix::Error::result(res) {
            Ok(_) => return Ok(()),
            Err(nix::errno::Errno::ENOTSUP) => (),
            Err(e) => return Err(e.into()),
        }
    }
    #[cfg(not(miri))]
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    unsafe {
        let fstore = libc::fstore_t {
            fst_flags: libc::F_ALLOCATEALL, // Allocate all requested space or no space at all.
            fst_posmode: libc::F_PEOFPOSMODE, // Allocate from the physical end of file.
            fst_offset: 0,
            fst_length: len.try_into().unwrap(),
            fst_bytesalloc: 0, // output size, can be > length
        };
        let res = libc::fcntl(file.as_raw_fd(), libc::F_PREALLOCATE, &fstore);
        match nix::Error::result(res) {
            Ok(_) => (), // we still need to call ftruncate (set_len) below
            Err(nix::errno::Errno::ENOTSUP) => (),
            Err(e) => return Err(e.into()),
        }
    }
    file.set_len(len)?;
    Ok(())
}

#[allow(unused_variables)]
pub fn fadvise_wont_need(f: &File, len: u64) -> io::Result<()> {
    #[cfg(any(target_os = "android", target_os = "linux"))]
    {
        let _ = nix::fcntl::posix_fadvise(
            f,
            0,
            len as _,
            nix::fcntl::PosixFadviseAdvice::POSIX_FADV_DONTNEED,
        );
    }
    Ok(())
}

#[allow(unused_variables)]
pub fn fadvise_read_ahead(file: &File, read_ahead: bool) -> io::Result<()> {
    #[cfg(any(target_os = "android", target_os = "linux"))]
    let _ = nix::fcntl::posix_fadvise(
        file,
        0,
        0,
        if read_ahead {
            nix::fcntl::PosixFadviseAdvice::POSIX_FADV_SEQUENTIAL
        } else {
            nix::fcntl::PosixFadviseAdvice::POSIX_FADV_RANDOM
        },
    );
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    unsafe {
        let _ = libc::fcntl(file.as_raw_fd(), libc::F_RDAHEAD, read_ahead as libc::c_int);
    }
    Ok(())
}

#[allow(unused_variables)]
pub fn fsync_range(file: &File, sync_offset: u64, sync_len: u64) -> io::Result<()> {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    unsafe {
        libc::sync_file_range(
            file.as_raw_fd(),
            sync_offset as _,
            sync_len as _,
            libc::SYNC_FILE_RANGE_WRITE,
        );
    }
    #[cfg(not(any(target_os = "linux", target_os = "android")))]
    let _ = file.sync_data();
    Ok(())
}

/// Atomically writes the file by first writing to a .tmp sibling file and then renaming.
/// The file contents are fsynced but not its parent directory.
pub fn atomic_file_write(path: &Path, contents: &[u8]) -> io::Result<()> {
    let ext = [
        path.extension()
            .unwrap_or_default()
            .to_string_lossy()
            .as_ref(),
        ".tmp",
    ]
    .concat();
    let tmp_path = path.with_extension(ext);
    let mut f = File::create(&tmp_path)?;
    f.write_all(contents)?;
    f.sync_all()?;
    drop(f);
    std::fs::rename(&tmp_path, path)
}

pub fn create_direct_io_file(path: &Path) -> io::Result<File> {
    let mut opts = std::fs::OpenOptions::new();
    opts.read(true).write(true).create(true).truncate(false);
    #[cfg(any(target_os = "android", target_os = "linux"))]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.custom_flags((nix::fcntl::OFlag::O_DIRECT | nix::fcntl::OFlag::O_DSYNC).bits());
    }
    opts.open(path)
}

pub fn sync_dir(path: &Path) -> io::Result<()> {
    fail::fail_point!("fsync", |s| Err(io::Error::new(
        io::ErrorKind::Other,
        format!("failpoint fsync {:?}", s)
    )));
    #[cfg(unix)]
    {
        // On unix directory opens must be read only.
        fs::File::open(path)?.sync_all()
    }
    #[cfg(not(unix))]
    {
        // On windows we must use FILE_FLAG_BACKUP_SEMANTICS to get a handle to the file
        // From: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
        const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x02000000;
        use std::os::windows::fs::OpenOptionsExt;
        fs::OpenOptions::new()
            .read(true)
            .write(true)
            .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
            .open(path)?
            .sync_all()
    }
}

#[derive(Debug, Deref, DerefMut)]
pub struct FileWithPath {
    #[deref]
    #[deref_mut]
    pub file: File,
    pub path: PathBuf,
}

pub trait FileExt {
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()>;
    fn read_exact_at_to_vec(&self, vec: &mut Vec<u8>, offset: u64, len: usize) -> io::Result<()>;
    fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()>;
    fn write_all_vectored_at(&self, bufs: &mut [IoSlice<'_>], offset: u64) -> io::Result<()>;
}

impl FileExt for std::fs::File {
    #[cfg(unix)]
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
        fail::fail_point!("fread", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fread {:?}", s)
        )));
        std::os::unix::fs::FileExt::read_exact_at(self, buf, offset)
    }

    #[cfg(windows)]
    fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
        fail::fail_point!("fread", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fread {:?}", s)
        )));
        while !buf.is_empty() {
            match std::os::windows::fs::FileExt::seek_read(self, buf, offset) {
                Ok(0) => break,
                Ok(n) => {
                    let tmp = buf;
                    buf = &mut tmp[n..];
                    offset += n as u64;
                }
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        if !buf.is_empty() {
            Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "failed to fill whole buffer",
            ))
        } else {
            Ok(())
        }
    }

    fn read_exact_at_to_vec(
        &self,
        vec: &mut Vec<u8>,
        offset: u64,
        read_len: usize,
    ) -> io::Result<()> {
        fail::fail_point!("fread", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fread {:?}", s)
        )));
        vec.reserve_exact(read_len);
        unsafe {
            // Safety: the methods and syscalls involved are safe to use with uninitialized data
            // The trait read_exact_at handles the slice argument like a
            // ReadBuf from https://rust-lang.github.io/rfcs/2930-read-buf.html
            // and will eventually be replaced with it.
            let uninit_slice: &mut [MaybeUninit<u8>] = &mut vec.spare_capacity_mut()[..read_len];
            // Replaces the unstable method MaybeUninit::slice_assume_init_mut
            let assumed_init: &mut [u8] =
                &mut *(uninit_slice as *mut [MaybeUninit<u8>] as *mut [u8]);
            self.read_exact_at(assumed_init, offset)?;
            vec.set_len(vec.len() + read_len);
        }
        Ok(())
    }

    #[cfg(unix)]
    fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
        fail::fail_point!("fwrite", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fwrite {:?}", s)
        )));
        std::os::unix::fs::FileExt::write_all_at(self, buf, offset)
    }

    #[cfg(all(unix, not(miri)))]
    fn write_all_vectored_at(&self, bufs: &mut [IoSlice<'_>], offset: u64) -> io::Result<()> {
        let mut bufs = bufs;
        fail::fail_point!("fwrite", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fwrite {:?}", s)
        )));
        const MAX_VECTORS: usize = 128;
        while !bufs.is_empty() {
            // Safety: This transmute is guaranteed to work on unix system, see IoSlice documentation.
            let iovec_bufs: &[libc::iovec] = unsafe { std::mem::transmute(&*bufs) };
            let result = nix::Error::result(unsafe {
                libc::pwritev(
                    self.as_raw_fd(),
                    iovec_bufs.as_ptr(),
                    std::cmp::min(iovec_bufs.len(), MAX_VECTORS) as libc::c_int,
                    offset as libc::off_t,
                )
            })
            .map_err(io::Error::from);
            match result {
                Ok(0) => {
                    return Err(io::Error::new(
                        io::ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(n) => IoSlice::advance_slices(&mut bufs, n as usize),
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }

    #[cfg(any(not(unix), miri))]
    fn write_all_vectored_at(&self, bufs: &mut [IoSlice<'_>], mut offset: u64) -> io::Result<()> {
        fail::fail_point!("fwrite", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fwrite {:?}", s)
        )));
        for buf in bufs {
            self.write_all_at(buf, offset)?;
            offset += buf.len() as u64;
        }
        Ok(())
    }

    #[cfg(not(unix))]
    fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
        fail::fail_point!("fwrite", |s| Err(io::Error::new(
            io::ErrorKind::Other,
            format!("failpoint fwrite {:?}", s)
        )));
        while !buf.is_empty() {
            match std::os::windows::fs::FileExt::seek_write(self, buf, offset) {
                Ok(0) => {
                    return Err(io::Error::new(
                        io::ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(n) => {
                    buf = &buf[n..];
                    offset += n as u64
                }
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) => return Err(e),
            }
        }
        Ok(())
    }
}

/// Automatically calls the enclosed function on panics
pub struct FnTrap<T: FnOnce()>(Option<T>);

impl<T: FnOnce()> FnTrap<T> {
    #[must_use]
    #[inline]
    pub fn new(f: T) -> Self {
        Self(Some(f))
    }

    #[inline]
    pub fn disarm(mut self) {
        self.0.take().unwrap();
    }
}

impl<T: FnOnce()> Drop for FnTrap<T> {
    fn drop(&mut self) {
        if let Some(f) = self.0.take() {
            f();
        }
    }
}

/// Automatically aborts the transaction on unhandled errors
#[derive(Debug, Default)]
pub struct Trap(AtomicBool);

pub struct TrapGuard<'a>(Option<&'a AtomicBool>);

impl Trap {
    #[inline]
    pub fn setup(&self) -> Result<TrapGuard<'_>, Error> {
        self.check()?;
        Ok(TrapGuard(Some(&self.0)))
    }

    #[inline]
    pub fn check(&self) -> Result<(), Error> {
        if !self.0.load(sync::atomic::Ordering::Relaxed) {
            Ok(())
        } else {
            Err(Error::TransactionAborted)
        }
    }
}

impl TrapGuard<'_> {
    #[inline]
    pub fn disarm(mut self) {
        debug_assert!(self.0.is_some());
        self.0 = None;
    }
}

impl Drop for TrapGuard<'_> {
    #[inline]
    fn drop(&mut self) {
        if let Some(arc) = &self.0 {
            arc.store(true, sync::atomic::Ordering::Relaxed);
        }
    }
}

pub trait TrapResult {
    fn guard_trap(self, guard: TrapGuard) -> Self;
}

impl<T, E> TrapResult for Result<T, E> {
    #[inline]
    fn guard_trap(self, guard: TrapGuard) -> Self {
        if self.is_ok() {
            guard.disarm()
        }
        self
    }
}

#[derive(Debug)]
pub struct SharedJoinHandle<T> {
    handle: sync::Mutex<Option<thread::JoinHandle<T>>>,
    tx: sync::mpsc::SyncSender<()>,
}

impl<T> SharedJoinHandle<T> {
    pub fn spawn<F>(name: String, f: F) -> Self
    where
        F: FnOnce() -> T + Send + 'static,
        T: Send + 'static,
    {
        let (tx, rx) = mpsc::sync_channel(0);
        let handle = thread::Builder::new()
            .name(name)
            .spawn(move || {
                let result = f();
                drop(rx);
                result
            })
            .unwrap();
        Self {
            handle: sync::Mutex::new(Some(handle)),
            tx,
        }
    }

    pub fn join(&self) -> Option<T> {
        let _ = self.tx.send(());
        let mut locked_handle = self.handle.lock().unwrap();
        let thread_handle = locked_handle.take()?;
        Some(thread_handle.join().unwrap())
    }
}

#[derive(Debug)]
pub struct WaitJoinHandle<T>(Option<thread::JoinHandle<T>>);

impl<T> WaitJoinHandle<T> {
    pub fn new(handle: thread::JoinHandle<T>) -> Self {
        Self(Some(handle))
    }
}

impl<T> Drop for WaitJoinHandle<T> {
    fn drop(&mut self) {
        if let Some(t) = self.0.take() {
            let _ = t.join();
        }
    }
}

impl<T> WaitJoinHandle<T> {
    pub fn join(mut self) -> thread::Result<T> {
        self.0.take().unwrap().join()
    }
}

#[derive(Display, PartialEq, Eq)]
#[display("{:?}", self)]
/// Outputs bytes as escaped ascii strings
pub struct EscapedBytes<'a>(pub &'a [u8]);

impl std::fmt::Debug for EscapedBytes<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut end_zeroes = 0;
        if cfg!(any(fuzzing, test)) {
            end_zeroes = self.0.iter().rev().take_while(|b| **b == 0).count();
            if end_zeroes <= 5 {
                end_zeroes = 0;
            }
        }
        for &b in &self.0[..self.0.len() - end_zeroes] {
            write!(f, "{}", std::ascii::escape_default(b))?
        }
        if end_zeroes != 0 {
            write!(f, "\\0*{end_zeroes}")?;
        }
        Ok(())
    }
}

#[derive(Display)]
#[display("{:?}", self)]
/// Outputs bytes sizes as human sizes
pub struct ByteSize(pub u64);

impl std::fmt::Debug for ByteSize {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        const KB: u64 = 1024;
        const MB: u64 = 1024 * 1024;
        const GB: u64 = 1024 * 1024 * 1024;
        let (value, suffix) = match self.0 {
            v @ 0..KB => (v as f64, "B"),
            v @ KB..MB => (v as f64 / KB as f64, "KB"),
            v @ MB..GB => (v as f64 / MB as f64, "MB"),
            v @ GB.. => (v as f64 / GB as f64, "GB"),
        };
        write!(f, "{value:.3}{suffix}")
    }
}

pub trait CellExt<T> {
    fn reset<F: FnOnce(T) -> T>(&self, f: F);
}

impl<T: Copy> CellExt<T> for std::cell::Cell<T> {
    #[inline]
    fn reset<F: FnOnce(T) -> T>(&self, f: F) {
        let mut v = self.get();
        v = f(v);
        self.set(v);
    }
}