Skip to content

Commit a817361

Browse files
get rid of custom database handle
1 parent ec1ecd6 commit a817361

File tree

3 files changed

+55
-61
lines changed

3 files changed

+55
-61
lines changed

crates/djls-server/src/db.rs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,21 @@
11
use salsa::Database;
22

3-
/// A thread-safe handle to a Salsa database that can be shared between threads.
4-
///
5-
/// This implements the insight from [this Salsa Zulip discussion](https://salsa.zulipchat.com/#narrow/channel/145099-Using-Salsa/topic/.E2.9C.94.20Advice.20on.20using.20salsa.20from.20Sync.20.2B.20Send.20context/with/495497515)
6-
/// where we're using the `StorageHandle` to create a thread-safe handle that can be
7-
/// shared between threads. When we need to use it, we clone the handle to get a new reference.
8-
///
9-
/// Usage:
10-
/// ```rust,ignore
11-
/// // Create a new handle
12-
/// let db_handle = ServerDatabaseHandle::new();
13-
///
14-
/// // Clone it to pass to different threads
15-
/// let db_handle_clone = db_handle.clone();
16-
///
17-
/// // Use it in an async context
18-
/// async_fn(move || {
19-
/// // Get a database from the handle
20-
/// let db = db_handle_clone.db();
21-
///
22-
/// // Use the database
23-
/// db.some_query(args)
24-
/// });
25-
/// ```
26-
#[derive(Clone, Default)]
27-
pub struct ServerDatabaseHandle(salsa::StorageHandle<ServerDatabase>);
28-
29-
impl ServerDatabaseHandle {
30-
/// Create a new thread-safe database handle
31-
///
32-
/// This creates a handle that can be safely shared between threads and cloned
33-
/// to create new references to the same underlying database storage.
34-
pub fn new() -> Self {
35-
Self(salsa::StorageHandle::new(None))
36-
}
37-
38-
/// Get a database instance from this handle
39-
///
40-
/// This creates a usable database from the handle, which can be used
41-
/// to query and update data in the database. The database can be cloned
42-
/// cheaply as it's just a reference to the underlying storage.
43-
pub fn db(&self) -> ServerDatabase {
44-
let storage = self.0.clone().into_storage();
45-
ServerDatabase { storage }
46-
}
47-
}
48-
49-
impl std::fmt::Debug for ServerDatabaseHandle {
50-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51-
f.debug_struct("ServerDatabaseHandle").finish()
52-
}
53-
}
54-
553
#[salsa::db]
564
#[derive(Clone, Default)]
575
pub struct ServerDatabase {
586
storage: salsa::Storage<Self>,
597
}
608

9+
impl ServerDatabase {
10+
/// Create a new database from storage
11+
pub fn new(storage: salsa::Storage<Self>) -> Self {
12+
Self { storage }
13+
}
14+
}
15+
6116
impl std::fmt::Debug for ServerDatabase {
6217
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63-
f.debug_struct("ServerDatabase").finish()
18+
f.debug_struct("ServerDatabase").finish_non_exhaustive()
6419
}
6520
}
6621

crates/djls-server/src/server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl LanguageServer for DjangoLanguageServer {
262262
.await;
263263

264264
self.with_session_mut(|session| {
265-
let db = session.db_handle().db();
265+
let db = session.db();
266266
session.documents_mut().handle_did_open(&db, &params);
267267
})
268268
.await;
@@ -277,7 +277,7 @@ impl LanguageServer for DjangoLanguageServer {
277277
.await;
278278

279279
self.with_session_mut(|session| {
280-
let db = session.db_handle().db();
280+
let db = session.db();
281281
let _ = session.documents_mut().handle_did_change(&db, &params);
282282
})
283283
.await;
@@ -302,8 +302,8 @@ impl LanguageServer for DjangoLanguageServer {
302302
.with_session(|session| {
303303
if let Some(project) = session.project() {
304304
if let Some(tags) = project.template_tags() {
305-
// Get a database instance from the handle
306-
let db = session.db_handle().db();
305+
// Get a database instance directly
306+
let db = session.db();
307307
return session.documents().get_completions(
308308
&db,
309309
params.text_document_position.text_document.uri.as_str(),

crates/djls-server/src/session.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use djls_conf::Settings;
22
use djls_project::DjangoProject;
3+
use salsa::StorageHandle;
34
use tower_lsp_server::lsp_types::ClientCapabilities;
45

5-
use crate::db::ServerDatabaseHandle;
6+
use crate::db::ServerDatabase;
67
use crate::documents::Store;
78

89
#[derive(Default)]
@@ -11,7 +12,32 @@ pub struct Session {
1112
project: Option<DjangoProject>,
1213
documents: Store,
1314
settings: Settings,
14-
db_handle: ServerDatabaseHandle,
15+
16+
/// A thread-safe Salsa database handle that can be shared between threads.
17+
///
18+
/// This implements the insight from [this Salsa Zulip discussion](https://salsa.zulipchat.com/#narrow/channel/145099-Using-Salsa/topic/.E2.9C.94.20Advice.20on.20using.20salsa.20from.20Sync.20.2B.20Send.20context/with/495497515)
19+
/// where we're using the `StorageHandle` to create a thread-safe handle that can be
20+
/// shared between threads. When we need to use it, we clone the handle to get a new reference.
21+
///
22+
/// Usage:
23+
/// ```rust,ignore
24+
/// // Use the StorageHandle in Session
25+
/// let db_handle = StorageHandle::new(None);
26+
///
27+
/// // Clone it to pass to different threads
28+
/// let db_handle_clone = db_handle.clone();
29+
///
30+
/// // Use it in an async context
31+
/// async_fn(move || {
32+
/// // Get a database from the handle
33+
/// let storage = db_handle_clone.into_storage();
34+
/// let db = ServerDatabase::new(storage);
35+
///
36+
/// // Use the database
37+
/// db.some_query(args)
38+
/// });
39+
/// ```
40+
db_handle: StorageHandle<ServerDatabase>,
1541
}
1642

1743
impl Session {
@@ -21,7 +47,7 @@ impl Session {
2147
project: None,
2248
documents: Store::new(),
2349
settings: Settings::default(),
24-
db_handle: ServerDatabaseHandle::new(),
50+
db_handle: StorageHandle::new(None),
2551
}
2652
}
2753

@@ -57,7 +83,20 @@ impl Session {
5783
&mut self.settings
5884
}
5985

60-
pub fn db_handle(&self) -> &ServerDatabaseHandle {
86+
/// Get the raw database handle from the session
87+
///
88+
/// Note: In most cases, you'll want to use `db()` instead to get a usable
89+
/// database instance directly.
90+
pub fn db_handle(&self) -> &StorageHandle<ServerDatabase> {
6191
&self.db_handle
6292
}
93+
94+
/// Get a database instance directly from the session
95+
///
96+
/// This creates a usable database from the handle, which can be used
97+
/// to query and update data in the database.
98+
pub fn db(&self) -> ServerDatabase {
99+
let storage = self.db_handle.clone().into_storage();
100+
ServerDatabase::new(storage)
101+
}
63102
}

0 commit comments

Comments
 (0)