90 lines
2.7 KiB
Rust
Raw Normal View History

use std::{sync::Arc, time};
2019-01-08 22:33:36 +03:00
2019-01-17 14:11:00 +03:00
use ra_db::{
2019-06-26 09:12:46 +03:00
salsa::{self, Database, Durability},
Canceled, CheckCanceled, FileId, SourceDatabase,
2019-01-17 14:11:00 +03:00
};
2019-01-08 22:33:36 +03:00
use crate::{
symbol_index::{self, SymbolsDatabase},
LineIndex,
};
2019-01-08 22:33:36 +03:00
2019-01-25 15:16:50 +03:00
#[salsa::database(
2019-01-26 11:20:30 +03:00
ra_db::SourceDatabaseStorage,
2019-01-25 23:27:16 +03:00
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage,
hir::db::InternDatabaseStorage,
2019-06-01 21:17:57 +03:00
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
hir::db::HirDatabaseStorage
2019-01-25 15:16:50 +03:00
)]
2019-01-08 22:33:36 +03:00
#[derive(Debug)]
pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>,
2019-01-26 20:33:33 +03:00
pub(crate) last_gc: time::Instant,
pub(crate) last_gc_check: time::Instant,
2019-01-08 22:33:36 +03:00
}
impl salsa::Database for RootDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
&self.runtime
}
2019-01-10 12:20:32 +03: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 22:33:36 +03:00
}
impl Default for RootDatabase {
fn default() -> RootDatabase {
2019-06-07 20:49:29 +03:00
RootDatabase::new(None)
}
}
impl RootDatabase {
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
2019-01-08 22:33:36 +03:00
let mut db = RootDatabase {
runtime: salsa::Runtime::default(),
2019-01-26 20:33:33 +03:00
last_gc: time::Instant::now(),
last_gc_check: time::Instant::now(),
2019-01-08 22:33:36 +03:00
};
2019-06-26 09:12:46 +03:00
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
2019-06-07 20:49:29 +03: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-06-30 15:03:23 +03:00
db.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity);
2019-01-08 22:33:36 +03:00
db
}
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self),
2019-07-04 13:26:44 -04:00
last_gc: self.last_gc,
last_gc_check: self.last_gc_check,
2019-01-08 22:33:36 +03:00
})
}
}
2019-01-25 23:27:16 +03:00
#[salsa::query_group(LineIndexDatabaseStorage)]
2019-01-26 11:20:30 +03:00
pub(crate) trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
2019-01-17 14:11:00 +03:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
2019-01-08 22:33:36 +03:00
}
2019-01-26 11:20:30 +03:00
fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex> {
2019-01-08 22:33:36 +03:00
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}