ckb_rocksdb/ops/
open.rs

1// Copyright 2019 Tyler Neely
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use std::iter;
17use std::path::Path;
18
19use crate::open_raw::{OpenRaw, OpenRawInput};
20use crate::{ColumnFamilyDescriptor, Error, Options};
21
22pub trait Open: OpenRaw {
23    /// Open a database with default options.
24    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    /// Open the database with the specified options.
31    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    /// Open a database with the given database options and column family names.
54    ///
55    /// Column families opened using this function will be created with default `Options`.
56    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    /// Open a database with the given database options and column family descriptors.
70    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}