Skip to content

Commit

Permalink
libsql: Deasyncify some Conn trait methods (#1135)
Browse files Browse the repository at this point in the history
There's no reason to have `is_autocommit()`, `changes()` or
`last_insert_row()` methods async because they're all fully local and
cannot block. Deasyncify them.
  • Loading branch information
penberg authored Mar 5, 2024
1 parent 50648a6 commit 6b13d77
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,13 @@ pub unsafe extern "C" fn libsql_column_type(
#[no_mangle]
pub unsafe extern "C" fn libsql_changes(conn: libsql_connection_t) -> u64 {
let conn = conn.get_ref();
RT.block_on(conn.changes())
conn.changes()
}

#[no_mangle]
pub unsafe extern "C" fn libsql_last_insert_rowid(conn: libsql_connection_t) -> i64 {
let conn = conn.get_ref();
RT.block_on(conn.last_insert_rowid())
conn.last_insert_rowid()
}

#[no_mangle]
Expand Down
6 changes: 3 additions & 3 deletions libsql-server/tests/hrana/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ fn affected_rows_and_last_rowid() {

let r = conn.execute("insert into t(x) values('a');", ()).await?;
assert_eq!(r, 1, "1st row inserted");
assert_eq!(conn.last_insert_rowid().await, 1, "1st row id");
assert_eq!(conn.last_insert_rowid(), 1, "1st row id");

let r = conn
.execute("insert into t(x) values('b'),('c');", ())
.await?;
assert_eq!(r, 2, "2nd and 3rd rows inserted");
assert_eq!(conn.last_insert_rowid().await, 3, "3rd row id");
assert_eq!(conn.last_insert_rowid(), 3, "3rd row id");

let r = conn.execute("update t set x = 'd';", ()).await?;
assert_eq!(r, 3, "all three rows updated");
assert_eq!(conn.last_insert_rowid().await, 3, "last row id unchanged");
assert_eq!(conn.last_insert_rowid(), 3, "last row id unchanged");

Ok(())
});
Expand Down
12 changes: 6 additions & 6 deletions libsql-server/tests/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,26 +336,26 @@ fn is_autocommit() {
let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?;
let conn = db.connect()?;

assert!(conn.is_autocommit().await);
assert!(conn.is_autocommit());
conn.execute("create table test (x)", ()).await?;

conn.execute("begin;", ()).await?;
assert!(!conn.is_autocommit().await);
assert!(!conn.is_autocommit());
conn.execute("insert into test values (12);", ()).await?;
conn.execute("commit;", ()).await?;
assert!(conn.is_autocommit().await);
assert!(conn.is_autocommit());

// make an explicit transaction
{
let tx = conn.transaction().await?;
assert!(!tx.is_autocommit().await);
assert!(conn.is_autocommit().await); // connection is still autocommit
assert!(!tx.is_autocommit());
assert!(conn.is_autocommit()); // connection is still autocommit

tx.execute("insert into test values (12);", ()).await?;
// transaction rolls back
}

assert!(conn.is_autocommit().await);
assert!(conn.is_autocommit());

let mut rows = conn.query("select count(*) from test", ()).await?;
assert_eq!(
Expand Down
18 changes: 9 additions & 9 deletions libsql/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub(crate) trait Conn {

async fn transaction(&self, tx_behavior: TransactionBehavior) -> Result<Transaction>;

async fn is_autocommit(&self) -> bool;
fn is_autocommit(&self) -> bool;

async fn changes(&self) -> u64;
fn changes(&self) -> u64;

async fn last_insert_rowid(&self) -> i64;
fn last_insert_rowid(&self) -> i64;

async fn reset(&self);
}
Expand Down Expand Up @@ -100,18 +100,18 @@ impl Connection {
}

/// Check weather libsql is in `autocommit` or not.
pub async fn is_autocommit(&self) -> bool {
self.conn.is_autocommit().await
pub fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}

/// Check the amount of changes the last query created.
pub async fn changes(&self) -> u64 {
self.conn.changes().await
pub fn changes(&self) -> u64 {
self.conn.changes()
}

/// Check the last inserted row id.
pub async fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid().await
pub fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid()
}

pub async fn reset(&self) {
Expand Down
12 changes: 6 additions & 6 deletions libsql/src/hrana/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ impl Conn for HttpConnection<HttpSender> {
})
}

async fn is_autocommit(&self) -> bool {
fn is_autocommit(&self) -> bool {
self.is_autocommit()
}

async fn changes(&self) -> u64 {
fn changes(&self) -> u64 {
self.affected_row_count()
}

async fn last_insert_rowid(&self) -> i64 {
fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid()
}

Expand Down Expand Up @@ -277,15 +277,15 @@ impl Conn for HranaStream<HttpSender> {
todo!("sounds like nested transactions innit?")
}

async fn is_autocommit(&self) -> bool {
fn is_autocommit(&self) -> bool {
false // for streams this method is callable only when we're within explicit transaction
}

async fn changes(&self) -> u64 {
fn changes(&self) -> u64 {
self.affected_row_count()
}

async fn last_insert_rowid(&self) -> i64 {
fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid()
}

Expand Down
6 changes: 3 additions & 3 deletions libsql/src/local/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ impl Conn for LibsqlConnection {
})
}

async fn is_autocommit(&self) -> bool {
fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}

async fn changes(&self) -> u64 {
fn changes(&self) -> u64 {
self.conn.changes()
}

async fn last_insert_rowid(&self) -> i64 {
fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid()
}

Expand Down
8 changes: 4 additions & 4 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl RemoteConnection {
// and will return false if no rollback happened and the
// execute was valid.
pub(self) async fn maybe_execute_rollback(&self) -> Result<bool> {
if self.inner.lock().state != State::TxnReadOnly && !self.local.is_autocommit().await {
if self.inner.lock().state != State::TxnReadOnly && !self.local.is_autocommit() {
self.local.execute("ROLLBACK", Params::None).await?;
Ok(true)
} else {
Expand Down Expand Up @@ -332,15 +332,15 @@ impl Conn for RemoteConnection {
})
}

async fn is_autocommit(&self) -> bool {
fn is_autocommit(&self) -> bool {
self.is_state_init()
}

async fn changes(&self) -> u64 {
fn changes(&self) -> u64 {
self.inner.lock().changes
}

async fn last_insert_rowid(&self) -> i64 {
fn last_insert_rowid(&self) -> i64 {
self.inner.lock().last_insert_rowid
}

Expand Down

0 comments on commit 6b13d77

Please sign in to comment.