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[0].idx:max() if max_tuple ~= nil then max = box.unpack('i', max_tuple[0]) else max = -1 end return box.insert(spaceno, max + 1, ...) end
(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 an exception.
To make sure there is good code reuse, we created an open source repository with various data structures and algorithms for Tarantool in Lua.