Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[env]
CXX="g++"
CXXFLAGS="-std=c++20 -DCXXASYNC_HAVE_COROUTINE_HEADER"
28 changes: 16 additions & 12 deletions librocksdb_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions librocksdb_sys/rust-titan/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
edition = "2021"
version = "0.1.0"
name = "rust-titan"
authors = ["Sunny Bains <sunny.bains@pingcap.com>"]

[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"
23 changes: 23 additions & 0 deletions librocksdb_sys/rust-titan/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
7 changes: 7 additions & 0 deletions librocksdb_sys/rust-titan/include/rust-titan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "rocksdb/db.h"

using ROCKSDB_NAMESPACE::DBOptions;

std::shared_ptr<DBOptions> new_db_options();
106 changes: 106 additions & 0 deletions librocksdb_sys/rust-titan/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<DBOptions>;
}
}

#[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<ffi::DBOptions>,
}

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);
}
}
9 changes: 9 additions & 0 deletions librocksdb_sys/rust-titan/src/rust-titan.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "rust-titan.h"
#include "rust-titan/src/lib.rs.h"

using ROCKSDB_NAMESPACE::DBOptions;

std::shared_ptr<DBOptions> new_db_options() {
return std::make_shared<DBOptions>();
}