Austin Ziegler
5/5/2005 7:17:00 PM
On 5/5/05, John Lam <jlam@iunknown.com> wrote:
> Wow wow wow! This is so massively cool. Thanks for creating this.
> I can't wait to spend some quality time with this.
>
> Do you snapshot state as the objects are modified? If so, does
> this avoid the problem of requiring two phase commit in your
> transaction groups?
I'm not quite sure what you're saying here. Transaction snapshots
are taken when you start the transaction:
require 'transaction/simple/group'
x = "Hello, you."
y = "And you, too."
g = Transaction::Simple::Group.new(x, y)
g.start_transaction(:first) # -> [ x, y ]
g.transaction_open?(:first) # -> true
x.transaction_open?(:first) # -> true
y.transaction_open?(:first) # -> true
At this point, the transaction state of x and y is set. 'g' doesn't
maintain any transaction state; x and y do independently. Strictly
speaking, the transaction group is just a synchronizer.
x.gsub!(/you/, "world") # -> "Hello, world."
y.gsub!(/you/, "me") # -> "And me, too."
Here, we've modified both x and y. No new snapshot has yet been
made.
g.start_transaction(:second) # -> [ x, y ]
There. That takes another snapshot and gives it the transaction name
of :second.
x.gsub!(/world/, "HAL") # -> "Hello, HAL."
y.gsub!(/me/, "Dave") # -> "And Dave, too."
Another modification.
g.rewind_transaction(:second) # -> [ x, y ]
x # -> "Hello, world."
y # -> "And me, too."
We've rewound the transaction on each object to its original state
here.
x.gsub!(/world/, "HAL") # -> "Hello, HAL."
y.gsub!(/me/, "Dave") # -> "And Dave, too."
g.commit_transaction(:second) # -> [ x, y ]
x # -> "Hello, HAL."
y # -> "And Dave, too."
Changes are made and the second transaction is committed on both
objects. Note: if something goes wrong in the committing of the
first object, then the second object won't be committed.
g.abort_transaction(:first) # -> [ x, y ]
x = -> "Hello, you."
y = -> "And you, too."
This aborts the transaction and restores the state of the objects to
their original value. Except that they are now able to do
transactions.
Make sense?
Most of this code is based on what I did for block transactions:
Transaction::Simple.start_named(:foo, x, y) do |tx, ty|
...
end
I may go through at some point and unify the implementations here
and provide block forms of the transaction group methods.
-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca