Commit 2580f6f7 authored by Tobias Koppers's avatar Tobias Koppers Committed by Tobias Koppers

add sled as database

parent 24a2e80e
......@@ -1595,7 +1595,7 @@ dependencies = [
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"parking_lot 0.12.1",
"signal-hook",
"signal-hook-mio",
"winapi",
......@@ -1611,7 +1611,7 @@ dependencies = [
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"parking_lot 0.12.1",
"signal-hook",
"signal-hook-mio",
"winapi",
......@@ -1806,7 +1806,7 @@ dependencies = [
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core",
"parking_lot_core 0.9.8",
]
[[package]]
......@@ -1820,7 +1820,7 @@ dependencies = [
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core",
"parking_lot_core 0.9.8",
]
[[package]]
......@@ -1940,7 +1940,7 @@ dependencies = [
"backtrace",
"lazy_static",
"mintex",
"parking_lot",
"parking_lot 0.12.1",
"rustc-hash",
"serde",
"serde_json",
......@@ -2201,7 +2201,7 @@ checksum = "d4029edd3e734da6fe05b6cd7bd2960760a616bd2ddd0d59a0124746d6272af0"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.3.5",
"windows-sys 0.48.0",
]
......@@ -2262,6 +2262,16 @@ dependencies = [
"syn 2.0.58",
]
[[package]]
name = "fs2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "fs_extra"
version = "1.3.0"
......@@ -4554,6 +4564,17 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.6",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
......@@ -4561,7 +4582,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
"parking_lot_core 0.9.8",
]
[[package]]
name = "parking_lot_core"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc"
dependencies = [
"cfg-if",
"instant",
"libc",
"redox_syscall 0.2.16",
"smallvec",
"winapi",
]
[[package]]
......@@ -4572,7 +4607,7 @@ checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.3.5",
"smallvec",
"windows-targets 0.48.1",
]
......@@ -5249,6 +5284,15 @@ dependencies = [
"swc_ecma_visit",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.3.5"
......@@ -6076,6 +6120,22 @@ dependencies = [
"autocfg",
]
[[package]]
name = "sled"
version = "0.34.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f96b4737c2ce5987354855aed3797279def4ebf734436c6aa4552cf8e169935"
dependencies = [
"crc32fast",
"crossbeam-epoch",
"crossbeam-utils",
"fs2",
"fxhash",
"libc",
"log",
"parking_lot 0.11.2",
]
[[package]]
name = "slice-group-by"
version = "0.3.0"
......@@ -6344,7 +6404,7 @@ dependencies = [
"napi",
"napi-derive",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"pathdiff",
"regex",
"rustc-hash",
......@@ -6433,7 +6493,7 @@ dependencies = [
"indexmap 2.2.6",
"is-macro",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"petgraph",
"radix_fmt",
"rayon",
......@@ -6484,7 +6544,7 @@ dependencies = [
"new_debug_unreachable",
"num-bigint",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"rkyv",
"rustc-hash",
"serde",
......@@ -7013,7 +7073,7 @@ checksum = "4afb24beda205684867ba555f59e4a470afbe67d9deaacc29b1a5e3a4a1fd58d"
dependencies = [
"auto_impl",
"dashmap 5.5.3",
"parking_lot",
"parking_lot 0.12.1",
"rayon",
"regex",
"serde",
......@@ -7036,7 +7096,7 @@ dependencies = [
"lru",
"normpath",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"path-clean 0.1.0",
"pathdiff",
"serde",
......@@ -7058,7 +7118,7 @@ dependencies = [
"num-bigint",
"num_cpus",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"phf 0.11.2",
"radix_fmt",
"rayon",
......@@ -7505,7 +7565,7 @@ dependencies = [
"anyhow",
"miette",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"swc_common",
]
......@@ -7605,7 +7665,7 @@ dependencies = [
"enumset",
"futures",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"serde",
"serde_json",
"swc_common",
......@@ -7801,7 +7861,7 @@ checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef"
dependencies = [
"cfg-if",
"fastrand 2.0.0",
"redox_syscall",
"redox_syscall 0.3.5",
"rustix 0.38.31",
"windows-sys 0.48.0",
]
......@@ -7987,7 +8047,7 @@ dependencies = [
"libc",
"mio",
"num_cpus",
"parking_lot",
"parking_lot 0.12.1",
"pin-project-lite",
"signal-hook-registry",
"socket2 0.5.6",
......@@ -8448,7 +8508,7 @@ dependencies = [
"indexmap 1.9.3",
"mopa",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"pin-project-lite",
"rayon",
"regex",
......@@ -8483,13 +8543,14 @@ dependencies = [
"indexmap 1.9.3",
"lmdb-rkv",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"pot",
"rand",
"rayon",
"rustc-hash",
"serde",
"serde_path_to_error",
"sled",
"smallvec",
"thread_local",
"tokio",
......@@ -8579,7 +8640,7 @@ dependencies = [
"jsonc-parser",
"mime",
"notify",
"parking_lot",
"parking_lot 0.12.1",
"rayon",
"rstest",
"serde",
......@@ -8663,7 +8724,7 @@ dependencies = [
"loom",
"num_cpus",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"rand",
"ref-cast",
"regex",
......@@ -8740,7 +8801,7 @@ dependencies = [
"nix 0.26.4",
"once_cell",
"owo-colors 3.5.0",
"parking_lot",
"parking_lot 0.12.1",
"portpicker",
"rand",
"regex",
......@@ -8912,7 +8973,7 @@ dependencies = [
"indexmap 1.9.3",
"mime",
"mime_guess",
"parking_lot",
"parking_lot 0.12.1",
"pin-project-lite",
"serde",
"serde_json",
......@@ -8948,7 +9009,7 @@ dependencies = [
"num-bigint",
"num-traits",
"once_cell",
"parking_lot",
"parking_lot 0.12.1",
"petgraph",
"regex",
"rustc-hash",
......@@ -9099,7 +9160,7 @@ dependencies = [
"mime",
"once_cell",
"owo-colors 3.5.0",
"parking_lot",
"parking_lot 0.12.1",
"regex",
"serde",
"serde_json",
......
......@@ -35,6 +35,7 @@ rayon = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_path_to_error = { workspace = true }
sled = "0.34.7"
smallvec = { workspace = true }
tokio = { workspace = true }
tokio-scoped = "0.2.0"
......
use crate::database::key_value_database::KeySpace;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ByKeySpace<T> {
infra: T,
task_meta: T,
......@@ -19,6 +20,16 @@ impl<T> ByKeySpace<T> {
}
}
pub fn try_new<E>(mut factory: impl FnMut(KeySpace) -> Result<T, E>) -> Result<Self, E> {
Ok(Self {
infra: factory(KeySpace::Infra)?,
task_meta: factory(KeySpace::TaskMeta)?,
task_data: factory(KeySpace::TaskData)?,
forward_task_cache: factory(KeySpace::ForwardTaskCache)?,
reverse_task_cache: factory(KeySpace::ReverseTaskCache)?,
})
}
pub fn get(&self, key_space: KeySpace) -> &T {
match key_space {
KeySpace::Infra => &self.infra,
......@@ -49,4 +60,25 @@ impl<T> ByKeySpace<T> {
]
.into_iter()
}
pub fn values(&self) -> [&T; 5] {
[
&self.infra,
&self.task_meta,
&self.task_data,
&self.forward_task_cache,
&self.reverse_task_cache,
]
}
pub fn from_values(values: [T; 5]) -> Self {
let [infra, task_meta, task_data, forward_task_cache, reverse_task_cache] = values;
Self {
infra,
task_meta,
task_data,
forward_task_cache,
reverse_task_cache,
}
}
}
......@@ -5,6 +5,7 @@ pub mod key_value_database;
pub mod lmdb;
pub mod noop_kv;
pub mod read_transaction_cache;
mod sled;
mod startup_cache;
pub use db_versioning::handle_db_versioning;
......@@ -12,4 +13,5 @@ pub use fresh_db_optimization::{is_fresh, FreshDbOptimization};
#[allow(unused_imports)]
pub use noop_kv::NoopKvDb;
pub use read_transaction_cache::ReadTransactionCache;
pub use sled::SledKeyValueDatabase;
pub use startup_cache::StartupCacheLayer;
use std::{borrow::Cow, path::Path};
use anyhow::Result;
use sled::{transaction::ConflictableTransactionResult, IVec, Transactional};
use crate::database::{
by_key_space::ByKeySpace,
key_value_database::{KeySpace, KeyValueDatabase, WriteBatch},
};
pub struct SledKeyValueDatabase {
db: sled::Db,
trees: ByKeySpace<sled::Tree>,
}
impl SledKeyValueDatabase {
pub fn new(path: &Path) -> Result<Self> {
let db = sled::open(path)?;
let trees = ByKeySpace::try_new(|key_space| {
db.open_tree(match key_space {
KeySpace::Infra => "infra",
KeySpace::TaskMeta => "task_meta",
KeySpace::TaskData => "task_data",
KeySpace::ForwardTaskCache => "forward_task_cache",
KeySpace::ReverseTaskCache => "reverse_task_cache",
})
})?;
Ok(Self { db, trees })
}
}
impl KeyValueDatabase for SledKeyValueDatabase {
type ReadTransaction<'l>
= ()
where
Self: 'l;
fn lower_read_transaction<'l: 'i + 'r, 'i: 'r, 'r>(
tx: &'r Self::ReadTransaction<'l>,
) -> &'r Self::ReadTransaction<'i> {
tx
}
fn begin_read_transaction(&self) -> Result<Self::ReadTransaction<'_>> {
Ok(())
}
type ValueBuffer<'l>
= IVec
where
Self: 'l;
fn get<'l, 'db: 'l>(
&'l self,
_transaction: &'l Self::ReadTransaction<'db>,
key_space: KeySpace,
key: &[u8],
) -> Result<Option<Self::ValueBuffer<'l>>> {
Ok(self.trees.get(key_space).get(key)?)
}
type WriteBatch<'l>
= SledWriteBatch<'l>
where
Self: 'l;
fn write_batch(&self) -> Result<Self::WriteBatch<'_>> {
Ok(SledWriteBatch {
batch: ByKeySpace::default(),
db: &self.db,
trees: &self.trees,
})
}
}
pub struct SledWriteBatch<'a> {
batch: ByKeySpace<sled::Batch>,
db: &'a sled::Db,
trees: &'a ByKeySpace<sled::Tree>,
}
impl<'a> WriteBatch<'a> for SledWriteBatch<'a> {
type ValueBuffer<'l>
= IVec
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l,
{
Ok(self.trees.get(key_space).get(key)?)
}
fn put(&mut self, key_space: KeySpace, key: Cow<[u8]>, value: Cow<[u8]>) -> Result<()> {
// self.batch
// .get_mut(key_space)
// .insert(cow_to_ivec(key), cow_to_ivec(value));
self.trees
.get(key_space)
.insert(cow_to_ivec(key), cow_to_ivec(value))?;
Ok(())
}
fn delete(&mut self, key_space: KeySpace, key: Cow<[u8]>) -> Result<()> {
// self.batch.get_mut(key_space).remove(cow_to_ivec(key));
self.trees.get(key_space).remove(key)?;
Ok(())
}
fn commit(self) -> Result<()> {
// Batching won't work as it consumes sooooo much memory
// match self.trees.values().transaction(
// |trees| -> ConflictableTransactionResult<(), anyhow::Error> {
// let trees = ByKeySpace::<&_>::from_values(vec_to_array_ref(trees));
// for (key_space, batch) in self.batch.iter() {
// trees.get(key_space).apply_batch(batch)?;
// }
// ConflictableTransactionResult::Ok(())
// },
// ) {
// Ok(()) => {
// self.db.flush()?;
// Ok(())
// }
// Err(sled::transaction::TransactionError::Abort(_)) => unreachable!(),
// Err(sled::transaction::TransactionError::Storage(err)) => Err(err.into()),
// }
self.db.flush()?;
Ok(())
}
}
fn cow_to_ivec(cow: Cow<[u8]>) -> IVec {
match cow {
Cow::Borrowed(slice) => IVec::from(slice),
Cow::Owned(vec) => IVec::from(vec),
}
}
fn vec_to_array_ref<T, const N: usize>(vec: &Vec<T>) -> [&T; N] {
let mut i = 0;
[(); N].map(|_| {
let v = &vec[i];
i += 1;
v
})
}
......@@ -14,7 +14,7 @@ use anyhow::Result;
pub use self::{backend::TurboTasksBackend, kv_backing_storage::KeyValueDatabaseBackingStorage};
use crate::database::{
handle_db_versioning, is_fresh, lmdb::LmbdKeyValueDatabase, FreshDbOptimization, NoopKvDb,
ReadTransactionCache, StartupCacheLayer,
ReadTransactionCache, SledKeyValueDatabase, StartupCacheLayer,
};
pub type LmdbBackingStorage = KeyValueDatabaseBackingStorage<
......@@ -37,8 +37,19 @@ pub fn noop_backing_storage(_path: &Path) -> Result<NoopBackingStorage> {
Ok(KeyValueDatabaseBackingStorage::new(NoopKvDb))
}
pub type DefaultBackingStorage = LmdbBackingStorage;
pub type SledBackingStorage =
KeyValueDatabaseBackingStorage<FreshDbOptimization<SledKeyValueDatabase>>;
pub fn sled_backing_storage(path: &Path) -> Result<SledBackingStorage> {
let path = handle_db_versioning(path)?;
let fresh_db = is_fresh(&path);
let database = SledKeyValueDatabase::new(&path)?;
let database = FreshDbOptimization::new(database, fresh_db);
Ok(KeyValueDatabaseBackingStorage::new(database))
}
pub type DefaultBackingStorage = SledBackingStorage;
pub fn default_backing_storage(path: &Path) -> Result<DefaultBackingStorage> {
lmdb_backing_storage(path)
sled_backing_storage(path)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment