pub fn open_cf_descriptors<P, I>(opts: &Options, path: P, cfs: I) -> Result<DB, Error>
    where
        P: AsRef<Path>,
        I: IntoIterator<Item = ColumnFamilyDescriptor>,
    {
        let cfs: Vec<_> = cfs.into_iter().collect();

        let cpath = to_cpath(&path)?;

        if let Err(e) = fs::create_dir_all(&path) {
            return Err(Error::new(format!(
                "Failed to create RocksDB directory: `{:?}`.",
                e
            )));
        }

        let db: *mut ffi::rocksdb_t;
        let mut cf_map = BTreeMap::new();

        if cfs.is_empty() {
            unsafe {
                db = ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr() as *const _));
            }
        } else {
            let mut cfs_v = cfs;
            // Always open the default column family.
            if !cfs_v.iter().any(|cf| cf.name == "default") {
                cfs_v.push(ColumnFamilyDescriptor {
                    name: String::from("default"),
                    options: Options::default(),
                });
            }
            // We need to store our CStrings in an intermediate vector
            // so that their pointers remain valid.
            let c_cfs: Vec<CString> = cfs_v
                .iter()
                .map(|cf| CString::new(cf.name.as_bytes()).unwrap())
                .collect();

            let mut cfnames: Vec<_> = c_cfs.iter().map(|cf| cf.as_ptr()).collect();

            // These handles will be populated by DB.
            let mut cfhandles: Vec<_> = cfs_v.iter().map(|_| ptr::null_mut()).collect();

            let mut cfopts: Vec<_> = cfs_v
                .iter()
                .map(|cf| cf.options.inner as *const _)
                .collect();

            unsafe {
                db = ffi_try!(ffi::rocksdb_open_column_families(
                    opts.inner,
                    cpath.as_ptr(),
                    cfs_v.len() as c_int,
                    cfnames.as_mut_ptr(),
                    cfopts.as_mut_ptr(),
                    cfhandles.as_mut_ptr(),
                ));
            }

            for handle in &cfhandles {
                if handle.is_null() {
                    return Err(Error::new(
                        "Received null column family handle from DB.".to_owned(),
                    ));
                }
            }

            for (cf_desc, inner) in cfs_v.iter().zip(cfhandles) {
                cf_map.insert(cf_desc.name.clone(), ColumnFamily { inner });
            }
        }

        if db.is_null() {
            return Err(Error::new("Could not initialize database.".to_owned()));
        }

        Ok(DB {
            inner: db,
            cfs: cf_map,
            path: path.as_ref().to_path_buf(),
        })
    }
/// Open a database with the given database options and column family descriptors.
Open a database with the given database options and column family descriptors.
Open a database with the given database options and column family descriptors .
pub fn open_cf_descriptors<P, I>(opts: &Options, path: P, cfs: I) -> Result<DB, Error>
    where
        P: AsRef<Path>,
        I: IntoIterator<Item = ColumnFamilyDescriptor>,
    {
        let cfs: Vec<_> = cfs.into_iter().collect();
        let cpath = to_cpath(&path)?;
        if let Err(e) = fs::create_dir_all(&path) {
            return Err(Error::new(format!(
                "Failed to create RocksDB directory: `{:?}`.",
                e
            )));
        }
        let db: *mut ffi::rocksdb_t;
        let mut cf_map = BTreeMap::new();
        if cfs.is_empty() {
            unsafe {
                db = ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr() as *const _));
            }
        } else {
            let mut cfs_v = cfs;
            // Always open the default column family.
            if !cfs_v.iter().any(|cf| cf.name == "default") {
                cfs_v.push(ColumnFamilyDescriptor {
                    name: String::from("default"),
                    options: Options::default(),
                });
            }
            // We need to store our CStrings in an intermediate vector
            // so that their pointers remain valid.
            let c_cfs: Vec<CString> = cfs_v
                .iter()
                .map(|cf| CString::new(cf.name.as_bytes()).unwrap())
                .collect();
            let mut cfnames: Vec<_> = c_cfs.iter().map(|cf| cf.as_ptr()).collect();
            // These handles will be populated by DB.
            let mut cfhandles: Vec<_> = cfs_v.iter().map(|_| ptr::null_mut()).collect();
            let mut cfopts: Vec<_> = cfs_v
                .iter()
                .map(|cf| cf.options.inner as *const _)
                .collect();
            unsafe {
                db = ffi_try!(ffi::rocksdb_open_column_families(
                    opts.inner,
                    cpath.as_ptr(),
                    cfs_v.len() as c_int,
                    cfnames.as_mut_ptr(),
                    cfopts.as_mut_ptr(),
                    cfhandles.as_mut_ptr(),
                ));
            }
            for handle in &cfhandles {
                if handle.is_null() {
                    return Err(Error::new(
                        "Received null column family handle from DB.".to_owned(),
                    ));
                }
            }
            for (cf_desc, inner) in cfs_v.iter().zip(cfhandles) {
                cf_map.insert(cf_desc.name.clone(), ColumnFamily { inner });
            }
        }
        if db.is_null() {
            return Err(Error::new("Could not initialize database.".to_owned()));
        }
        Ok(DB {
            inner: db,
            cfs: cf_map,
            path: path.as_ref().to_path_buf(),
        })
    }
pub fn open_cf_descriptors < P , I > ( opts : & Options , path : P , cfs : I ) -> Result < DB , Error > where P : AsRef < Path > , I : IntoIterator < Item = ColumnFamilyDescriptor > , { let cfs : Vec < _ > = cfs . into_iter ( ) . collect ( ) ; let cpath = to_cpath ( & path ) ? ; if let Err ( e ) = fs :: create_dir_all ( & path ) { return Err ( Error :: new ( format ! ( " " e ) ) ) ; } let db : * mut ffi :: rocksdb_t ; let mut cf_map = BTreeMap :: new ( ) ; if cfs . is_empty ( ) { unsafe { db = ffi_try ! ( ffi rocksdb_open ( opts inner cpath as_ptr ( ) as const ) ) ; } } else { let mut cfs_v = cfs ; // Always open the default column family. if ! cfs_v . iter ( ) . any ( | cf | cf . name == " " ) { cfs_v . push ( ColumnFamilyDescriptor { name : String :: from ( " " ) , options : Options :: default ( ) , } ) ; } // We need to store our CStrings in an intermediate vector // so that their pointers remain valid. let c_cfs : Vec < CString > = cfs_v . iter ( ) . map ( | cf | CString :: new ( cf . name . as_bytes ( ) ) . unwrap ( ) ) . collect ( ) ; let mut cfnames : Vec < _ > = c_cfs . iter ( ) . map ( | cf | cf . as_ptr ( ) ) . collect ( ) ; // These handles will be populated by DB. let mut cfhandles : Vec < _ > = cfs_v . iter ( ) . map ( | _ | ptr :: null_mut ( ) ) . collect ( ) ; let mut cfopts : Vec < _ > = cfs_v . iter ( ) . map ( | cf | cf . options . inner as * const _ ) . collect ( ) ; unsafe { db = ffi_try ! ( ffi rocksdb_open_column_families ( opts inner cpath as_ptr ( ) cfs_v len ( ) as c_int cfnames as_mut_ptr ( ) cfopts as_mut_ptr ( ) cfhandles as_mut_ptr ( ) ) ) ; } for handle in & cfhandles { if handle . is_null ( ) { return Err ( Error :: new ( " " . to_owned ( ) , ) ) ; } } for ( cf_desc , inner ) in cfs_v . iter ( ) . zip ( cfhandles ) { cf_map . insert ( cf_desc . name . clone ( ) , ColumnFamily { inner } ) ; } } if db . is_null ( ) { return Err ( Error :: new ( " " . to_owned ( ) ) ) ; } Ok ( DB { inner : db , cfs : cf_map , path : path . as_ref ( ) . to_path_buf ( ) , } ) }
Open a database with the given database options and column family descriptors.
Open a database with the given database options and column family descriptors .
opts
&Options
path
P
cfs
I
opts
&Options
path
P
cfs
I