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
use crate::ffi;
use crate::ffi_util::to_cpath;
use crate::{handle::Handle, ColumnFamily, Error, IngestExternalFileOptions};
use std::ffi::CString;
use std::path::Path;

pub trait IngestExternalFile {
    fn ingest_external_file_full<P: AsRef<Path>>(
        &self,
        paths: Vec<P>,
        opts: Option<&IngestExternalFileOptions>,
    ) -> Result<(), Error>;

    /// Loads a list of external SST files created with SstFileWriter into the DB with default opts
    fn ingest_external_file<P: AsRef<Path>>(&self, paths: Vec<P>) -> Result<(), Error> {
        self.ingest_external_file_full(paths, None)
    }

    /// Loads a list of external SST files created with SstFileWriter into the DB
    fn ingest_external_file_opts<P: AsRef<Path>>(
        &self,
        paths: Vec<P>,
        opts: &IngestExternalFileOptions,
    ) -> Result<(), Error> {
        self.ingest_external_file_full(paths, Some(opts))
    }
}

pub trait IngestExternalFileCF {
    fn ingest_external_file_cf_full<P: AsRef<Path>>(
        &self,
        cf: Option<&ColumnFamily>,
        paths: Vec<P>,
        opts: Option<&IngestExternalFileOptions>,
    ) -> Result<(), Error>;

    /// Loads a list of external SST files created with SstFileWriter into the DB for given Column Family
    /// with default opts
    fn ingest_external_file_cf<P: AsRef<Path>>(
        &self,
        cf: &ColumnFamily,
        paths: Vec<P>,
    ) -> Result<(), Error> {
        self.ingest_external_file_cf_full(Some(cf), paths, None)
    }

    /// Loads a list of external SST files created with SstFileWriter into the DB for given Column Family
    fn ingest_external_file_opts<P: AsRef<Path>>(
        &self,
        cf: &ColumnFamily,
        paths: Vec<P>,
        opts: &IngestExternalFileOptions,
    ) -> Result<(), Error> {
        self.ingest_external_file_cf_full(Some(cf), paths, Some(opts))
    }
}

impl<T> IngestExternalFile for T
where
    T: IngestExternalFileCF,
{
    fn ingest_external_file_full<P: AsRef<Path>>(
        &self,
        paths: Vec<P>,
        opts: Option<&IngestExternalFileOptions>,
    ) -> Result<(), Error> {
        self.ingest_external_file_cf_full(None, paths, opts)
    }
}

impl<T> IngestExternalFileCF for T
where
    T: Handle<ffi::rocksdb_t> + super::Write,
{
    fn ingest_external_file_cf_full<P: AsRef<Path>>(
        &self,
        cf: Option<&ColumnFamily>,
        paths: Vec<P>,
        opts: Option<&IngestExternalFileOptions>,
    ) -> Result<(), Error> {
        let paths_v: Vec<CString> = paths
            .iter()
            .map(|path| {
                to_cpath(
                    path,
                    "Failed to convert path to CString when IngestExternalFile.",
                )
            })
            .collect::<Result<Vec<_>, _>>()?;

        let cpaths: Vec<_> = paths_v.iter().map(|path| path.as_ptr()).collect();

        let mut default_opts = None;

        let ief_handle = IngestExternalFileOptions::input_or_default(opts, &mut default_opts)?;

        unsafe {
            match cf {
                Some(cf) => ffi_try!(ffi::rocksdb_ingest_external_file_cf(
                    self.handle(),
                    cf.handle(),
                    cpaths.as_ptr(),
                    paths_v.len(),
                    ief_handle
                )),
                None => ffi_try!(ffi::rocksdb_ingest_external_file(
                    self.handle(),
                    cpaths.as_ptr(),
                    paths_v.len(),
                    ief_handle
                )),
            };

            Ok(())
        }
    }
}