realm / realm-core

Core database component for the Realm Mobile Database SDKs
https://realm.io
Apache License 2.0
1.02k stars 165 forks source link

Nested Transactions #1149

Open mrackwitz opened 9 years ago

mrackwitz commented 9 years ago

Allow to start a new nested write transaction within an already open write transaction.

cmelchior commented 9 years ago

Android issue for this: https://github.com/realm/realm-java/issues/1509

mrackwitz commented 9 years ago

Cocoa issue for this: https://github.com/realm/realm-cocoa/issues/2502

finnschiermer commented 8 years ago

Implementation Idea: extend existing machinery to buffer multiple transactions before "physical" commit. We need to a) be able to rollback multiple transactions to get the accessors correctly updated b) save freelists, toprefs and baselines on a stack for each nested transaction start c) restore on transaction rollback d) reuse current copy-on-write machinery by moving "baseline" for each nested transaction e) don't merge changes when multiple nested transactions commit, instead consider them as multiple distinct transactions, when it comes to numbering and log generation. f) BUT (to ensure durability) write all the transactions without syncing/flipping top pointers between each one. Only do that in the end.

simonask commented 8 years ago

It seems that there are also two separate use cases in play, and I have no idea what the implications are of each or whether they automatically collapse, but they are:

Furthermore, it seems that a natural requirement to impose is that a transaction with subtransactions may not be modified after starting a subtransaction, potentially even if that subtransaction has been rolled back.

finnschiermer commented 8 years ago

I'm not sure I understand. It doesn't make sense to commit something which has a nested uncommitted transaction, does it? So commit must always occur from the inside out. What am I missing?

simonask commented 8 years ago

If instead of real nesting you just have a chain of transactions where you want to set "checkpoints" on the way, but always commit things to the database in order according to when the transaction was started (in a FIFO manner), that seems to be a clear use case :)

finnschiermer commented 8 years ago

Ok, I'll call that pipelined commits. I think we can support both with the same underlying machinery. Mostly.