ckb_rocksdb/ops/
ingest_external_file.rs

1use crate::ffi;
2use crate::ffi_util::to_cpath;
3use crate::{handle::Handle, ColumnFamily, Error, IngestExternalFileOptions};
4use std::ffi::CString;
5use std::path::Path;
6
7pub trait IngestExternalFile {
8    fn ingest_external_file_full<P: AsRef<Path>>(
9        &self,
10        paths: Vec<P>,
11        opts: Option<&IngestExternalFileOptions>,
12    ) -> Result<(), Error>;
13
14    /// Loads a list of external SST files created with SstFileWriter into the DB with default opts
15    fn ingest_external_file<P: AsRef<Path>>(&self, paths: Vec<P>) -> Result<(), Error> {
16        self.ingest_external_file_full(paths, None)
17    }
18
19    /// Loads a list of external SST files created with SstFileWriter into the DB
20    fn ingest_external_file_opts<P: AsRef<Path>>(
21        &self,
22        paths: Vec<P>,
23        opts: &IngestExternalFileOptions,
24    ) -> Result<(), Error> {
25        self.ingest_external_file_full(paths, Some(opts))
26    }
27}
28
29pub trait IngestExternalFileCF {
30    fn ingest_external_file_cf_full<P: AsRef<Path>>(
31        &self,
32        cf: Option<&ColumnFamily>,
33        paths: Vec<P>,
34        opts: Option<&IngestExternalFileOptions>,
35    ) -> Result<(), Error>;
36
37    /// Loads a list of external SST files created with SstFileWriter into the DB for given Column Family
38    /// with default opts
39    fn ingest_external_file_cf<P: AsRef<Path>>(
40        &self,
41        cf: &ColumnFamily,
42        paths: Vec<P>,
43    ) -> Result<(), Error> {
44        self.ingest_external_file_cf_full(Some(cf), paths, None)
45    }
46
47    /// Loads a list of external SST files created with SstFileWriter into the DB for given Column Family
48    fn ingest_external_file_opts<P: AsRef<Path>>(
49        &self,
50        cf: &ColumnFamily,
51        paths: Vec<P>,
52        opts: &IngestExternalFileOptions,
53    ) -> Result<(), Error> {
54        self.ingest_external_file_cf_full(Some(cf), paths, Some(opts))
55    }
56}
57
58impl<T> IngestExternalFile for T
59where
60    T: IngestExternalFileCF,
61{
62    fn ingest_external_file_full<P: AsRef<Path>>(
63        &self,
64        paths: Vec<P>,
65        opts: Option<&IngestExternalFileOptions>,
66    ) -> Result<(), Error> {
67        self.ingest_external_file_cf_full(None, paths, opts)
68    }
69}
70
71impl<T> IngestExternalFileCF for T
72where
73    T: Handle<ffi::rocksdb_t> + super::Write,
74{
75    fn ingest_external_file_cf_full<P: AsRef<Path>>(
76        &self,
77        cf: Option<&ColumnFamily>,
78        paths: Vec<P>,
79        opts: Option<&IngestExternalFileOptions>,
80    ) -> Result<(), Error> {
81        let paths_v: Vec<CString> = paths
82            .iter()
83            .map(|path| {
84                to_cpath(
85                    path,
86                    "Failed to convert path to CString when IngestExternalFile.",
87                )
88            })
89            .collect::<Result<Vec<_>, _>>()?;
90
91        let cpaths: Vec<_> = paths_v.iter().map(|path| path.as_ptr()).collect();
92
93        let mut default_opts = None;
94
95        let ief_handle = IngestExternalFileOptions::input_or_default(opts, &mut default_opts)?;
96
97        unsafe {
98            match cf {
99                Some(cf) => ffi_try!(ffi::rocksdb_ingest_external_file_cf(
100                    self.handle(),
101                    cf.handle(),
102                    cpaths.as_ptr(),
103                    paths_v.len(),
104                    ief_handle
105                )),
106                None => ffi_try!(ffi::rocksdb_ingest_external_file(
107                    self.handle(),
108                    cpaths.as_ptr(),
109                    paths_v.len(),
110                    ief_handle
111                )),
112            };
113
114            Ok(())
115        }
116    }
117}