Bug in Document.toMutable() in db21

I’m using the following code with DB21 to save a document that was previously read from the db:

        MutableDocument mutable = document.toMutable();
        database.save(mutable);

The problem is that while document.getKeys() returns 7 keys, mutable.getKeys() returns 0. It seems that toMutable does not copy over the actual keys from the source document. Looking at the code for toMutable:

public MutableDocument toMutable() {
    return new MutableDocument(this, null);
}

shouldn’t the second parameter be either _dict or _dict.toMutable()? It can’t be intended behavior that a function to make a data structure mutable does so with data loss.

It seems to work as expected in my tests.

Please provider the exact code snippet including the debug output of your documents.

It does copy over the data. If you are interested , this is where that happens. The dict parameter is needed when you create a new MutableDocument with data.

That is the exact snippet- I had a document (objects in my app are long lived, the document would have been created when it was read fro the db several minutes ago, or created via new if the document did not exist yet). I called toMutable() on it. The result had no keys (calling getKeys() for debugging purposes returned an empty list). This was 100% reproducible. Replacing it with the following code:

public static MutableDocument toMutable(Document document) {
    if(document == null) {
        return null;
    }
    return new MutableDocument(document.getId(), document.toMap());
}

fixed the problem. As a sanity check I just replaced the body of that code with a call to toMutable() again, and once again I was returned an empty mutable document.

You didn’t provide me the complete context - that snippet just shows you calling toMutable().
What is document ? Is it a previously saved document that you fetch from the database or is it just a newly created document?

Hi @gabe.sechan,

I added unit tests based on your comment. However, I could not reproduce the problem. Let us know if this test case does not cover your scenario.

Note: I tested with the master branch. DB021 might have the issue.

1 Like

I don’t see the problem in DB021 either.

@gabe.sechan, you need to provide us with a complete program to reproduce this, not just the line that fails. Or at least some code that starts with opening a new empty database and goes until the problem occurs.

(Also, it’s always a good idea to indicate what platform/language this is. I’m guessing Java?)