You are viewing kostja_osipov

Fish Magic - Post a comment

> Recent Entries
> Archive
> Friends
> Profile
> My photos at flickr

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[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.

Read Comments


Leave a comment


No HTML allowed in subject

  
 
   
 

(will be screened)


> Go to Top
LiveJournal.com