diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..6706980f5 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[env] +CXX="g++" +CXXFLAGS="-std=c++20 -DCXXASYNC_HAVE_COROUTINE_HEADER" diff --git a/librocksdb_sys/build.rs b/librocksdb_sys/build.rs index 22e9e4030..cff0c607e 100644 --- a/librocksdb_sys/build.rs +++ b/librocksdb_sys/build.rs @@ -15,7 +15,7 @@ extern crate bindgen; extern crate cc; extern crate cmake; -use cc::Build; +use cc::{Build, Tool}; use cmake::Config; use std::path::{Path, PathBuf}; use std::{env, str}; @@ -79,24 +79,29 @@ fn main() { build.flag("-std=c++11"); build.flag("-fno-rtti"); } - link_cpp(&mut build); - build.warnings(false).compile("libcrocksdb.a"); -} -fn link_cpp(build: &mut Build) { let tool = build.get_compiler(); - let stdlib = if tool.is_like_gnu() { - "libstdc++.a" + + if tool.is_like_gnu() { + link_lib("libstdc++.a", &tool); + build.cpp_link_stdlib(None); } else if tool.is_like_clang() { - "libc++.a" + link_lib("libc++.a", &tool); + build.cpp_link_stdlib(None); } else { // Don't link to c++ statically on windows. - return; }; + + link_lib("liburing.a", &tool); + + build.warnings(false).compile("libcrocksdb.a"); +} + +fn link_lib(lib: &str, tool: &Tool) { let output = tool .to_command() .arg("--print-file-name") - .arg(stdlib) + .arg(lib) .output() .unwrap(); if !output.status.success() || output.stdout.is_empty() { @@ -111,7 +116,7 @@ fn link_cpp(build: &mut Build) { return; } // remove lib prefix and .a postfix. - let libname = &stdlib[3..stdlib.len() - 2]; + let libname = &lib[3..lib.len() - 2]; // optional static linking if cfg!(feature = "static_libcpp") { println!("cargo:rustc-link-lib=static={}", &libname); @@ -122,7 +127,6 @@ fn link_cpp(build: &mut Build) { "cargo:rustc-link-search=native={}", path.parent().unwrap().display() ); - build.cpp_link_stdlib(None); } fn build_rocksdb() -> Build { diff --git a/librocksdb_sys/rust-titan/Cargo.toml b/librocksdb_sys/rust-titan/Cargo.toml new file mode 100644 index 000000000..930834a36 --- /dev/null +++ b/librocksdb_sys/rust-titan/Cargo.toml @@ -0,0 +1,28 @@ +[package] +edition = "2021" +version = "0.1.0" +name = "rust-titan" +authors = ["Sunny Bains "] + +[lib] +name = "rust_titan" +path = "src/lib.rs" +crate-type = ["staticlib"] + +[dependencies] +rocksdb = { path = "../.." } + +[dependencies.cxx] +version = "1" +cxx-build = "1" +features = ["c++20"] + +[build-dependencies] +cc = "1.0.3" +cmake = "0.1" +cxx-build = "1" +pkg-config = "0.3" + +[dependencies.snappy-sys] +git = "https://github.com/busyjay/rust-snappy.git" +branch = "static-link" diff --git a/librocksdb_sys/rust-titan/build.rs b/librocksdb_sys/rust-titan/build.rs new file mode 100644 index 000000000..3ae05744c --- /dev/null +++ b/librocksdb_sys/rust-titan/build.rs @@ -0,0 +1,23 @@ +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=include/rust-titan.h"); + println!("cargo:rerun-if-changed=src/rust-titan.cc"); + println!("cargo:rustc-link-arg=-lcxxbridge1"); + + // FIXME: Use linklib() from top level build.rs + println!("cargo:rustc-link-arg=-lstdc++"); + println!("cargo:rerun-if-changed=src/rust-titan.cc"); + + println!("cargo:rustc-link-lib=static=rust-titan"); + + cxx_build::bridge("src/lib.rs") + .file("src/rust-titan.cc") + .flag("-DCXXASYNC_HAVE_COROUTINE_HEADER") + .flag("-fcoroutines") + .flag("-std=c++20") + .flag_if_supported("-Wall") + .include("include") + .include("../rocksdb/include") + .include("../crocksdb") + .compile("rust-titan"); +} diff --git a/librocksdb_sys/rust-titan/include/rust-titan.h b/librocksdb_sys/rust-titan/include/rust-titan.h new file mode 100644 index 000000000..d65c3808e --- /dev/null +++ b/librocksdb_sys/rust-titan/include/rust-titan.h @@ -0,0 +1,7 @@ +#pragma once + +#include "rocksdb/db.h" + +using ROCKSDB_NAMESPACE::DBOptions; + +std::shared_ptr new_db_options(); diff --git a/librocksdb_sys/rust-titan/src/lib.rs b/librocksdb_sys/rust-titan/src/lib.rs new file mode 100644 index 000000000..b03fc0d4f --- /dev/null +++ b/librocksdb_sys/rust-titan/src/lib.rs @@ -0,0 +1,106 @@ +use std::fmt; +use cxx::SharedPtr; +use rocksdb::rocksdb::*; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("rust-titan.h"); + include!("rocksdb/db.h"); + + #[namespace = "rocksdb"] + type DBOptions; + + unsafe fn new_db_options() -> SharedPtr; + } +} + +#[derive(Copy, Clone, Debug)] +enum TitanBlobRunMode { + /// Titan process read/write as normal + Normal = 0, + + /// Titan stop writing value into blob log during flush + /// and compaction. Existing values in blob log is still + /// readable and garbage collected. + ReadOnly = 1, + + /// On flush and compaction, Titan will convert blob + /// index into real value, by reading from blob log, + /// and store the value in SST file. + Fallback = 2, +} + +struct TitanDBOptions { + /// The directory to store data specific to TitanDB alongside with + /// the base DB. + /// + /// Default: {dbname}/titandb + dirname: String, + + /// Disable background GC + /// + /// Default: false + disable_background_gc: bool, + + /// Max background GC thread + /// + /// Default: 1 + max_background_gc: i32, + + /// How often to schedule delete obsolete blob files periods. + /// If set zero, obsolete blob files won't be deleted. + /// + /// Default: 10 + purge_obsolete_files_period_sec: u32, + + /// If non-zero, dump titan internal stats to info log every + /// titan_stats_dump_period_sec. + /// + /// Default: 600 (10 min) + titan_stats_dump_period_sec: u32, + + /// In C++ we would inherit from this class + db_options: SharedPtr, +} + +impl fmt::Display for TitanDBOptions { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {} {} {} {}", + self.dirname, + self.disable_background_gc, + self.max_background_gc, + self.purge_obsolete_files_period_sec, + self.titan_stats_dump_period_sec, + ) + } +} + +impl TitanDBOptions { + pub fn new() -> Self { + let db_options_ptr = unsafe { ffi::new_db_options() }; + + Self { + dirname: "".to_string(), + disable_background_gc: false, + max_background_gc: 1, + purge_obsolete_files_period_sec: 10, + titan_stats_dump_period_sec: 600, + db_options: db_options_ptr, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn create_db_options() { + let titan_options = TitanDBOptions::new(); + + println!("{}", titan_options); + } +} diff --git a/librocksdb_sys/rust-titan/src/rust-titan.cc b/librocksdb_sys/rust-titan/src/rust-titan.cc new file mode 100644 index 000000000..43fb4a9d0 --- /dev/null +++ b/librocksdb_sys/rust-titan/src/rust-titan.cc @@ -0,0 +1,9 @@ +#include "rust-titan.h" +#include "rust-titan/src/lib.rs.h" + +using ROCKSDB_NAMESPACE::DBOptions; + +std::shared_ptr new_db_options() { + return std::make_shared(); +} +