use std::iter;
use std::path::Path;
use crate::open_raw::{OpenRaw, OpenRawInput};
use crate::{ColumnFamilyDescriptor, Error, Options};
pub trait Open: OpenRaw {
fn open_default<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let mut opts = Options::default();
opts.create_if_missing(true);
Self::open(&opts, path)
}
fn open<P: AsRef<Path>>(opts: &Options, path: P) -> Result<Self, Error> {
Self::open_with_descriptor(opts, path, Self::Descriptor::default())
}
fn open_with_descriptor<P: AsRef<Path>>(
opts: &Options,
path: P,
descriptor: Self::Descriptor,
) -> Result<Self, Error> {
let input = OpenRawInput {
options: opts,
path: path.as_ref(),
column_families: vec![],
open_descriptor: descriptor,
outlive: vec![opts.outlive.clone()],
};
Self::open_raw(input)
}
}
pub trait OpenCF: OpenRaw {
fn open_cf<P, I, N>(opts: &Options, path: P, cfs: I) -> Result<Self, Error>
where
P: AsRef<Path>,
I: IntoIterator<Item = N>,
N: AsRef<str>,
{
let cfs = cfs
.into_iter()
.map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default()));
Self::open_cf_descriptors(opts, path, cfs)
}
fn open_cf_descriptors<P, I>(opts: &Options, path: P, cfs: I) -> Result<Self, Error>
where
P: AsRef<Path>,
I: IntoIterator<Item = ColumnFamilyDescriptor>,
{
Self::open_cf_descriptors_with_descriptor(opts, path, cfs, Self::Descriptor::default())
}
fn open_cf_descriptors_with_descriptor<P, I>(
opts: &Options,
path: P,
cfs: I,
descriptor: Self::Descriptor,
) -> Result<Self, Error>
where
P: AsRef<Path>,
I: IntoIterator<Item = ColumnFamilyDescriptor>,
{
let column_families: Vec<_> = cfs.into_iter().collect();
let outlive = iter::once(opts.outlive.clone())
.chain(column_families.iter().map(|cf| cf.options.outlive.clone()))
.collect();
let input = OpenRawInput {
options: opts,
path: path.as_ref(),
column_families,
open_descriptor: descriptor,
outlive,
};
Self::open_raw(input)
}
}