tutorial:transfer-api_transactions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:transfer-api_transactions [2023/07/19 13:43] – technici4n | tutorial:transfer-api_transactions [2023/07/19 14:00] (current) – [Nested transactions] technici4n | ||
---|---|---|---|
Line 23: | Line 23: | ||
</ | </ | ||
- | While this seems correct, it is possible that '' | + | While this seems correct, it is possible that '' |
+ | * Our tank has water, lava, and 1 unit of power. | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
We are now in a broken state where water was consumed but lava cannot be consumed. Either we abort the process and accept that water was deleted, or we continue the process and we still produce the obsidian. Either we delete resources that we shouldn’t, | We are now in a broken state where water was consumed but lava cannot be consumed. Either we abort the process and accept that water was deleted, or we continue the process and we still produce the obsidian. Either we delete resources that we shouldn’t, | ||
Line 92: | Line 97: | ||
</ | </ | ||
- | Here is how transactions can be visualized: | + | Here is how transactions can be visualized: |
+ | * Opening a new transaction creates a new copy of the state. From now on, that copy is modified. | ||
+ | * Aborting a transaction discards that copy. Back to the original state. | ||
+ | * Committing a transaction replaces the original state by the modified copy. From now on, this is the new state. | ||
- | We can represent this in a graph with branches: | + | We can represent this in a graph with branches: |
+ | * Any modification operates on the top branch. | ||
+ | * Opening a new transaction creates a new branch. | ||
+ | * Aborting a transaction discards the top branch. | ||
+ | * Committing a transaction merges the top branch into the branch below it. | ||
Here is the branching graph for that first example: | Here is the branching graph for that first example: | ||
- | {{https:// | + | {{:tutorial: |
==== Nested transactions ==== | ==== Nested transactions ==== | ||
Line 140: | Line 152: | ||
Here is the corresponding graph: | Here is the corresponding graph: | ||
- | {{https:// | + | {{:tutorial: |
=== Transaction vs TransactionContext === | === Transaction vs TransactionContext === | ||
- | You might have noticed that sometimes we use '' | + | You might have noticed that sometimes we use '' |
+ | * Use '' | ||
+ | * Use '' | ||
==== Implementing support for transactions ==== | ==== Implementing support for transactions ==== | ||
Line 150: | Line 164: | ||
In this section we explain how '' | In this section we explain how '' | ||
- | A '' | + | A '' |
+ | - Choose a data type to represent copies of internal state. Usually this will be a record. Here we will use '' | ||
+ | - Extend '' | ||
+ | - Implement functions to create copies of the internal state, and restore copies thereof - respectively '' | ||
+ | - Call '' | ||
Let’s start with the following template: | Let’s start with the following template: | ||
Line 226: | Line 244: | ||
For performance reasons, aborting or committing a transaction just runs a list of actions, but does not worry about more complex things such as nesting or state copies. See '' | For performance reasons, aborting or committing a transaction just runs a list of actions, but does not worry about more complex things such as nesting or state copies. See '' | ||
- | Now that we have been through this background knowledge, here is how the '' | + | Now that we have been through this background knowledge, here is how the '' |
+ | * When '' | ||
+ | * If we already have a snapshot for this transaction, | ||
+ | * Otherwise call '' | ||
+ | * When a transaction is aborted: | ||
+ | * We are guaranteed to have a snapshot for that transaction due to how state is managed. | ||
+ | * Call '' | ||
+ | * When a transaction is committed: | ||
+ | * If this is an outer (= not nested) transaction, | ||
+ | * We know that something probably changed, otherwise we would not have a registered close callback. | ||
+ | * Call '' | ||
+ | * If this is a nested transaction, | ||
+ | * If the parent transaction already has an older snapshot, discard the more recent snapshot. | ||
+ | * Otherwise the snapshot is moved to the parent transaction. | ||
Hopefully that gives an overview of what is happening under the hood. You should now be ready to read [[https:// | Hopefully that gives an overview of what is happening under the hood. You should now be ready to read [[https:// | ||
tutorial/transfer-api_transactions.1689774230.txt.gz · Last modified: 2023/07/19 13:43 by technici4n