Skip to content
Snippets Groups Projects
Commit e0a6e074 authored by Dmitry Simonenko's avatar Dmitry Simonenko
Browse files

lua-autoinc: lua box.auto_increment function added

parent 47e3f4a3
No related branches found
No related tags found
No related merge requests found
......@@ -241,6 +241,19 @@ function box.update_ol(space, ops_list, ...)
return box.process(19, box.pack(format, unpack(args_list)))
end
-- Assumes that spaceno has a TREE int32 (NUM) primary key
-- inserts a tuple after getting the next value of the
-- primary key and returns it back to the user
function box.auto_increment(spaceno, ...)
spaceno = tonumber(spaceno)
local max_tuple = box.space[spaceno].index[0].idx:max()
local max = -1
if max_tuple ~= nil then
max = box.unpack('i', max_tuple[0])
end
return box.insert(spaceno, max + 1, ...)
end
function box.on_reload_configuration()
local index_mt = {}
-- __len and __index
......
No preview for this file type
......@@ -310,3 +310,19 @@ lua box.space[17].index[1].idx:count()
---
error: 'index.count(): one or more arguments expected'
...
lua box.auto_increment(18, 'a')
---
- 0: {'a'}
...
lua box.insert(18, 5)
---
- 5: {}
...
lua box.auto_increment(18, 'b')
---
- 6: {'b'}
...
lua box.auto_increment(18, 'c')
---
- 7: {'c'}
...
......@@ -106,3 +106,11 @@ exec admin "lua box.space[17].index[1].idx:count(2, 2)"
exec admin "lua box.space[17].index[1].idx:count(3)"
exec admin "lua box.space[17].index[1].idx:count(3, 3)"
exec admin "lua box.space[17].index[1].idx:count()"
#
# Tests for lua box.auto_increment
#
exec admin "lua box.auto_increment(18, 'a')"
exec admin "lua box.insert(18, 5)"
exec admin "lua box.auto_increment(18, 'b')"
exec admin "lua box.auto_increment(18, 'c')"
......@@ -226,3 +226,11 @@ space[17].index[1].key_field[0].fieldno = 1
space[17].index[1].key_field[0].type = "NUM"
space[17].index[1].key_field[1].fieldno = 2
space[17].index[1].key_field[1].type = "NUM"
# lua box.auto_increment() testing
# http://bugs.launchpad.net/tarantool/+bug/1006354
space[18].enabled = 1
space[18].index[0].type = "TREE"
space[18].index[0].unique = 1
space[18].index[0].key_field[0].fieldno = 0
space[18].index[0].key_field[0].type = "NUM"
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