Skip to content
Merged
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
1 change: 1 addition & 0 deletions libsql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ core = [
"dep:bitflags",
"dep:bytes",
"dep:futures",
"dep:parking_lot",
]
stream = [
"dep:futures",
Expand Down
30 changes: 15 additions & 15 deletions libsql/src/local/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use super::{Database, Error, Result, Rows, RowsFuture, Statement, Transaction};
use crate::TransactionBehavior;

use libsql_sys::ffi;
use std::cell::RefCell;
use std::{ffi::c_int, fmt, path::Path, sync::Arc};
use parking_lot::RwLock;

/// A connection to a libSQL database.
#[derive(Clone)]
Expand All @@ -25,7 +25,7 @@ pub struct Connection {
#[cfg(feature = "replication")]
pub(crate) writer: Option<crate::replication::Writer>,

authorizer: RefCell<Option<AuthHook>>,
authorizer: Arc<RwLock<Option<AuthHook>>>,
}

impl Drop for Connection {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Connection {
drop_ref: Arc::new(()),
#[cfg(feature = "replication")]
writer: db.writer()?,
authorizer: RefCell::new(None),
authorizer: Arc::new(RwLock::new(None)),
};
#[cfg(feature = "sync")]
if let Some(_) = db.sync_ctx {
Expand All @@ -93,7 +93,7 @@ impl Connection {
drop_ref: Arc::new(()),
#[cfg(feature = "replication")]
writer: None,
authorizer: RefCell::new(None),
authorizer: Arc::new(RwLock::new(None)),
}
}

Expand Down Expand Up @@ -473,7 +473,7 @@ impl Connection {
}
}

*self.authorizer.borrow_mut() = hook.clone();
*self.authorizer.write() = hook.clone();

let (callback, user_data) = match hook {
Some(_) => {
Expand Down Expand Up @@ -633,7 +633,7 @@ impl Connection {
pub(crate) fn wal_insert_handle(&self) -> WalInsertHandle<'_> {
WalInsertHandle {
conn: self,
in_session: RefCell::new(false),
in_session: RwLock::new(false),
}
}
}
Expand All @@ -647,7 +647,7 @@ unsafe extern "C" fn authorizer_callback(
accessor: *const ::std::os::raw::c_char,
) -> ::std::os::raw::c_int {
let conn = user_data as *const Connection;
let hook = unsafe { (*conn).authorizer.borrow() };
let hook = unsafe { (*conn).authorizer.read() };
let hook = match &*hook {
Some(hook) => hook,
None => return ffi::SQLITE_OK,
Expand Down Expand Up @@ -688,37 +688,37 @@ unsafe extern "C" fn authorizer_callback(

pub(crate) struct WalInsertHandle<'a> {
conn: &'a Connection,
in_session: RefCell<bool>,
in_session: RwLock<bool>
}

impl WalInsertHandle<'_> {
pub fn insert_at(&self, frame_no: u32, frame: &[u8]) -> Result<()> {
assert!(*self.in_session.borrow());
assert!(*self.in_session.read());
self.conn.wal_insert_frame(frame_no, frame)
}

pub fn in_session(&self) -> bool {
*self.in_session.borrow()
*self.in_session.read()
}

pub fn begin(&self) -> Result<()> {
assert!(!*self.in_session.borrow());
assert!(!*self.in_session.read());
self.conn.wal_insert_begin()?;
self.in_session.replace(true);
*self.in_session.write() = true;
Ok(())
}

pub fn end(&self) -> Result<()> {
assert!(*self.in_session.borrow());
assert!(*self.in_session.read());
self.conn.wal_insert_end()?;
self.in_session.replace(false);
*self.in_session.write() = false;
Ok(())
}
}

impl Drop for WalInsertHandle<'_> {
fn drop(&mut self) {
if *self.in_session.borrow() {
if *self.in_session.read() {
if let Err(err) = self.conn.wal_insert_end() {
tracing::error!("{:?}", err);
Err(err).unwrap()
Expand Down
Loading