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

RUST-1478 GridFS upload methods #751

Merged
merged 1 commit into from
Oct 20, 2022

Conversation

isabelatkinson
Copy link
Contributor

@isabelatkinson isabelatkinson commented Sep 26, 2022

Implements the upload_from_stream and upload_from_stream_with_id methods described in this section of the spec: https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#file-upload

};

impl GridFsBucket {
/// Uploads a user file to a GridFS bucket. Bytes are read from `source` and stored in chunks in
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as last PR, docs will be updated/examples will be added when I mark everything public.

@isabelatkinson isabelatkinson marked this pull request as ready for review October 12, 2022 16:04
@isabelatkinson isabelatkinson changed the title RUST-1478 Implement GridFS upload methods RUST-1478 GridFS upload methods Oct 12, 2022
@isabelatkinson isabelatkinson requested review from a team and kmahar and removed request for a team October 12, 2022 16:23
Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

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

looks good! just a couple of small comments re: things from the spec.

let mut n = 0;

loop {
let mut buf = vec![0u8; chunk_size as usize];
Copy link
Contributor

Choose a reason for hiding this comment

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

Micro-optimization nit: since chunk_size is static during iteration, it might be more efficient to construct buf outside the loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

As part of this, rather than truncating each time, we'll probably need to just slice the buffer.

async move {
let bucket = test_runner.get_bucket(id).await;
let hex_bytes = self.source.get("$$hexBytes").unwrap().as_str().unwrap();
// Iterates over two characters of the hex string at a time, parses each pair into a
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this use hex::decode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, this must've been implemented before i added that dependency. updated.

let mut n = 0;

loop {
let mut buf = vec![0u8; chunk_size as usize];
Copy link
Contributor

Choose a reason for hiding this comment

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

As part of this, rather than truncating each time, we'll probably need to just slice the buffer.

// Indexes should be considered equivalent regardless of numeric value type.
// e.g. { "filename": 1, "uploadDate": 1 } is equivalent to
// { "filename": 1.0, "uploadDate": 1.0 }
let number_matches = |key: &str| index_model.keys.get(key).and_then(get_int) == Some(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the indexes we create all currently use 1 for the value, but I think it would be good to avoid future pitfalls by checking against get_int of the input document too instead of Some(1).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My only hesitation with doing this when originally implementing was needing to account for extraneous BSON types that aren't really relevant here, something like this:

let number_matches = |key: &str, value: &Bson| {
    if let model_value = index_model.keys.get(key) {
        match get_int(value) {
            Some(num) => get_int(model_value) == Some(num),
            // this case is unreachable in practice
            None => model_value == value,
        }
    } else {
        false
    }
}

The reason I felt okay with hard-coding Some(1) here was because that value is significant in that it represents an increasing index, which is the only kind that has relevance for the various GridFS operations. Given that, and the fact that GridFS is a fairly static spec, it seemed like a fine tradeoff for the sake of readability.

This is all fairly pedantic, however, so if you think we should make the change I'm happy to do so.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think your proposed code looks great actually 🙂

That said, I agree that this is a really minor detail and I'm happy to defer to your judgment on whether this change is worth making.

// Indexes should be considered equivalent regardless of numeric value type.
// e.g. { "filename": 1, "uploadDate": 1 } is equivalent to
// { "filename": 1.0, "uploadDate": 1.0 }
let number_matches = |key: &str| index_model.keys.get(key).and_then(get_int) == Some(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think your proposed code looks great actually 🙂

That said, I agree that this is a really minor detail and I'm happy to defer to your judgment on whether this change is worth making.

@isabelatkinson isabelatkinson merged commit 7b62ff3 into mongodb:main Oct 20, 2022
@isabelatkinson isabelatkinson deleted the gridfs-upload branch October 20, 2022 19:28
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.

4 participants