Skip to content

[Feature Req] Optionally use Transaction if supported #582

@Atreyagaurav

Description

@Atreyagaurav

Would it be possible to use transaction if supported, else use normal dataset.

Considering the output dataset being able to use any driver user wants, I want to speed up the writing process when I can. If the output is to gpkg file I'd like to use transaction, if it's to shp then the code I have will fail. So I'm hoping for something that can work on both cases.

The Idea I have is, considering the code for Transaction::commit uses the internal Dataset, we can probably add the commit method on the Dataset and do something like this:

let trans = d.start_transaction().is_ok();
// do stuffs with d: Dataset
if trans {
    d.commit()?;
}

Currently, You have to do something like this:

let mut trans = false;
if let Some(txn) = d.start_transaction() {
    // do stuffs with txn 
    trans = true;
}

if !trans {
    // do stuffs with d
}

You can't use if let ... {} else {} pattern, as you can't borrow d mutably on start_transaction() and on the else block at the same time. And due to rust's type system, you can't replace d: Dataset with txn: Transaction to do the same thing (although you can with deref).

But basically to write a code that works with drivers that support transaction and drivers that don't, I think the first pattern I suggested feels like the best way to do it, as the code is internally calling commit on the Dataset anyway.

pub fn commit(mut self) -> Result<()> {
let rv = unsafe { gdal_sys::GDALDatasetCommitTransaction(self.dataset.c_dataset()) };
self.rollback_on_drop = false;
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: rv,
method_name: "GDALDatasetCommitTransaction",
});
}
Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions