rust/crates/ra_ide_api/src/db.rs

89 lines
2.5 KiB
Rust
Raw Normal View History

2019-01-26 11:33:33 -06:00
use std::{
sync::Arc,
time,
};
2019-01-08 13:33:36 -06:00
2019-01-17 05:11:00 -06:00
use ra_db::{
2019-01-26 02:20:30 -06:00
CheckCanceled, FileId, Canceled, SourceDatabase,
2019-06-07 01:50:32 -05:00
salsa::{self, Database},
2019-01-17 05:11:00 -06:00
};
2019-01-08 13:33:36 -06:00
2019-01-26 02:17:05 -06:00
use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}};
2019-01-08 13:33:36 -06:00
2019-01-25 06:16:50 -06:00
#[salsa::database(
2019-01-26 02:20:30 -06:00
ra_db::SourceDatabaseStorage,
2019-01-25 14:27:16 -06:00
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage,
hir::db::InternDatabaseStorage,
2019-06-01 13:17:57 -05:00
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
hir::db::HirDatabaseStorage
2019-01-25 06:16:50 -06:00
)]
2019-01-08 13:33:36 -06:00
#[derive(Debug)]
pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>,
2019-01-26 11:33:33 -06:00
pub(crate) last_gc: time::Instant,
pub(crate) last_gc_check: time::Instant,
2019-01-08 13:33:36 -06:00
}
impl salsa::Database for RootDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
&self.runtime
}
2019-01-10 03:20:32 -06:00
fn on_propagated_panic(&self) -> ! {
Canceled::throw()
}
fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) {
match event().kind {
salsa::EventKind::DidValidateMemoizedValue { .. }
| salsa::EventKind::WillExecute { .. } => {
self.check_canceled();
}
_ => (),
}
}
2019-01-08 13:33:36 -06:00
}
impl Default for RootDatabase {
fn default() -> RootDatabase {
2019-06-07 12:49:29 -05:00
RootDatabase::new(None)
}
}
impl RootDatabase {
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
2019-01-08 13:33:36 -06:00
let mut db = RootDatabase {
runtime: salsa::Runtime::default(),
2019-01-26 11:33:33 -06:00
last_gc: time::Instant::now(),
last_gc_check: time::Instant::now(),
2019-01-08 13:33:36 -06:00
};
2019-01-26 02:17:05 -06:00
db.set_crate_graph(Default::default());
db.set_local_roots(Default::default());
db.set_library_roots(Default::default());
2019-06-07 12:49:29 -05:00
let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity);
db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity);
2019-01-08 13:33:36 -06:00
db
}
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
2019-01-26 11:33:33 -06:00
last_gc: self.last_gc.clone(),
last_gc_check: self.last_gc_check.clone(),
2019-01-08 13:33:36 -06:00
})
}
}
2019-01-25 14:27:16 -06:00
#[salsa::query_group(LineIndexDatabaseStorage)]
2019-01-26 02:20:30 -06:00
pub(crate) trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
2019-01-17 05:11:00 -06:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
2019-01-08 13:33:36 -06:00
}
2019-01-26 02:20:30 -06:00
fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex> {
2019-01-08 13:33:36 -06:00
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}