Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move crsql_createCrr to Rust #349

Merged
merged 5 commits into from
Sep 13, 2023
Merged

Move crsql_createCrr to Rust #349

merged 5 commits into from
Sep 13, 2023

Conversation

Azarattum
Copy link
Contributor

Closes #325.

Functions moved to rust:

  • getTableInfo -> pull_table_info (found a todo comment to rename this one)
  • freeTableInfo -> free_table_info
  • isTableCompatible -> is_table_compatible
  • createCrr -> create_crr

Table info is implemented with raw pointers to ensure compatibility with C counterparts. Therefore all the C tests verify the new Rust implementations.

Also we can convert the entire tableinfo.c as long as we convert get-table.c (as it is its only dependency). I can continue doing that here or start a new issue/PR for that.

Copy link
Collaborator

@tantaman tantaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it and learned some idiomatic rust today.

There are two minor comments but looks good.

if self.len() == 0 {
null_mut()
} else {
self.shrink_to(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why shrink before converting to raw parts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Vec also has a capacity which might be larger than its length. When we transfer ownership out of Rust we need to strip this unused space to avoid memory leaks. Essentially we make sure that capacity = length which is what we assume when transferring ownership back to Rust to free all the stuff.

core/rs/core/src/c.rs Show resolved Hide resolved
*/
pub fn create_crr(
db: *mut sqlite::sqlite3,
_schema: &str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one day we'll support attached dbs in cr-sqlite...

core/rs/core/src/tableinfo.rs Show resolved Hide resolved
core/rs/core/src/tableinfo.rs Show resolved Hide resolved
@tantaman
Copy link
Collaborator

also -- I'm assuming you can address the two issues and update the PR even though I approved the pull request?

That was a flow we often used at Facebook (approved with comments to be addressed) but idk if it works on GitHub.

@tantaman
Copy link
Collaborator

Also we can convert the entire tableinfo.c as long as we convert get-table.c (as it is its only dependency). I can continue doing that here or start a new issue/PR for that.

Lets do it in a new PR.

@Azarattum
Copy link
Contributor Author

I love it and learned some idiomatic rust today.

This is curious... I expected to get some things wrong. The big reason why I took this PR is that I wanted to start learning Rust. Actually this is my first real code in Rust 😅

if !info.nonPks.is_null() {
drop(Vec::from_raw_parts(
info.nonPks,
info.nonPksLen as usize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the shrink was required for from_raw_parts to correctly drop here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly

}

if cols.len() != columns_len {
err.set(&"Number of fetched columns did not match expected number of columns");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need to reference the static string as it should already be a reference.

E.g.,

change

&"Number of fetched columns did not match expected number of columns"

to

"Number of fetched columns did not match expected number of columns"

@tantaman
Copy link
Collaborator

This is curious...

haha, well I learned more ways to use generic traits and about map_err

drop(Box::from_raw(table_info));
}

pub fn is_table_compatible(db: *mut sqlite::sqlite3, table: &str, err: *mut *mut c_char) -> bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return Result<bool, ResultCode> given it can fail for reasons besides the table not being compatible.

"SELECT count(*) FROM pragma_index_list('{table}')
WHERE \"origin\" != 'pk' AND \"unique\" = 1"
);
match db.prepare_v2(&sql).and_then(|stmt| {
Copy link
Collaborator

@tantaman tantaman Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if is_table_compatible returns a Result then you can skip all the and_then and match stuff.

edit: ah we can't given we set specific error messages in every case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we treat Failed to analyze errors as Ok(false) or Err(code) then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err(code)

@tantaman
Copy link
Collaborator

Actually this is my first real code in Rust

Could have fooled me but I took a second look over and found some things that should be addressed.

Copy link
Collaborator

@tantaman tantaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see prior comments about

  • static string references
  • returning Result from is_table_compatible
  • free_cols being a bit complicated

@Azarattum
Copy link
Contributor Author

@tantaman what do you think? I've added a Countable util trait since that code repeated too often. is_table_compatible function is considerably simpler now.

@Azarattum Azarattum requested a review from tantaman September 13, 2023 15:09
@tantaman
Copy link
Collaborator

is_table_compatible function is considerably simpler now.

👍

@tantaman tantaman merged commit 52d0837 into vlcn-io:main Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

move crsql_createCrr to Rust
2 participants