Skip to content

Commit

Permalink
Get Transaction to deref to ReadTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Mar 20, 2024
1 parent c91d19b commit 401224f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl Handle {

pub fn move_prefix(&self, from: &[u8], to: &[u8]) -> Result<()> {
let mut tx = self.start_deferred_transaction()?;
let items = tx.read().list_items(from)?;
let items = tx.list_items(from)?;
let mut to_vec = to.to_vec();
for item in items {
to_vec.truncate(to.len());
Expand All @@ -461,7 +461,7 @@ impl Handle {

pub fn delete_prefix(&self, prefix: &[u8]) -> PubResult<()> {
let mut tx = self.start_deferred_transaction()?;
for item in tx.read().list_items(prefix)? {
for item in tx.list_items(prefix)? {
tx.delete_key(&item.key)?;
}
tx.commit(())?.complete();
Expand Down
2 changes: 1 addition & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'a> Reader<'a> {
}

pub fn list_items(&self, prefix: &[u8]) -> PubResult<Vec<Item>> {
self.owned_tx.read().list_items(prefix)
self.owned_tx.list_items(prefix)
}

fn get_file_clone(
Expand Down
28 changes: 10 additions & 18 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,21 @@ pub(crate) struct Transaction<'h> {
altered_files: HashSet<FileId>,
}

// TODO: Implement this, so the .read method doesn't need to exist: writable transactions should
// passthrough to read methods automatically.
// TODO: Try doing this with a read trait that just requires a rusqlite::Transaction be available.

// impl<'h> Deref for Transaction<'h> {
// type Target = ReadTransaction<&'h rusqlite::Transaction<'h>>;
//
// fn deref(&self) -> &Self::Target {
// unsafe {
// &ReadTransaction {
// tx: ReadOnlyRusqliteTransaction { conn: &self.tx },
// }
// }
// }
// }
impl<'h> Deref for Transaction<'h> {
type Target = ReadTransaction<rusqlite::Transaction<'h>>;

impl<'h> Transaction<'h> {
pub fn read(&self) -> ReadTransaction<&rusqlite::Transaction> {
ReadTransaction {
tx: ReadOnlyRusqliteTransaction { conn: &self.tx },
fn deref(&self) -> &Self::Target {
unsafe {
std::mem::transmute::<&rusqlite::Transaction, &ReadTransaction<rusqlite::Transaction>>(
&self.tx,
)
}
}
}

impl<'h> Transaction<'h> {
pub fn new(tx: rusqlite::Transaction<'h>, handle: &'h Handle) -> Self {
Self {
tx,
Expand Down Expand Up @@ -353,7 +346,6 @@ impl<'h> Transaction<'h> {
if let Some(max) = self.handle.instance_limits.max_value_length_sum {
loop {
let actual = self
.read()
.sum_value_length()
.context("reading value_length sum")?;
if actual <= max {
Expand Down

0 comments on commit 401224f

Please sign in to comment.