1use std::iter;
17use std::path::Path;
18
19use crate::open_raw::{OpenRaw, OpenRawInput};
20use crate::{ColumnFamilyDescriptor, Error, Options};
21
22pub trait Open: OpenRaw {
23 fn open_default<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
25 let mut opts = Options::default();
26 opts.create_if_missing(true);
27 Self::open(&opts, path)
28 }
29
30 fn open<P: AsRef<Path>>(opts: &Options, path: P) -> Result<Self, Error> {
32 Self::open_with_descriptor(opts, path, Self::Descriptor::default())
33 }
34
35 fn open_with_descriptor<P: AsRef<Path>>(
36 opts: &Options,
37 path: P,
38 descriptor: Self::Descriptor,
39 ) -> Result<Self, Error> {
40 let input = OpenRawInput {
41 options: opts,
42 path: path.as_ref(),
43 column_families: vec![],
44 open_descriptor: descriptor,
45 outlive: vec![opts.outlive.clone()],
46 };
47
48 Self::open_raw(input)
49 }
50}
51
52pub trait OpenCF: OpenRaw {
53 fn open_cf<P, I, N>(opts: &Options, path: P, cfs: I) -> Result<Self, Error>
57 where
58 P: AsRef<Path>,
59 I: IntoIterator<Item = N>,
60 N: AsRef<str>,
61 {
62 let cfs = cfs
63 .into_iter()
64 .map(|name| ColumnFamilyDescriptor::new(name.as_ref(), Options::default()));
65
66 Self::open_cf_descriptors(opts, path, cfs)
67 }
68
69 fn open_cf_descriptors<P, I>(opts: &Options, path: P, cfs: I) -> Result<Self, Error>
71 where
72 P: AsRef<Path>,
73 I: IntoIterator<Item = ColumnFamilyDescriptor>,
74 {
75 Self::open_cf_descriptors_with_descriptor(opts, path, cfs, Self::Descriptor::default())
76 }
77
78 fn open_cf_descriptors_with_descriptor<P, I>(
79 opts: &Options,
80 path: P,
81 cfs: I,
82 descriptor: Self::Descriptor,
83 ) -> Result<Self, Error>
84 where
85 P: AsRef<Path>,
86 I: IntoIterator<Item = ColumnFamilyDescriptor>,
87 {
88 let column_families: Vec<_> = cfs.into_iter().collect();
89 let outlive = iter::once(opts.outlive.clone())
90 .chain(column_families.iter().map(|cf| cf.options.outlive.clone()))
91 .collect();
92 let input = OpenRawInput {
93 options: opts,
94 path: path.as_ref(),
95 column_families,
96 open_descriptor: descriptor,
97 outlive,
98 };
99
100 Self::open_raw(input)
101 }
102}