October 3rd, 2011
|11:17 pm - Implementing auto_increment using Lua stored procedures (Tarantool)|
Most databases, which do not natively support SEQUENCE data type require user to create a separate table, or other database object to hold sequence information.
In Tarantool, thanks to being able to access indexes directly from Lua, supporting sequences is just a few lines of code, wrapping the standard INSERT command:
function box.auto_increment(spaceno, ...)
max_tuple = box.space[spaceno].index.idx:max()
if max_tuple ~= nil then
max = box.unpack('i', max_tuple)
max = -1
return box.insert(spaceno, max + 1, ...)
(You can see a syntax-highlighted source with comments here.)
There is no need for locks, mutexes or alike: a stored procedure is atomic. There is also no lying and no mystery implementation
to support auto_increment for HASH keys: HASH isn't an ordered data structure, and :max() method for it simply throws
To make sure there is good code reuse, we created an open source repository with various data structures and algorithms for Tarantool in Lua.