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-1743 Question: GridFS is the rust driver compatible with the java driver? #938

Closed
jse1t5 opened this issue Aug 24, 2023 · 3 comments
Closed
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@jse1t5
Copy link

jse1t5 commented Aug 24, 2023

Versions/Environment

  1. What version of Rust are you using? 1.70
  2. What operating system are you using? macOS Ventura 13.4.1
  3. What versions of the driver and its dependencies are you using?
  1. What version of MongoDB are you using? 5.0.12
  2. What is your MongoDB topology standalone

Describe the bug

We're actually trying to read a file from a GridFsBucket in Java which was created in Rust.
Java claims that it cannot cast a Long value into an Integer.

Im not sure why this happens but in my case on the Upload side in Rust the "chunkSize" in "fs.files" and "n" in "fs.chunks" has the type int64.
It seems like the driver is implemented with a u32 ?!

https://github.com/mongodb/mongo-rust-driver/blob/v2.6.1/src/gridfs.rs#L55
https://github.com/mongodb/mongo-rust-driver/blob/v2.6.1/src/gridfs.rs#L34

Currently we use the Java driver version:

<dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver-sync</artifactId>
      <version>4.6.1</version>
      <scope>compile</scope>
</dependency>

A File upload on the java side stores chunkSize and n as i32.

To Reproduce
Steps to reproduce the behavior:

  1. Create File via GridFsBucket in Rust
  2. Read File via GridFsBucket in Java

For now the following Code fixes my Problem but it would be nice to remove this Workaround:

async fn fix_mongodb_types(db: &DB, oid: &Bson) -> anyhow::Result<()> {
    let mut opts = FindOneOptions::default();
    opts.projection = Some(doc! { "chunkSize": 1});
    let file = db.client().collection::<DocChunkSize>("fs.files").find_one(doc! { "_id": &oid }, Some(opts)).await?.unwrap();
    db.client().collection::<Document>("fs.files").update_one(
        doc! { "_id": oid }, 
        doc! {
        "$set": {
            "chunkSize": Bson::Int32(file.chunk_size as i32)
        }
    }, None).await?;
    let mut opts = FindOptions::default();
    opts.projection = Some(doc! { "_id": 1, "n": 1 });
    let mut chunks = db.client().collection::<DocN>("fs.chunks").find(doc! { "files_id": &oid }, None).await?;
    while let Some(ch) = chunks.next().await {
        if let Ok(ch) = ch {
            db.client().collection::<Document>("fs.chunks").update_one(
                doc! { "_id": &ch.id }, 
                doc! {
                "$set": {
                    "n": Bson::Int32(ch.n as i32)
                }
            }, None).await?;
        }
    }
    Ok(())
}
@Logarithmus
Copy link

Logarithmus commented Aug 29, 2023

Clearly Java driver is wrong here, because having negative number of chunks or chunk size makes absolutely no sense. Before Java 8, there was absolutely no way of using unsigned numbers in Java. Currently you can use Integer wrapper class for this. I guess mongodb java driver needs some modification for this to work.

@abr-egn
Copy link
Contributor

abr-egn commented Aug 29, 2023

Thank you for this report!

This is indeed a bug in the Rust driver - because bson has no representation for unsigned numbers, the Rust bson crate stores them as the next-wider signed integer type. In this case, it meant that the fields in question were being stored as int64, and the specification requires them to be int32. #941 will fix this, and will be in our next release (this week).

@kevinAlbs kevinAlbs added tracked-in-jira Ticket filed in Mongo's Jira system and removed triage labels Aug 29, 2023
@kevinAlbs kevinAlbs changed the title Question: GridFS is the rust driver compatible with the java driver? RUST-1743 Question: GridFS is the rust driver compatible with the java driver? Aug 29, 2023
@abr-egn
Copy link
Contributor

abr-egn commented Aug 30, 2023

The fix for this is included in release 2.7.0-beta.

@abr-egn abr-egn closed this as completed Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

No branches or pull requests

4 participants