ckb_rocksdb/
lib.rs

1// Copyright 2014 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
16//! Rust wrapper for RocksDB.
17//!
18//! # Examples
19//!
20//! ```
21//! use ckb_rocksdb::prelude::*;
22//! # use ckb_rocksdb::TemporaryDBPath;
23//! // NB: db is automatically closed at end of lifetime
24//!
25//! let path = "_path_for_rocksdb_storage";
26//! # let path = TemporaryDBPath::new();
27//! # {
28//!
29//! let db = DB::open_default(&path).unwrap();
30//! db.put(b"my key", b"my value").unwrap();
31//! match db.get(b"my key") {
32//!     Ok(Some(value)) => println!("retrieved value {}", value.to_utf8().unwrap()),
33//!     Ok(None) => println!("value not found"),
34//!     Err(e) => println!("operational problem encountered: {}", e),
35//! }
36//! db.delete(b"my key").unwrap();
37
38//! # }
39//! ```
40//!
41//! Opening a database and a single column family with custom options:
42//!
43//! ```
44//! use ckb_rocksdb::{prelude::*, ColumnFamilyDescriptor};
45//! # use ckb_rocksdb::TemporaryDBPath;
46//!
47//! let path = "_path_for_rocksdb_storage_with_cfs";
48//! # let path = TemporaryDBPath::new();
49//!
50//! let mut cf_opts = Options::default();
51//! cf_opts.set_max_write_buffer_number(16);
52//! let cf = ColumnFamilyDescriptor::new("cf1", cf_opts);
53//!
54//! let mut db_opts = Options::default();
55//! db_opts.create_missing_column_families(true);
56//! db_opts.create_if_missing(true);
57//! # {
58//! let db = DB::open_cf_descriptors(&db_opts, &path, vec![cf]).unwrap();
59//! # }
60//! ```
61//!
62
63pub extern crate librocksdb_sys as ffi;
64
65#[macro_use]
66pub mod ffi_util;
67mod util;
68
69pub mod backup;
70pub mod checkpoint;
71pub mod column_family;
72pub mod compaction_filter;
73pub mod compaction_filter_factory;
74mod comparator;
75mod db;
76mod db_iterator;
77mod db_options;
78mod db_pinnable_slice;
79mod db_vector;
80mod db_with_ttl;
81mod handle;
82pub mod merge_operator;
83mod open_raw;
84pub mod ops;
85mod optimistic_transaction;
86mod optimistic_transaction_db;
87mod options;
88mod read_only_db;
89mod secondary_db;
90mod slice_transform;
91mod snapshot;
92mod sst_file_writer;
93mod transaction;
94mod transaction_db;
95mod write_batch;
96
97pub mod prelude;
98
99pub use crate::column_family::ColumnFamilyDescriptor;
100pub use crate::compaction_filter::Decision as CompactionDecision;
101pub use crate::db::DB;
102pub use crate::db_iterator::{DBIterator, DBRawIterator, Direction, IteratorMode};
103pub use crate::db_options::{
104    BlockBasedIndexType, BlockBasedOptions, BottommostLevelCompaction, Cache, CompactOptions,
105    CuckooTableOptions, DBCompactionStyle, DBCompressionType, DBPath, DBRecoveryMode,
106    DataBlockIndexType, Env, FifoCompactOptions, FlushOptions, IngestExternalFileOptions,
107    KeyEncodingType, LogLevel, MemtableFactory, Options, PlainTableFactoryOptions, ReadOptions,
108    UniversalCompactOptions, UniversalCompactionStopStyle, WriteOptions,
109};
110pub use crate::db_pinnable_slice::DBPinnableSlice;
111pub use crate::db_vector::DBVector;
112pub use crate::db_with_ttl::{DBWithTTL, TTLOpenDescriptor};
113pub use crate::handle::{ConstHandle, Handle};
114pub use crate::options::FullOptions;
115pub use crate::read_only_db::ReadOnlyDB;
116pub use crate::secondary_db::{SecondaryDB, SecondaryOpenDescriptor};
117pub use crate::slice_transform::SliceTransform;
118pub use crate::snapshot::Snapshot;
119pub use crate::sst_file_writer::SstFileWriter;
120pub use crate::util::TemporaryDBPath;
121pub use crate::write_batch::WriteBatch;
122
123pub use crate::merge_operator::MergeOperands;
124use std::error;
125use std::fmt;
126
127pub use crate::optimistic_transaction::{OptimisticTransaction, OptimisticTransactionSnapshot};
128pub use crate::optimistic_transaction_db::{OptimisticTransactionDB, OptimisticTransactionOptions};
129pub use crate::transaction::{Transaction, TransactionSnapshot};
130pub use crate::transaction_db::{TransactionDB, TransactionDBOptions, TransactionOptions};
131
132/// A simple wrapper round a string, used for errors reported from
133/// ffi calls.
134#[derive(Debug, Clone, PartialEq)]
135pub struct Error {
136    message: String,
137}
138
139impl Error {
140    pub fn new(message: String) -> Error {
141        Error { message }
142    }
143
144    pub fn into_string(self) -> String {
145        self.into()
146    }
147}
148
149impl AsRef<str> for Error {
150    fn as_ref(&self) -> &str {
151        &self.message
152    }
153}
154
155impl From<Error> for String {
156    fn from(e: Error) -> String {
157        e.message
158    }
159}
160
161impl error::Error for Error {
162    fn description(&self) -> &str {
163        &self.message
164    }
165}
166
167impl fmt::Display for Error {
168    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
169        self.message.fmt(formatter)
170    }
171}
172
173/// An opaque type used to represent a column family. Returned from some functions, and used
174/// in others
175pub struct ColumnFamily {
176    inner: *mut ffi::rocksdb_column_family_handle_t,
177}
178
179unsafe impl Send for ColumnFamily {}