foundationdb/lib.rs
1// Copyright 2018 foundationdb-rs developers, https://github.com/Clikengo/foundationdb-rs/graphs/contributors
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7#![doc = include_str!("../README.md")]
8
9use foundationdb_sys::if_cfg_api_versions;
10
11#[macro_use]
12extern crate static_assertions;
13
14pub mod api;
15if_cfg_api_versions! {min = 510, max = 600 =>
16 pub mod cluster;
17}
18mod database;
19pub mod directory;
20mod error;
21if_cfg_api_versions! {min = 700 =>
22 #[deny(missing_docs)]
23 pub mod fdb_keys;
24}
25pub mod future;
26mod keyselector;
27if_cfg_api_versions! {min = 710 =>
28 #[deny(missing_docs)]
29 pub mod mapped_key_values;
30}
31
32pub mod metrics;
33
34/// Generated configuration types for use with the various `set_option` functions
35#[allow(clippy::all)]
36pub mod options;
37if_cfg_api_versions! {min = 710 =>
38 #[cfg(feature = "tenant-experimental")]
39 pub mod tenant;
40}
41
42#[cfg(any(feature = "recipes", feature = "recipes-leader-election"))]
43pub mod recipes;
44
45// Re-export metrics types for convenience
46pub use crate::metrics::TransactionMetrics;
47pub mod timekeeper;
48mod transaction;
49mod tuple_ext;
50
51pub mod tuple {
52 pub use crate::tuple_ext::*;
53 pub use foundationdb_tuple::*;
54}
55
56if_cfg_api_versions! {min = 510, max = 600 =>
57 pub use crate::cluster::Cluster;
58}
59
60pub use crate::database::*;
61pub use crate::error::FdbBindingError;
62pub use crate::error::{FdbError, FdbResult};
63pub use crate::keyselector::*;
64pub use crate::transaction::*;
65
66/// Initialize the FoundationDB Client API, this can only be called once per process.
67///
68/// # Returns
69///
70/// A `NetworkAutoStop` handle which must be dropped before the program exits.
71///
72/// # Safety
73///
74/// You *MUST* ensure drop is called on the returned object before the program exits.
75/// This is not required if the program is aborted.
76///
77/// This method used to be safe in version `0.4`. But because `drop` on the returned object
78/// might not be called before the program exits, it was found unsafe.
79///
80/// # Examples
81///
82/// ```rust
83/// let network = unsafe { foundationdb::boot() };
84/// // do some interesting things with the API...
85/// drop(network);
86/// ```
87///
88/// ```rust
89/// #[tokio::main]
90/// async fn main() {
91/// let network = unsafe { foundationdb::boot() };
92/// // do some interesting things with the API...
93/// drop(network);
94/// }
95/// ```
96pub unsafe fn boot() -> api::NetworkAutoStop {
97 unsafe {
98 let network_builder = api::FdbApiBuilder::default()
99 .build()
100 .expect("foundationdb API to be initialized");
101 network_builder.boot().expect("fdb network running")
102 }
103}
104
105/// Returns the default Fdb cluster configuration file path
106#[cfg(target_os = "linux")]
107pub fn default_config_path() -> &'static str {
108 "/etc/foundationdb/fdb.cluster"
109}
110
111/// Returns the default Fdb cluster configuration file path
112#[cfg(target_os = "macos")]
113pub fn default_config_path() -> &'static str {
114 "/usr/local/etc/foundationdb/fdb.cluster"
115}
116
117/// Returns the default Fdb cluster configuration file path
118#[cfg(target_os = "windows")]
119pub fn default_config_path() -> &'static str {
120 "C:/ProgramData/foundationdb/fdb.cluster"
121}
122
123/// slice::from_raw_parts assumes the pointer to be aligned and non-null.
124/// Since Rust nightly (mid-February 2024), it is enforced with debug asserts,
125/// but the FDBServer can return a null pointer if the slice is empty:
126/// ptr: 0x0, len: 0
127/// To avoid the assert, an empty slice is directly returned in that situation
128fn from_raw_fdb_slice<T, U: Into<usize>>(ptr: *const T, len: U) -> &'static [T] {
129 if ptr.is_null() {
130 return &[];
131 }
132 unsafe { std::slice::from_raw_parts(ptr, len.into()) }
133}