Skip to content
Snippets Groups Projects
Commit 292cd479 authored by Ilya's avatar Ilya Committed by Roman Tsisyk
Browse files

Add a wrapper for box.begin/box.commit call

Add box.atomic(fun, arg) wrapper to execute fun(arg) in a transaction.

Closes #818
parent 73a39ad3
No related branches found
No related tags found
No related merge requests found
......@@ -228,6 +228,21 @@ box.begin = function()
box.error()
end
end
local function atomic_tail(status, ...)
if not status then
box.rollback()
error((...), 2)
end
box.commit()
return ...
end
box.atomic = function(fun, ...)
box.begin()
return atomic_tail(pcall(fun, ...))
end
-- box.commit yields, so it's defined as Lua/C binding
-- box.rollback yields as well
......
......@@ -55,7 +55,8 @@ t = {} for n in pairs(box) do table.insert(t, tostring(n)) end table.sort(t)
...
t
---
- - backup
- - atomic
- backup
- begin
- cfg
- commit
......
......@@ -423,3 +423,83 @@ function gh_1638() box.begin(); box.rollback() end
for i = 1, 1000 do fiber.create(function() gh_1638() end) end
---
...
--
--gh-818 add atomic()
--
space = box.schema.space.create('atomic')
---
...
index = space:create_index('primary')
---
...
test_run:cmd("setopt delimiter ';'")
---
- true
...
function args(...)
return 'args', ...
end;
---
...
box.atomic(args, 1, 2, 3, 4, 5);
---
- args
- 1
- 2
- 3
- 4
- 5
...
function tx()
space:auto_increment{'first row'}
space:auto_increment{'second row'}
return space:select{}
end;
---
...
box.atomic(tx);
---
- - [1, 'first row']
- [2, 'second row']
...
function tx_error(space)
space:auto_increment{'third'}
space:auto_increment{'fourth'}
error("some error")
end;
---
...
box.atomic(tx_error, space);
---
- error: '[string "function tx_error(space) space:auto_incre..."]:1: some error'
...
function nested(space)
box.begin()
end;
---
...
box.atomic(nested, space);
---
- error: 'Operation is not permitted when there is an active transaction '
...
function rollback(space)
space:auto_increment{'fifth'}
box.rollback()
end;
---
...
box.atomic(rollback, space);
---
...
test_run:cmd("setopt delimiter ''");
---
- true
...
space:select{}
---
- - [1, 'first row']
- [2, 'second row']
...
space:drop()
---
...
......@@ -195,3 +195,45 @@ box.space.test:drop()
--
function gh_1638() box.begin(); box.rollback() end
for i = 1, 1000 do fiber.create(function() gh_1638() end) end
--
--gh-818 add atomic()
--
space = box.schema.space.create('atomic')
index = space:create_index('primary')
test_run:cmd("setopt delimiter ';'")
function args(...)
return 'args', ...
end;
box.atomic(args, 1, 2, 3, 4, 5);
function tx()
space:auto_increment{'first row'}
space:auto_increment{'second row'}
return space:select{}
end;
box.atomic(tx);
function tx_error(space)
space:auto_increment{'third'}
space:auto_increment{'fourth'}
error("some error")
end;
box.atomic(tx_error, space);
function nested(space)
box.begin()
end;
box.atomic(nested, space);
function rollback(space)
space:auto_increment{'fifth'}
box.rollback()
end;
box.atomic(rollback, space);
test_run:cmd("setopt delimiter ''");
space:select{}
space:drop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment