Newbie MapDB question on mutability after read

42 views
Skip to first unread message

David Ohsie

unread,
Mar 16, 2018, 6:34:26 PM3/16/18
to MapDB
Question on mutability:

I know that it is not allowed to insert an object into MapDB and then mutate that object.

Question: What if you read an object out of a MapDB map (say you do a get() on an HTreeMap).  Is the object that you get() a copy that is safe to mutate?  Or could it be a reference which is unsafe to mutate?

Foo f = map.get(1);
f.setBar(2); // This mutates the Foo object which I retrieved from a MapDB map.  Is this OK or forbidden?

David Ohsie
Dell EMC

Andrew Byrd

unread,
Mar 18, 2018, 2:33:02 AM3/18/18
to ma...@googlegroups.com


> On Mar 17, 2018, at 07:34, David Ohsie <david...@gmail.com> wrote:
> I know that it is not allowed to insert an object into MapDB and then mutate that object.

I wouldn’t say it’s not allowed. It just doesn’t behave the way you might expect. When you put the object into MapDB it is stored in its present form at the time of the function call. Subsequent changes to the object (instance) will not be reflected in the MapDB unless you call put() again. However, MapDB can cache objects to avoid repeatedly reading them from its own data storage mechanism. So getting an object from MapDB, modifying it, and then calling get() again with the same key will yield an instance with unpredictable contents.

> Question: What if you read an object out of a MapDB map (say you do a get() on an HTreeMap). Is the object that you get() a copy that is safe to mutate? Or could it be a reference which is unsafe to mutate?

It is not entirely “unsafe” to mutate it, it just might behave differently than you expect for certain sequences of actions. As mentioned above, there is an “instance cache" that is enabled by default. If you modify an object received from MapDB and then get that same object again from MapDB, it’s unpredictable whether it will hand you the same instance you modified or a new instance made from the pre-modification data in MapDB.

The above description is based on my understanding of the architecture and past experience. I am not an expert on the internals so it is possible that I’m not 100% correct on the details.

-Andrew

Message has been deleted

Andrew Byrd

unread,
Mar 20, 2018, 5:06:43 AM3/20/18
to ma...@googlegroups.com


On Mar 18, 2018, at 21:23, David Ohsie <david...@gmail.com> wrote:

Thank you for the response.  My weak understanding is that it was unsafe to mutate an object after an insertion, not just possibly confusing.  This is because you could be mutating it while it is in the process of being persisted.  The manual says "Data stored in MapDB (keys and values) should be immutable. MapDB serializes objects on background.”.

Ah, then I retract what I wrote. There is more to this than I thought. I knew about the asynchronous writes but I assumed the serialization was happening immediately when you call put() and it was only the actual writing of bytes to disk that happened asynchronously.

Sorry if I added to the confusion, and at least I now understand that we’re advised against mutating these objects at all.

Andrew
Reply all
Reply to author
Forward
0 new messages